Sub topic 6
Tags on AWS resources
This chapter covers how tags work on AWS resources: their structure, the rules that constrain them, how to apply them, and the mistakes that most often break cost reporting later. It assumes you can use the AWS Management Console and know what AWS accounts and AWS Organizations are. You should also recognise services such as EC2, S3 and RDS. The CLI examples are optional, but they show the exact behavior.
What a tag is
A tag is a label made of a key and a value, both plain text, that you attach to an AWS resource. For example, Project=checkout has the key Project and the value checkout. AWS does not interpret user-defined tags. They carry meaning only because people and tools agree on them.
Tags matter for cost analysis because a tag is the only way to tell Cost Explorer that an EC2 instance belongs to the checkout project rather than the search project. Without tags, you can group spend by service, account or region, but not by who or what the spend was for.
The rules for keys and values
The following limits apply to most services. Some services are stricter, so check the documentation for the service you use.
| Rule | Limit |
|---|---|
| Tags per resource | 50 user-defined tags |
| Key length | 1 to 128 characters |
| Value length | 0 to 256 characters (a value may be empty) |
| Keys per resource | Each key appears once. A resource cannot have two `Project` tags. |
| Case | Keys and values are case sensitive |
| Reserved prefix | `aws:` is reserved for AWS and cannot be used in your keys |
| Allowed characters | Letters, numbers, spaces, and `_ . : / = + - @` |
Case sensitivity causes the most trouble in practice. Project, project and PROJECT are three different keys. The values Prod, prod and production are three different values. Cost Explorer treats each one as a separate group, so inconsistent spelling splits one project's spend across several rows.
Applying tags
You can tag a resource in several places.
- At creation. Most consoles and APIs accept tags in the create request. This is the best moment, because the resource is never untagged.
- After creation. The console's Tags tab, the service's own tag API, or the Resource Groups Tagging API can add tags to an existing resource.
- In infrastructure as code. CloudFormation, Terraform and the CDK can apply tags to every resource in a stack.
- In bulk. Tag Editor in the console finds resources across Regions and services and edits their tags together.
The CLI call below adds two tags to an EC2 instance.
aws ec2 create-tags \
--resources i-0123456789abcdef0 \
--tags Key=Project,Value=checkout Key=Environment,Value=prodcreate-tags is idempotent for keys. If Project already exists on the instance, the call overwrites its value instead of adding a second tag. This makes it safe to run in a script more than once, but it also means a careless script can silently replace a correct value.
To confirm what is on the instance, read the tags back.
aws ec2 describe-tags \
--filters Name=resource-id,Values=i-0123456789abcdef0Where a tag can be placed
Not every tag on a resource's neighbours reaches the resource. Keep these points in mind.
- Tags belong to individual resources. An EC2 instance's tags are not copied to its EBS volumes, snapshots or network interfaces unless you ask for it. In the EC2 launch API, the
TagSpecificationsparameter lets you name which resource types receive the tags at launch. - Tags on an account, an organizational unit or a VPC do not flow down to the resources inside them. There is no inheritance by default.
- Some services create resources on your behalf, such as an Auto Scaling group launching instances. These services have their own setting for propagating tags to what they launch. In Auto Scaling groups, each tag has a
PropagateAtLaunchflag. - Some charges have no taggable resource at all, such as support fees or certain data transfer charges. These stay untagged no matter what you do.
The following diagram shows how a tag set on a group reaches an instance only when propagation is requested.
Tags are not retroactive
A tag describes a resource from the moment it is applied. Activating a tag for cost allocation, covered in a later chapter, also takes effect only going forward. If you tag an instance on the 20th, spend from the 1st to the 19th stays untagged in cost reports. A later chapter shows how to measure that gap.
This is the main reason to tag at creation and to decide on your keys before you build, not after the first large bill.
Tags and permissions
Tagging is controlled by IAM permissions, separate from the permission to create the resource. A user may be allowed to run instances but not to tag them. In that case a launch request that includes tags can fail with an authorization error, even though the same request without tags would succeed. The actions involved include ec2:CreateTags and, for the Resource Groups Tagging API, tag:TagResources.
You can also use tags in IAM policies, through condition keys such as aws:RequestTag/Project and aws:ResourceTag/Project. This is how teams require a tag at creation or restrict people to the resources of their own project. Treat this as a separate use of tags from cost allocation. A tag used for access control must be protected from changes by the people it restricts, while a tag used for cost reporting mostly needs to be present and consistent.
Common errors
- Inconsistent spelling.
Environment=prodon one resource andenvironment=Prodon another produce two groups. Agree on exact keys and allowed values and write them down. - Tagging only the main resource. The instance is tagged but its volumes and snapshots are not, so storage cost lands in the untagged group.
- Tagging after the fact and expecting history to change. Cost reports will not rewrite past days.
- Overwriting values through a script that reuses a key. Check the current tag before changing it.
- Reaching the 50-tag limit with free-form tags. Keep the set small and purposeful.
- Assuming a tag is visible in Cost Explorer as soon as it is on a resource. It is not. A key must also be activated as a cost allocation tag, and the data can take up to 24 hours to appear afterwards.
- Using the reserved
aws:prefix. The API rejects these keys for user-defined tags.
Checking your understanding
Exercise
A team launches an EC2 instance with the tags Project=checkout and Environment=prod. A week later, the monthly report grouped by Project shows the instance's EBS volume cost in the untagged group. Give two likely reasons and one fix for each.
Show answer
First, tags are not copied from an instance to its volumes. The launch request needed TagSpecifications that included the volume resource type. Fix: tag the volume directly with create-tags, or relaunch with tags specified for both instance and volume. Second, the key may not be activated as a cost allocation tag, or it was activated after the charges were incurred, since activation is not retroactive. Fix: activate the Project key from the management account and accept that earlier spend stays untagged. A misspelled key such as project instead of Project is a third possibility and is fixed by correcting the key on the volume.
Exercise
Why can a script that runs aws ec2 create-tags with Key=Environment,Value=staging against an existing prod instance cause a reporting problem, and what does it do to the instance's tags?
Show answer
create-tags overwrites the value when the key already exists, so the instance ends up with a single Environment tag whose value is staging. No error is raised. Its spend then moves from the prod group to the staging group in tag-based reports, from the time the change is picked up.