, . HCL2 (0.12) :
resource "aws_instance" "web" {
count = "${var.ec2_instance_count}"
ami = "${var.base_ami}"
availability_zone = "${var.region_a}"
instance_type = "${var.ec2_instance_size}"
security_groups = ["sec1"]
tags = {
Name = "${var.role} ${var.env}"
role = "${var.app_role}"
"${var.app_role}" = "${var.env}" # <------ like this
}
}
# 21566 , "${var.app_role}" (var.app_role), , .
( , , : var.app_role , .)
, . HCL2 (0.12) :
, merge :
variable "app_role" {
type = string
}
locals {
tags = merge(
{
Name = "${var.role} ${var.env}"
role = "${var.app_role}"
},
{
for k in [var.app_role]: k => "${var.env}"
}
)
}
Alternatively, you can use zipmapto create it in one go:
locals {
tags = zipmap(
[
"Name",
"role",
var.app_role
],
[
"${var.role} ${var.env}",
var.app_role,
var.env
]
)
}
Then you can use this map in the resource:
resource "aws_instance" "web" {
count = "${var.ec2_instance_count}"
ami = "${var.base_ami}"
availability_zone = "${var.region_a}"
instance_type = "${var.ec2_instance_size}"
security_groups = ["sec1"]
tags = local.tags // or inline the above here
}
One caveat: if it var.app_roleis equal to either "Name", or "role", it will overwrite your static value. You can avoid this by swapping the arguments in mergeor reordering the lists in zipmap, although such a collision is likely to be a configuration error that must be detected & corrected before application.
source
share