I have lots of AWS resources and need to tag each one with unique tags.
My structure is this:
terraform/
├─ main.tf
├─ variables.tf
├─ modules/
│ ├─ s3/
│ │ ├─ s3.tf
│ │ ├─ variables.tf
│ │ ├─
│ ├─ lambda/
│ │ ├─ lambda.tf
│ │ ├─ variables.tf
| |
|
I can apply default tags at the AWS provider level in main.tf:
provider "aws" {
region = "eu-west-2"
default_tags {
tags = {
Environment = var.environment
Terraform = "True"
}
}
}
And I use modules in main.tf to reference the resources:
module "s3_bucket" {
source = "./modules/s3"
bucket_name = var.bucket_name
}
However I also need to add a “Name” tag to each resource created, e.g:
resource "aws_s3_bucket" "bucket" {
bucket = "${var.bucket_name}"
tags = {
"Name" = "Bucket-1"
"Type" = "ObjectStore"
}
}
(I could use the bucket_name
here as the tag.Name, but the vast majority of modules don’t have a ‘name’ variable passed in like this)
Without having to manually add these tags at resource level for each resource I have, what would be the best way of adding them?
For example something like adding the tags for all S3 resources in one place.
The ‘Name’ tag isn’t very particular and just needs to sensibly describe the resource.