Set the value of a key of tags to the result of a ternary operator using the current value

To participate into a payback program we’d to tag all of our resources with a specific key, value tuple. The value is now requested to have a different value than before but only for newly created resources. But key must not be changed. Said that newly created resources have to get the new value but existing should stay at old value even if the properties of the resources are changed.

This is the requirement.

Till now we defined the tags once in the provider and used them everywhere. Only in few cases we enriched tags by the key “Name” for convenience.

That’s why we’re looking for a super smart solution like this pseudo-code:

provider "aws" {
 ...
  default_tags {
    tags = local.tags
  }
}

locals {
  tags = {
    "payback" = payback_in_tf_stat_file_is_set_to == "2023" ? "2023" : "2024"
}

Wishful thinking to have a solution which could match the requirement?

You can have an expression which generates the tags, but I think setting default_tags in the aws provider will always update existing tags as well. Changing that behavior would require a new feature from the aws provider to assign something like default_tags but only the first time a resource is created.

I think you should be able to do this with individual resources. If you use ignore_changes on the tags attribute, the configured value will only be applied on creation, but updates to the config will be ignored. I’m not that familiar with the aws feature though, and that strategy may not work in combination with default_tags, so you would have to set it for all resources.

ignore_changes might work - will think about that - thx !

Well, the details …

EC2 instances / EKS needs special love wrt the launch template, attached EBS volumes too.
In the end it boils down to have dedicated provider for each different “payback” tag.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.