【问题标题】:Terraform use backend on moduleTerraform 在模块上使用后端
【发布时间】:2020-06-25 11:52:52
【问题描述】:

我需要创建优化 terraform 的结构。

有我像模块一样导入的根路径变量 /variables.tf

variable "aws_profile" { default = "default" }
variable "aws_region" { default = "us-east-1" }

有了模块文件夹后 /ec2_instance/main.tf

module "global_vars" {
  source = "../"
}

provider "aws" {
  region  = module.global_vars.aws_region
  profile = module.global_vars.aws_profile
}

terraform {
  backend "s3" {
    encrypt = true
    bucket  = "some_bucket"
    key     = "path_to_statefile/terraform.tfstate"
    region  = "region"
    profile = "profile"
  }
}

module "instances_cluster" {
  some actions
}

它正在工作,但我需要将后端和提供程序部分移动到根文件夹上的 main.tf,然后像模块一样包含在内。 我该怎么做?

我尝试在带有后端部分的根文件夹上创建 /main.tf,但它们不工作并且支持在本地写入状态文件。

【问题讨论】:

    标签: amazon-s3 terraform terraform-provider-aws terraform-template-file


    【解决方案1】:

    您必须进行一些重构,但这些是我将采取的步骤

    1. 在 root 和 ec2_instance 模块中运行 terraform plan 以验证零更改,以便开始重构
    2. ec2_instance/main.tf 注释掉backend
    3. 将后端从ec2_instance/main.tf 放入根main.tf
    4. 在根main.tf 中,引用ec2_instance 模块
    5. 在根模块中运行terraform plan 并注意创建和删除
    6. 对于每个创建和删除对,创建一个terraform state mv 语句并运行每个
    7. 验证terraform plan 的更改为零

    【讨论】:

      猜你喜欢
      • 2017-12-02
      • 2021-06-26
      • 1970-01-01
      • 2021-04-14
      • 2018-06-03
      • 1970-01-01
      • 1970-01-01
      • 2021-12-30
      • 2019-11-11
      相关资源
      最近更新 更多