helloV
helloV

Reputation: 52433

AWS EC2: Add a tag when launching an instance using CLI

If you want to add a tag to an instance when launching, you have to perform two steps:

  1. Launch an instance (run-instances)
  2. Add a tag to the newly created instance (create-tags)

Is there a way to add a tag (or set a name) when launching an instance using a single CLI command?

Upvotes: 1

Views: 1562

Answers (1)

helloV
helloV

Reputation: 52433

This request had been pending for a long time and AWS finally supported this in March 2017.

See: Amazon EC2 and Amazon EBS add support for tagging resources upon creation and additional resource-level permissions


Make sure your AWS CLI version is at least 1.11.106

$ aws --version
aws-cli/1.11.109 Python/2.6.9 Linux/4.1.17-22.30.amzn1.x86_64 botocore/1.5.72


CLI to tag the instance when launching:

The following example applies a tag with a key of webserver and value of production to the instance.

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}]'


CLI to tag the instance and the volume:

The command also applies a tag with a key of cost-center and a value of cc123 to any EBS volume that's created (in this case, the root volume).

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}]'

Upvotes: 5

Related Questions