我的所有terraform文件都以:
开头terraform {
required_version = ">= 0.11, < 0.12"
backend "s3" {
bucket = "my-terraform-state"
key = "my-service/my-component/terraform.tfstate"
region = "eu-west-1"
encrypt = "true"
}
}
provider "aws" {
region = "eu-west-1"
version = ">= 1.8, < 1.9"
}
我想要的是
module "header" {
source = "module-location"
region = "eu-west-1"
state_key = "my-service/my-component"
}
当我尝试这样的事情时,它会失败,并显示后端配置不能包含变量的消息。我还读到了只读状态,并注意到它可以包含变量。
答案 0 :(得分:1)
您可以拥有一个名为terraform.tf
的单一文件:
terraform {
required_version = ">= 0.11, < 0.12"
backend "s3" {
bucket = "my-terraform-state"
key = "my-service/my-component/terraform.tfstate"
region = "eu-west-1"
encrypt = "true"
}
}
和provider.tf
:
provider "aws" {
region = "eu-west-1"
version = ">= 1.8, < 1.9"
}
将这些文件放在根目录中。 Terraform将使用它们,您不再需要将这些标题放在每个文件的顶部。
您不能在terraform.tf
中使用变量/插值,因为这个文件很早就被terraform使用了。