Terraform 动态内部动态

Terraform dynamic inside dynamic

想法是生成这样的东西:

  environment {
    ...

    environment_variable {
      ...
    }

    environment_variable {
      ...
    }
  }

其中有一个具有某些属性的环境块和具有某些属性的 0...n environment_variables。

我有以下模块,它在动态中使用动态。这可能吗?或者我该如何实现?

到目前为止我有以下模块,它在动态中使用动态:

模块:

dynamic "environment" {
  for_each = var.environment_definition

  content {
    field1 = environment.value.field1
    field2 = environment.value.field2

    dynamic "environment_variable" {
      for_each = length(var.environment_variables) == 0 ? [] : [var.environment_variables]

      content {
        name  = environment_variable.value.env_name
        value = environment_variable.value.env_value
      }
    }
  }
}

variables.tf:

variable "environment_definition" {
  description = ""
  type = any
  default = {}
}

variable "environment_variables" {
  description = ""
  type = map(object({
    env_name  = string
    env_value = string
  }))
  default = {}
}

上面的模块及其变量在我的 main.tf:

中被调用
  environment_definition = {
    field1                = "value"
    field2                = "value"
  }

  environment_variables = {
    tf_version = {
      env_name  = "TERRAFORM_VERSION"
      env_value = "0.12.28"
    },
    aws_account = {
      env_name  = "AWS_ACCOUNT"
      env_value = "Account123456"
    },
    environment = {
      env_name  = "ENVIRONMENT"
      env_value = "dev"
    }
  }

但是我收到这个错误:

Error: Missing map element

  on ../../../modules/codebuild/main.tf line 123, in resource "aws_codebuild_project" "this":
 152:           value = environment_variable.value.env_value
    |----------------
    | environment_variable.value is map of object with 3 elements

This map does not have an element with the key "env_value".

我怎样才能成功实施?

你试过这个吗?既然你有地图,我想你应该使用 keyvalue 来代替。

dynamic "environment" {
  for_each = var.environment_definition

  content {
    field1 = environment.value.field1
    field2 = environment.value.field2

    dynamic "environment_variable" {
      for_each = length(var.environment_variables) == 0 ? [] : [var.environment_variables]

      content {
        name  = environment_variable.key
        value = environment_variable.value
      }
    }
  }
}

下面的示例摘自 Terraform 的博客,认为它描述了您正在尝试做的事情:

# Configuration for Terraform 0.12

locals {
  standard_tags = {
    Component   = "user-service"
    Environment = "production"
  }
}

resource "aws_autoscaling_group" "example" {
  # ...

  tag {
    key                 = "Name"
    value               = "example-asg-name"
    propagate_at_launch = false
  }

  dynamic "tag" {
    for_each = local.standard_tags

    content {
      key                 = tag.key
      value               = tag.value
      propagate_at_launch = true
    }
  }
}

参考:https://www.hashicorp.com/blog/hashicorp-terraform-0-12-preview-for-and-for-each/

问题是你调用了根变量而不是 each.value:

content {
        name  = environment_variable.value.env_name
        value = environment_variable.value.env_value
      }

问题是你把你的映射放在一个数组中:

  for_each = length(var.environment_variables) == 0 ? [] : 
   [var.environment_variables]

所以你现在处理的是地图的数组,这意味着第一个元素

key = 0 and value = tf_version = { env_name = "TERRAFORM_VERSION" env_value = "0.12.28" }

您的代码应该是:

dynamic "environment_variable" {
      #solution1
      for_each = var.environment_variable
      #solution 2 am not sure this works but so you get the prob
     for_each = length(var.environment_variables) == 0 ? {} : 
   var.environment_variables
      content {
        name  = environment_variable.value.env_name
        value = environment_variable.value.env_value
      }
    }

而且你应该尝试更改动态名称更好

content {
        name  = <dynamic_name>.value.env_name
        value = <dynamic_name>.value.env_value
      }

https://www.hashicorp.com/blog/hashicorp-terraform-0-12-preview-for-and-for-each/