Configure default tags for AWS resources
AWS recommends that you define a robust and consistent tagging strategy to enable better auditing, cost, and access control for your AWS resources. The AWS Terraform provider v3.38.0+ allows you to add default tags to all resources that the provider creates, making it easier to implement a consistent tagging strategy for all of the AWS resources you manage with Terraform.
In this tutorial, you will configure a set of default tags for your AWS resources. Then, you will override those tags for a specific resource. You will also learn how to use the default tags configuration to manage Auto Scaling group tags.
Prerequisites
You can complete this tutorial using the same workflow with either Terraform Community Edition or HCP Terraform. HCP Terraform is a platform that you can use to manage and execute your Terraform projects. It includes features like remote state and execution, structured plan output, workspace resource summaries, and more.
This tutorial assumes that you are familiar with the Terraform and HCP Terraform workflows. If you are new to Terraform, complete Get Started tutorials first. If you are new to HCP Terraform, complete the HCP Terraform Get Started tutorials first.
For this tutorial, you will need:
- Terraform v1.1+ installed locally.
- an HCP Terraform account and organization.
- HCP Terraform locally authenticated.
- an AWS account.
- the AWS CLI.
- an HCP Terraform variable set configured with your AWS credentials.
jq
installed.
Clone example repository
Clone the example repository for this tutorial, which contains configuration for an EC2 instance and an Auto Scaling group with default tags.
Change into the repository directory.
Review example configuration
Open main.tf
to review the example configuration.
The default_tags
block in the AWS provider defines Environment
and
Service
tags.
The default_tags
block applies tags to all resources managed by this
provider, except for the Auto Scaling groups (ASG).
Create infrastructure
Set the TF_CLOUD_ORGANIZATION
environment variable to your HCP Terraform
organization name. This will configure your HCP Terraform integration.
Initialize your configuration. Terraform will automatically create the learn-terraform-aws-default-tags
workspace in your HCP Terraform organization.
Note
This tutorial assumes that you are using a tutorial-specific HCP Terraform organization with a global variable set of your AWS credentials. Review the Create a Credential Variable Set for detailed guidance. If you are using a scoped variable set, assign it to your new workspace now.
Apply your configuration to deploy an EC2 instance and Auto Scaling group.
Respond yes
to the prompt to confirm the operation.
Tip
This tutorial shows the output for Terraform commands run in HCP Terraform. If you are following the Terraform Community Edition workflow, the output may differ slightly but the results will be the same.
If you use HCP Terraform to provision your resources, your workspace now displays the list of all of the resources it manages.
Now, verify the EC2 instance tags using the AWS CLI.
Then, check the tags on the Auto Scaling group. As expected, Terraform did not apply the default tags to this resource.
You can also verify the tags on your resources by inspecting your EC2 instances and Auto Scaling groups in the AWS console.
Propagate default tags to Auto Scaling group
An Auto Scaling group is a collection of EC2 instances that use the same configuration. You can define the range of instances in an Auto Scaling group and the desired count, and the service will ensure that that number of instances is running at any given time. If an instance is terminated, the Auto Scaling group will launch another in its place using the launch configuration at the time.
AWS Auto Scaling Groups (ASGs) dynamically create and destroy EC2 instances as defined in the ASG's configuration. Because these EC2 instances are created and destroyed by AWS, Terraform does not manage them, and is not directly aware of them. As a result, the AWS provider cannot apply your default tags to the EC2 instances managed by your ASG. You can, however, use a data source and dynamic blocks to apply the default tags set on the provider to EC2 instances managed by your ASG.
Add the following data source to your main.tf
file to access the default tags
configured for the workspace's provider.
Then, add the following block to the aws_autoscaling_group.example
configuration to dynamically generate a tag block for each item in the
aws_default_tags
data source.
Now apply your configuration again. Respond yes
to the prompt to add the tags to your Auto Scaling Group's configuration.
Use the AWS CLI to confirm that the Auto Scaling group now has the default tags.
After the previous apply, the running EC2 instance in the Auto Scaling group does not have the desired default tags because the instance only has the tags configured for the Auto Scaling group at the time of the instance's launch.
Check the Auto Scaling group EC2 instance tags using the AWS CLI.
The tags include the default ASG tags but not the default tags from your provider configuration.
Use the -replace
option for terraform apply
to reprovision the Auto Scaling
group and launch a new instance with the appropriate tags.
Confirm that the new EC2 instance managed by the ASG has the default tags.
Override default tags
You may occasionally need to override the default tags to customize a resource.
When you define the same tag in both the default_tags
configuration in the
provider and on a resource, the resource-specific tag overwrites the default
setting.
Add the following configuration to your aws_instance.example
resource in
main.tf
.
Run terraform apply
again to update the tags on the instance. Notice that the
execution plan shows updates to two fields, tags
and tags_all
. The tags
attribute represents the resource-specific tags in Terraform state, while
tags_all
is the total of the resource tags and the default tags specified on
the provider.
Respond yes
to the prompt to confirm the change.
Inspect the updated tags using the AWS CLI. Notice that the EC2 instance's
tags have replaced the default Service
tag.
Destroy infrastructure
Run terraform destroy
to clean up your provisioned infrastructure. Respond
yes
to the prompt to confirm the operation.
If you used HCP Terraform for this tutorial, after destroying your resources, delete the learn-terraform-aws-default-tags
workspace from your HCP Terraform organization.
Next steps
In this tutorial, you configured and overrode default tags for AWS resources. You also learned how to add default tags to Auto Scaling groups using dynamic blocks. To learn more about the configuration and concepts used in this tutorial, review the following:
- Learn how to query resources using data sources.
- Review how to create dynamic configuration using
for_each
blocks. - Read the AWS white paper on tagging strategies.