AWS for DevOps — Tagging & Right-Sizing

2 minute read

14.5 — Tagging & Right-Sizing

Tagging tells you who owns what. Right-sizing tells you what’s wasting money. Together, they’re the most impactful cost optimization techniques.

Resource Tagging

Tags are key-value pairs attached to AWS resources. They enable cost allocation, automation, and access control.

Mandatory Tag Strategy

Tag Key Purpose Example Values
Environment Cost allocation by env production, staging, dev
Team Cost allocation by team platform, backend, data
Service Identify the application api, web, worker
CostCenter Finance allocation CC-1001, CC-2002
ManagedBy How it was created terraform, cloudformation, manual

Enforcing Tags

# Use AWS Config rule to check for required tags
$ aws configservice put-config-rule \
    --config-rule '{
      "ConfigRuleName": "required-tags",
      "Source": {
        "Owner": "AWS",
        "SourceIdentifier": "REQUIRED_TAGS"
      },
      "InputParameters": "{\"tag1Key\":\"Environment\",\"tag2Key\":\"Team\",\"tag3Key\":\"Service\"}",
      "Scope": {
        "ComplianceResourceTypes": [
          "AWS::EC2::Instance",
          "AWS::RDS::DBInstance",
          "AWS::S3::Bucket"
        ]
      }
    }'

Using Tags for Cost Allocation

# Activate cost allocation tags (in Billing console or CLI)
$ aws ce get-cost-and-usage \
    --time-period Start=2026-03-01,End=2026-03-31 \
    --granularity MONTHLY \
    --metrics BlendedCost \
    --group-by Type=TAG,Key=Team
{
    "ResultsByTime": [{
        "Groups": [
            {"Keys": ["Team$platform"], "Metrics": {"BlendedCost": {"Amount": "320.50"}}},
            {"Keys": ["Team$backend"], "Metrics": {"BlendedCost": {"Amount": "210.30"}}},
            {"Keys": ["Team$data"], "Metrics": {"BlendedCost": {"Amount": "185.20"}}},
            {"Keys": ["Team$"], "Metrics": {"BlendedCost": {"Amount": "95.00"}}}
        ]
    }]
}
# Note: "Team$" means untagged resources — that $95 is invisible cost

Right-Sizing

Right-sizing means matching instance types to actual resource usage. Most instances are over-provisioned.

How to Identify Over-Provisioned Instances

# Get right-sizing recommendations from Cost Explorer
$ aws ce get-rightsizing-recommendation \
    --service "AmazonEC2" \
    --configuration '{
      "RecommendationTarget": "SAME_INSTANCE_FAMILY",
      "BenefitsConsidered": true
    }'

Check CloudWatch Metrics

# Average CPU for an instance over 14 days
$ aws cloudwatch get-metric-statistics \
    --namespace AWS/EC2 \
    --metric-name CPUUtilization \
    --dimensions Name=InstanceId,Value=i-0abc123def456 \
    --start-time "2026-03-16T00:00:00Z" \
    --end-time "2026-03-30T00:00:00Z" \
    --period 86400 \
    --statistics Average Maximum

Right-Sizing Decision Guide

Avg CPU Max CPU Memory Usage Action
< 10% < 30% < 30% Downsize by 2 sizes (e.g., m5.xlarge → m5.small)
10-30% < 50% < 50% Downsize by 1 size (e.g., m5.xlarge → m5.large)
30-60% < 80% 50-70% Correct size — keep as is
> 60% > 80% > 70% Upsize — may need bigger instance

Scheduling Non-Production Resources

Dev and staging environments don’t need to run 24/7:

# Stop dev instances at 7 PM, start at 8 AM
# Use AWS Instance Scheduler or a simple Lambda + EventBridge:

# EventBridge rule: stop at 7 PM IST (1:30 PM UTC) weekdays
$ aws events put-rule \
    --name stop-dev-instances \
    --schedule-expression "cron(30 13 ? * MON-FRI *)"

# EventBridge rule: start at 8 AM IST (2:30 AM UTC) weekdays
$ aws events put-rule \
    --name start-dev-instances \
    --schedule-expression "cron(30 2 ? * MON-FRI *)"

Savings: Running 10 hours/day × 5 days/week = 50 hours vs 168 hours = 70% savings on dev resources.

Common Mistakes

Mistake Why It Hurts Fix
No tagging strategy Can’t attribute costs to teams Enforce mandatory tags
Untagged resources Invisible costs Use Config rules to find untagged resources
Never right-sizing Paying for idle capacity Review monthly with Cost Explorer recommendations
Dev environments running 24/7 70% waste Schedule start/stop for business hours

Quick Reference

Task Command
Tag a resource aws ec2 create-tags --resources ID --tags Key=Team,Value=platform
Find untagged instances aws ec2 describe-instances --filters Name=tag-key,Values=Team --query 'Reservations[].Instances[?!Tags].InstanceId'
Get right-sizing recommendations aws ce get-rightsizing-recommendation --service AmazonEC2
Cost by tag aws ce get-cost-and-usage --group-by Type=TAG,Key=Team

Next → Cost Optimization Exercises

Updated: