Reputation: 52433
If you want to add a tag to an instance when launching, you have to perform two steps:
run-instances
)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
Reputation: 52433
This request had been pending for a long time and AWS finally supported this in March 2017
.
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
The following example applies a tag with a key of
webserver
and value ofproduction
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}]'
The command also applies a tag with a key of
cost-center
and a value ofcc123
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