gpt4 book ai didi

terraform - 地形 map 中的变量键

转载 作者:行者123 更新时间:2023-12-04 01:23:42 27 4
gpt4 key购买 nike

在 Terraform 中,我正在尝试创建一个模块,其中包含一个带有变量键的 map 。我不确定这是否可能,但我尝试了以下但没有成功。

resource "aws_instance" "web" {
ami = "${var.base_ami}"
availability_zone = "${var.region_a}"
instance_type = "${var.ec2_instance_size}"
security_groups = ["sec1"]
count = "${var.ec2_instance_count}"
tags {
Name = "${var.role} ${var_env}"
role = "${var.app_role}"
${var.app_role} = "${var_env}"
}
}

还有这个:

tags {
Name = "${var.role} ${var_env}"
}
tags."${var.role}" = "${var.env}"

有任何想法吗?目前这对 Terraform 是不可能的吗?

最佳答案

(现在)有一个 lookup function terraform 插值语法支持,允许您在 map 中查找动态键。
使用它,我现在可以执行以下操作:

output "image_bucket_name" {
value = "${lookup(var.image_bucket_names, var.environment, "No way this should happen")}"
}
在哪里:
variable "image_bucket_names" {
type = "map"

default = {
development = "bucket-dev"
staging = "bucket-for-staging"
preprod = "bucket-name-for-preprod"
production = "bucket-for-production"
}

}
environment是一个简单的字符串变量。

关于terraform - 地形 map 中的变量键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35491987/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com