Bird
Raised Fist0
AWScloud~5 mins

Resource tagging for cost tracking in AWS - Commands & Configuration

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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.

Practice

(1/5)
1. What is the main purpose of adding tags to AWS resources for cost tracking?
easy
A. To organize and identify resources for cost allocation
B. To increase the storage capacity of resources
C. To improve the speed of resource deployment
D. To automatically back up resources daily

Solution

  1. Step 1: Understand the role of tags in AWS

    Tags are labels that help organize resources by adding key-value pairs.
  2. Step 2: Connect tags to cost tracking

    Tags allow grouping resources to see costs clearly in reports.
  3. Final Answer:

    To organize and identify resources for cost allocation -> Option A
  4. Quick Check:

    Tags help track costs [OK]
Hint: Tags label resources to track costs easily [OK]
Common Mistakes:
  • Thinking tags increase storage or speed
  • Confusing tags with backups
  • Assuming tags change resource performance
2. Which of the following is the correct syntax to add a tag with key Environment and value Production to an AWS EC2 instance using AWS CLI?
easy
A. aws ec2 tag-instance --id i-1234567890abcdef0 --key Environment --value Production
B. aws ec2 create-tags --resources i-1234567890abcdef0 --tags Key=Environment,Value=Production
C. aws ec2 add-tag --instance i-1234567890abcdef0 --tag Environment=Production
D. aws ec2 set-tags --resource i-1234567890abcdef0 --tags Environment:Production

Solution

  1. Step 1: Recall AWS CLI command for tagging

    The correct command is aws ec2 create-tags with resource ID and tags specified as Key=Value pairs.
  2. Step 2: Match syntax with options

    aws ec2 create-tags --resources i-1234567890abcdef0 --tags Key=Environment,Value=Production matches the correct syntax exactly.
  3. Final Answer:

    aws ec2 create-tags --resources i-1234567890abcdef0 --tags Key=Environment,Value=Production -> Option B
  4. Quick Check:

    Correct AWS CLI tag syntax [OK]
Hint: Use 'create-tags' with Key= and Value= pairs [OK]
Common Mistakes:
  • Using wrong command names like add-tag or tag-instance
  • Incorrect tag format without Key= and Value=
  • Mixing resource and instance flags
3. Given the following AWS CLI command, what will be the effect?
aws ec2 create-tags --resources i-0abcd1234efgh5678 --tags Key=Project,Value=Alpha Key=Owner,Value=TeamA
medium
A. The instance will have two tags: Project=Alpha and Owner=TeamA
B. The command will fail due to multiple tags in one command
C. Only the last tag Owner=TeamA will be applied
D. The tags will overwrite existing tags on the instance

Solution

  1. Step 1: Understand AWS CLI tagging with multiple tags

    The create-tags command supports multiple tags by repeating the Key=Value pairs.
  2. Step 2: Analyze the command effect

    Both tags Project=Alpha and Owner=TeamA will be added to the instance.
  3. Final Answer:

    The instance will have two tags: Project=Alpha and Owner=TeamA -> Option A
  4. Quick Check:

    Multiple tags added together [OK]
Hint: Multiple tags can be added by repeating Key=Value pairs [OK]
Common Mistakes:
  • Assuming only one tag can be added at a time
  • Thinking the command fails with multiple tags
  • Believing tags overwrite all existing tags
4. You tried to tag an AWS S3 bucket using this command:
aws s3api put-bucket-tagging --bucket my-bucket --tags Key=Environment,Value=Dev

But it failed. What is the likely error?
medium
A. S3 buckets cannot be tagged
B. The bucket name is invalid
C. The command should be run with root user only
D. The --tags parameter is missing the required JSON format

Solution

  1. Step 1: Check S3 tagging command syntax

    The put-bucket-tagging command requires tags in JSON format under the --tagging parameter, not --tags.
  2. Step 2: Identify the error cause

    Using --tags with Key=Value string causes syntax error; correct usage is JSON with --tagging.
  3. Final Answer:

    The --tags parameter is missing the required JSON format -> Option D
  4. Quick Check:

    S3 tagging needs JSON format [OK]
Hint: Use JSON format with --tagging for S3 bucket tags [OK]
Common Mistakes:
  • Using --tags instead of --tagging with JSON
  • Assuming only root user can tag buckets
  • Believing S3 buckets cannot be tagged
5. You want to track costs for multiple projects in your AWS account. Which tagging strategy will give the clearest cost reports?
hard
A. Tag only the resources with the highest cost
B. Use different tag keys like Project1, Project2 for each project
C. Use a single tag key Project with unique values for each project on all resources
D. Use tags only on EC2 instances, ignoring other resources

Solution

  1. Step 1: Understand best practice for cost tracking tags

    Using one consistent tag key with different values groups costs clearly by that key.
  2. Step 2: Evaluate options for clarity

    Use a single tag key Project with unique values for each project on all resources uses a single key Project with unique values, making reports easy to filter and compare.
  3. Final Answer:

    Use a single tag key Project with unique values for each project on all resources -> Option C
  4. Quick Check:

    Consistent tag keys for clear cost reports [OK]
Hint: Use one tag key with different values for projects [OK]
Common Mistakes:
  • Using multiple tag keys for similar data
  • Tagging only some resources
  • Ignoring tags on non-EC2 resources