How to Add Tags During EC2 Instance Launch Using AWS CLI: A Developer’s Guide


2 views

Tags are metadata labels that help organize, track, and manage AWS resources. When launching EC2 instances through CLI, proper tagging becomes crucial for:

  • Cost allocation and reporting
  • Automated resource management
  • Security compliance tracking
  • Environment separation (prod/dev/staging)

The correct way to add tags during instance creation is through the --tag-specifications parameter:

aws ec2 run-instances \
--image-id ami-0abcdef1234567890 \
--instance-type t3.medium \
--key-name my-key-pair \
--tag-specifications 'ResourceType=instance,Tags=[{Key=Name,Value=Production-WebServer},{Key=Environment,Value=Prod}]'

For complex tagging requirements, consider these patterns:

1. Multiple Resource Tagging (Instance + Volume):

aws ec2 run-instances \
--image-id ami-0abcdef1234567890 \
--instance-type t3.medium \
--tag-specifications \
'ResourceType=instance,Tags=[{Key=Name,Value=API-Server},{Key=Owner,Value=DevOps}]' \
'ResourceType=volume,Tags=[{Key=Backup,Value=daily}]'

2. Dynamic Tag Generation (Using shell variables):

DEPLOY_ENV="staging"
TIMESTAMP=$(date +%Y%m%d)

aws ec2 run-instances \
--image-id ami-0abcdef1234567890 \
--tag-specifications \
"ResourceType=instance,Tags=[{Key=Environment,Value=$DEPLOY_ENV},{Key=DeploymentDate,Value=$TIMESTAMP}]"
  • JSON formatting errors: Always validate your tag specifications with jq or online validators
  • Tag propagation delay: Tags may take a few seconds to appear in the AWS console
  • Case sensitivity: Tag keys are case-sensitive - maintain consistency

After launch, verify tags with:

aws ec2 describe-tags --filters "Name=resource-id,Values=i-1234567890abcdef0"
  • Define a consistent tagging strategy before implementation
  • Include mandatory tags (Owner, Environment, CostCenter) in your IaC templates
  • Consider using AWS Config rules to enforce tagging compliance

When provisioning EC2 instances through the AWS Command Line Interface, adding tags at creation time is crucial for proper resource management. Tags help organize instances by purpose, owner, environment, or any custom classification that makes sense for your infrastructure.

The simplest way to add tags during instance creation is by using the --tag-specifications parameter:

aws ec2 run-instances \
    --image-id ami-0abcdef1234567890 \
    --instance-type t2.micro \
    --tag-specifications 'ResourceType=instance,Tags=[{Key=Name,Value=ProductionServer}]'

For more complex tagging scenarios, you can specify multiple tags and different resource types:

aws ec2 run-instances \
    --image-id ami-0abcdef1234567890 \
    --instance-type t2.micro \
    --tag-specifications 'ResourceType=instance,Tags=[{Key=Name,Value=WebServer},{Key=Environment,Value=Prod},{Key=Owner,Value=DevOpsTeam}]' \
    'ResourceType=volume,Tags=[{Key=Backup,Value=Daily}]'

Here's how to combine tagging with other common parameters like user data and security groups:

aws ec2 run-instances \
    --image-id ami-0abcdef1234567890 \
    --key-name my-key-pair \
    --security-group-ids sg-12345678 \
    --instance-type t2.micro \
    --user-data file://init-script.sh \
    --tag-specifications 'ResourceType=instance,Tags=[{Key=Name,Value=BastionHost},{Key=AutoStop,Value=true}]'

If you need to tag instances after creation, use the create-tags command:

aws ec2 create-tags \
    --resources i-1234567890abcdef0 \
    --tags Key=PatchGroup,Value=Critical
  • Always include a Name tag for quick identification
  • Use consistent tag keys across your organization
  • Consider automating tag enforcement through AWS Config
  • Leverage tags for cost allocation and reporting