0
0
AWScloud~5 mins

Resource tagging for cost tracking in AWS - Commands & Configuration

Choose your learning style9 modes available
Introduction
When you use cloud resources, costs can add up quickly. Resource tagging helps you label your cloud items so you can see which projects or teams use what, making it easier to track and manage costs.
When you want to know how much a specific project is costing you in the cloud.
When you have multiple teams sharing cloud resources and want to split the bill fairly.
When you want to organize your cloud resources by environment like development, testing, and production.
When you need to generate reports showing costs by department or cost center.
When you want to automate actions based on resource tags, like shutting down unused resources.
Commands
This command adds tags to an EC2 instance. Tags are key-value pairs that help identify and organize your resources for cost tracking.
Terminal
aws ec2 create-tags --resources i-0abcdef1234567890 --tags Key=Project,Value=WebsiteRedesign Key=Environment,Value=Production
Expected OutputExpected
No output (command runs silently)
--resources - Specifies the resource ID to tag.
--tags - Defines the key-value pairs to assign as tags.
This command shows the tags assigned to the specified EC2 instance, so you can verify your tags were applied correctly.
Terminal
aws ec2 describe-tags --filters Name=resource-id,Values=i-0abcdef1234567890
Expected OutputExpected
{ "Tags": [ { "ResourceId": "i-0abcdef1234567890", "ResourceType": "instance", "Key": "Project", "Value": "WebsiteRedesign" }, { "ResourceId": "i-0abcdef1234567890", "ResourceType": "instance", "Key": "Environment", "Value": "Production" } ] }
--filters - Filters the output to show tags for the specified resource ID.
This command retrieves the cost for the tagged project 'WebsiteRedesign' for May 2024, helping you track expenses by tag.
Terminal
aws ce get-cost-and-usage --time-period Start=2024-05-01,End=2024-05-31 --granularity MONTHLY --filter '{"Tags":{"Key":"Project","Values":["WebsiteRedesign"]}}' --metrics "UnblendedCost"
Expected OutputExpected
{ "ResultsByTime": [ { "TimePeriod": { "Start": "2024-05-01", "End": "2024-05-31" }, "Total": { "UnblendedCost": { "Amount": "1234.56", "Unit": "USD" } } } ] }
--time-period - Specifies the date range for the cost query.
--filter - Filters costs by the specified tag key and value.
--metrics - Defines which cost metric to retrieve.
Key Concept

If you remember nothing else from this pattern, remember: tagging your cloud resources with clear key-value pairs lets you track and manage costs easily.

Common Mistakes
Using inconsistent tag keys or values across resources.
This makes it hard to group and track costs correctly because tags won't match.
Define a standard set of tag keys and allowed values and apply them consistently.
Not tagging all relevant resources.
Costs from untagged resources won't show up in tag-based reports, causing incomplete cost tracking.
Ensure all resources are tagged when created or use automation to tag existing resources.
Using tags with typos or extra spaces.
Tags with typos or spaces are treated as different tags, fragmenting cost data.
Double-check tag spelling and formatting before applying.
Summary
Use 'aws ec2 create-tags' to add key-value tags to your cloud resources.
Verify tags with 'aws ec2 describe-tags' to ensure correct application.
Query costs by tags using 'aws ce get-cost-and-usage' to track spending per project or team.