Module - Variable Tags

Looking on how to accomplish variable tags for reusable modules. Cant seem to find any good documentation on how to accomplish this.

Hi @stevenjw0228001,

Unfortunately I’m not sure what you mean by “variable tags”. It would help if you could describe what you want to achieve in some more detail, ideally including some things you might’ve tried or considered so far, even if they didn’t work out, because that at least helps put your goals in context and gives a starting point for approaches that might work.

I’m in agreement with @apparentlymart in not being quite sure what you mean by “variable tags” but I can share how I have my modules utilize tags if that helps and serves as a basis to start from.

In most all of my modules, I insure that a ManagedBy tag is created and set it to Terraform but also wanting to allow for customizable additional tags to be added as I write them to be used to work as a template I use the following pattern in my module code for any resource I wish to tag.

  tags = merge(
      ManagedBy = "Terraform"

I then just have the definition for additional_tags as:

variable "additional_tags" {
  type        = map(string)
  description = "A mapping of additional resource tags"
  default     = {}

This has allowed me to enforce certain tags be present while also being flexible enough to support additional custom tags.

If I build a module for EC2 and I want to always Tag my instance with certain keys that I will be changing per instance deployment, how do you go about that?

You might be coming from CloudFormation which adds some tags without writing anything in the template. Terraform does not have an equivalent “automatic tagging” feature.

You have to add tags yourself explicitly.
The use of the function merge as described by @jbouse is one method.
There is also a more recent alternative for EC2 resources, an independant ec2_tag resource. See