Terraform Create AWS Tag and Value based off of a variable

  Kiến thức lập trình

We have different environments within our AWS infrastructure which are stored in a CloudFormation export. I am automating a daily shutdown down of Dev environments and the script checks to see if a certain tag exists on any EC2 in the environment. I would like to write my TF in such a manner that if the “environment” is “dev” we create a tag with a value “whatever” otherwise the value is blank. This is what I have so far:

data "aws_cloudformation_export" "envshort" {
name = "${var.mycode}—EnvShort"
}

tags = merge(
{
scheduler-policy = data.aws_cloudformation_export.envshort.value == "dev" ? "weekdays" : ""
},
local.tags,
)

When I run my plan, nothing is created it seems to just disappear.

of course when I do:

tags = merge(
{
scheduler-policy = "weekdays"
},
local.tags,
)

Everything works as expected. So my question is: Can I do this and my syntax is just wrong or is it just not possible.

LEAVE A COMMENT