地形动态内部动态

时间:2020-07-16 12:18:39

标签: terraform terraform-provider-aws

想法是生成这样的东西:

  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".

我如何成功实现呢?

2 个答案:

答案 0 :(得分:1)

您尝试过吗?既然您有地图,我认为您应该改用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/

答案 1 :(得分:1)

问题是,您调用了根变量,因此而不是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]

所以您现在要处理地图的第一个元素

键= 0且 值= 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/