Skip to content

Instantly share code, notes, and snippets.

@Rabattkarte
Created October 13, 2022 14:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Rabattkarte/2996b019b73476ccc08569390ef76d61 to your computer and use it in GitHub Desktop.
Save Rabattkarte/2996b019b73476ccc08569390ef76d61 to your computer and use it in GitHub Desktop.
How to merge default tags with user provided tags in Terraform HCL
# Define a variable for user-provided tags as a map of strings.
variable "user_tags" {
type = map(string)
default = {}
description = "A user defined map of tags that should be attached to every tagable resource."
}
locals {
# Define the common tags that are added to all resources.
common_tags = {
"Managed by" = "Terraform"
"Terraform module" = path.module
}
# Merge `local.common_tags` with `var.user_tags` in a new local.
# `var.custom_tags` takes precedence.
all_tags = merge(
local.common_tags,
var.user_tags
)
}
# Define a resource that allows tags.
resource "provider_resource" "placeholder" {
# Resource configuration ...
# Use `local.all_tags` in your module's resources.
tags = local.all_tags
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment