从地形设置变量中提取值

Extract Values from terraform set variable

我正在尝试使用 terraform 资源配置 aws 服务目录产品

resource "aws_servicecatalog_provisioned_product" "example" {}

Terraform resource output description

资源的导出值之一是 outputs,它是集合的形式,我正在使用下面的

将其收集到输出变量中
output "Provisioned_Product_Outputs" {
  value = aws_servicecatalog_provisioned_product.example.outputs
} 

输出看起来像

Provisioned_Product_Outputs = toset([
  {
    "description" = "Backup plan"
    "key" = "BackupPlan"
    "value" = "light"
  },
  {
    "description" = "Current user zone to run"
    "key" = "CurrentAZ"
    "value" = "primary"
  },
  {
    "description" = "InstanceID of Vm"
    "key" = "EC2InstanceID"
    "value" = "i-04*******"
  },
  {
    "description" = "InstanceHostName"
    "key" = "InstanceHostName"
    "value" = "{\"fqdn\":\"foo.domain.com\"}"
  },
  {
    "description" = "The ARN of the launched Cloudformation Stack"
    "key" = "CloudformationStackARN"
    "value" = "arn:aws:cloudformation:{region}:{AccountID}:stack/SC-{AccountID}-pp-iy******"
  },
])

我想只选择输出值而不是像下面这样的整个集合。

output "EC2InstanceID" {
  value = "i-04*******"
} 

output "InstanceHostName" {
  value = ""{\"fqdn\":\"foo.domain.com\"}""
}

output "CloudformationStackARN" {
  value =  "arn:aws:cloudformation:{region}:{AccountID}:stack/SC-{AccountID}-pp-iy******"
}

有没有办法应用或有一些条件允许我使用键值对检查正确的值并在输出中应用该值

问候

因为您知道您的输出是 set,您可以使用 contains:

在集合内的对象上创建一个过滤器
output "outputs" {
  value = {
    for output in aws_servicecatalog_provisioned_product.example.outputs : output.key =>
    output.value if contains(["EC2InstanceID", "InstanceHostName", "CloudformationStackARN"], output.key)
  }
}

输出将类似于:

outputs = {
  "CloudformationStackARN" = "arn:aws:cloudformation:{region}:{AccountID}:stack/SC-{AccountID}-pp-iy******"
  "EC2InstanceID" = "i-04*******"
  "InstanceHostName" = "{\"fqdn\":\"foo.domain.com\"}"
}

如果你想有单独的输出,你必须手动输入每个输出:

output "EC2InstanceID" {
  value = [for output in aws_servicecatalog_provisioned_product.example.outputs : output.value if output.key == "EC2InstanceID"][0]
}

output "InstanceHostName" {
  value = [for output in aws_servicecatalog_provisioned_product.example.outputs : output.value if output.key == "InstanceHostName"][0]
}

output "CloudformationStackARN" {
  value = [for output in aws_servicecatalog_provisioned_product.example.outputs : output.value if output.key == "CloudformationStackARN"][0]
}

输出不能有 for_each 属性。目前 resourcemodule 块支持 for_each 属性。