有没有办法将terraform后端配置放在模块中?

时间:2018-02-04 00:49:29

标签: terraform

我的所有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"
}

当我尝试这样的事情时,它会失败,并显示后端配置不能包含变量的消息。我还读到了只读状态,并注意到它可以包含变量。

1 个答案:

答案 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使用了。