【问题标题】:Create multiple objects from one Terraform module using Terragrunt使用 Terragrunt 从一个 Terraform 模块创建多个对象
【发布时间】:2019-12-31 21:34:43
【问题描述】:

我正在通过 terragrunt 使用 terraform。 我有一个文件夹,里面有一个 terragrunt.hcl 文件。该文件的目的是在 GCP 中创建多个子网。

为了创建子网,我有一个模块,它接受多个输入。

我希望能够在我的 terragrunt.hcl 文件中创建多个子网。

我认为最好的方法是创建一个包含字典的列表(或 terraform 所称的地图),然后对其进行迭代。

我有一些代码不工作

这是一些无效的代码。

#terragrunt.hcl

include {
  path = find_in_parent_folders()
}

  inputs = {
    # Common tags to be assigned to all resources
    subnetworks = [
      {
        "subnetName": "subnet1-euw"
        "subNetwork": "10.2.0.0/16"
        "region": "europe-west1"
      },
      {
        "subnetName": "subnet1-usc1"
        "subNetwork": "10.3.0.0/16"
        "region": "us-central1"
      }
    ]
  }

terraform {
  module "subnetworks" {
    source = "github.com/MyProject/infrastructure-modules.git//vpc/subnetwork"
    vpc_name = "MyVPC"
    vpc_subnetwork_name = [for network in subnetworks: network.subnetName]
    vpc_subnetwork_cidr = [for network in subnetworks: network.subNetwork]
    vpc_subnetwork_region = [for network in subnetworks: network.region]
  }
}

似乎我不能在“terraform”块内使用“模块”。希望代码至少显示了我想要实现的目标。

作为参考,我调用的模块如下所示

#main.tf

terraform {
  # Intentionally empty. Will be filled by Terragrunt.
  backend "gcs" {}
}

resource "google_compute_subnetwork" "vpc_subnetwork" {
  name          = var.vpc_subnetwork_name
  ip_cidr_range = var.vpc_subnetwork_cidr
  region        = var.vpc_subnetwork_region
  network       = var.vpc_name
}
#variables.tf
variable "vpc_name" {
  description = "Name of VPC"
  type        = string
}

variable "vpc_subnetwork_name" {
  description = "Name of subnetwork"
  type        = string
}

variable "vpc_subnetwork_cidr" {
  description = "Subnetwork CIDR"
  type        = string
}

variable "vpc_subnetwork_region" {
  description = "Subnetwork region"
  type        = string
}

【问题讨论】:

    标签: terraform terragrunt


    【解决方案1】:

    Terragrunt 没有循环结构。在 Terragrunt 中,您将使用目录层次结构来做您想做的事情。例如,要实现上述目标,如下所示:

    └── live
        ├── empty.yaml
        ├── euw
        │   ├── region.yaml
        │   └── vpc
        │       └── terragrunt.hcl
        ├── terragrunt.hcl
        └── usc1
            ├── region.yaml
            └── vpc
                └── terragrunt.hcl
    

    在live/terragrunt.hcl 中,您可以在 terragrunt 配置中使用其他 yaml 文件:

    # live/terragrunt.hcl
    inputs = merge(
      # Configure Terragrunt to use common vars encoded as yaml to help you keep often-repeated variables (e.g., account ID)
      # DRY. We use yamldecode to merge the maps into the inputs, as opposed to using varfiles due to a restriction in
      # Terraform >=0.12 that all vars must be defined as variable blocks in modules. Terragrunt inputs are not affected by
      # this restriction.
      yamldecode(
        file("${get_terragrunt_dir()}/${find_in_parent_folders("region.yaml", "${path_relative_from_include()}/empty.yaml")}"),
      )
    )
    

    在每个区域内的region.yaml 中,您只需说明该区域:

    # live/euw/region.yaml
    # These variables apply to this entire region. They are automatically pulled in using the extra_arguments
    # setting in the root terraform.tfvars file's Terragrunt configuration.
    region: "europe-west1"
    
    # live/usc1/region.yaml
    region: "us-central1"
    

    现在您可以将每个区域 terragrunt.hcl 文件中的区域作为变量引用:

    # live/euw/vpc/terragrunt.hcl
    terraform {
      source = "github.com/MyProject/infrastructure-modules.git//vpc/subnetwork"
    }
    
    include {
      path = find_in_parent_folders()
    }
    
    inputs = {
      vpc_subnetwork_name = "subnet1-${region}"
      vpc_subnetwork_cidr = "10.2.0.0/16"
      vpc_subnetwork_region = region
      vpc_name = "MyVPC"
    }
    

    还有:

    # live/usc1/vpc/terragrunt.hcl
    terraform {
      source = "github.com/MyProject/infrastructure-modules.git//vpc/subnetwork"
    }
    
    include {
      path = find_in_parent_folders()
    }
    
    inputs = {
      vpc_subnetwork_name = "subnet1-${region}"
      vpc_subnetwork_cidr = "10.3.0.0/16"
      vpc_subnetwork_region = region
      vpc_name = "MyVPC"
    }
    

    您可能会发现来自 Gruntwork 的 example terragrunt repository 很有帮助。

    【讨论】:

    • 在您的示例中,您正在为多个区域设置相同的 CIDR。正如您在我的示例中看到的,不同的区域具有不同的 CIDR。如果您可以将我的示例中的两个子网都包含在您的示例中,那么这可能会更清楚。谢谢!
    • 当然,我认为从布局上看很清楚,但我将其更新为显式。
    • 如果我们需要在yaml文件中表示一张地图怎么办?
    猜你喜欢
    • 2021-05-27
    • 1970-01-01
    • 1970-01-01
    • 2022-09-28
    • 2022-07-01
    • 1970-01-01
    • 2018-12-14
    • 1970-01-01
    • 2017-05-07
    相关资源
    最近更新 更多