Skip to content

Instantly share code, notes, and snippets.

@AlexAtkinson
Last active February 22, 2023 20:08
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 AlexAtkinson/199748aabd22e6dfc5868fa54c80685f to your computer and use it in GitHub Desktop.
Save AlexAtkinson/199748aabd22e6dfc5868fa54c80685f to your computer and use it in GitHub Desktop.
Terraform: Apply tags to all resources (with automatic repo url construction for env0)
# Working Example:
# 1. Copy this content into a .tf file.
# 2. run: terraform init
# 3. run: terraform plan
# Note: Orchestration creates a valid URL within env0 ci.
locals {
git_org_url = "https://github.com/foo"
git_repo = "terraform-foo"
git_branch = "main"
global_tags = {
#Environment = var.WORKLOAD_ENVIRONMENT
#WorkloadGroup = var.WORKLOAD_GROUP
#Application = var.application
#Department = var.department
Environment = "WORKLOAD_ENVIRONMENT"
WorkloadGroup = "WORKLOAD_GROUP"
Application = "application"
Department = "department"
# Orchestration = "${local.git_org_url}/${local.git_repo}/tree/${local.git_branch}${replace(abspath(path.cwd), "/.*${local.git_repo}/", "")}"
Orchestration = "${local.git_org_url}/${local.git_repo}/tree/${local.git_branch}${replace(abspath(path.cwd), "/\\/tmp\\/(\\w+-?)+/", "")}"
CostCenter = "ZZ ${title("fooBar")}"
}
module_tags = {
# Must be defined globally to enable the merge function in snip-provider.tf
# Override by creating a map 'module_tags' map within a file named 'locals_override.tf'
}
}
provider "aws" {
region = "us-east-2"
default_tags {
tags = merge(
local.global_tags,
local.module_tags
)
}
}
output "tags" {
value = merge(local.global_tags,local.module_tags)
}
# locals_override.tf example.
# Put in separate tf file and uncomment.
#locals {
# module_tags = {
# Foo = "foo",
# Bar = "bar"
# }
#}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment