5

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
14

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

  • 1
    just a small add - you need to upgrade or reinstall your AWS EC2 Cli – Up_One Nov 11 '17 at 0:38
1

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

  • I need to add tags when creating instances, like from GUI – Anuruddha Lanka Liyanarachchi Sep 24 '15 at 9:37
  • There's no way to do it in a single command. You must use this second command after the instance is launched. The GUI does the same thing but it hides it. – Matt Houser Sep 25 '15 at 13:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.