14

I am trying to create an instance in ec2 using CLI. Is there anyway to specify tags to the instance when using CLI to create instances?

aws ec2 run-instances --image-id $ami_id --key-name $deployment_key_name \
--region $region --security-groups default --instance-type m4.large \
--user-data file://./yaml/master.yaml

2 Answers2

28

As of 28 March 2017, you can specify tags for instances (and attached volumes) as part of the run-instances command.

Example:

aws ec2 run-instances --image-id ami-abc12345 --count 1 \
--instance-type t2.micro --key-name MyKeyPair \
--subnet-id subnet-6e7f829e \
--tag-specifications 'ResourceType=instance,Tags=[{Key=webserver,Value=production}]' 'ResourceType=volume,Tags=[{Key=cost-center,Value=cc123}]' 

Announcement blog post: https://aws.amazon.com/blogs/aws/new-tag-ec2-instances-ebs-volumes-on-creation/

Additional documentation (see example 4): http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/Using_Tags.html#Using_Tags_CLI

TylerW
  • 556
  • 4
  • 8
0

Use the aws ec2 create-tags command afterwards to add tags by instance ID.

mschuett
  • 3,066
  • 20
  • 21