Thursday, 2 May 2019

How to work with Tags Using the AWS CLI by Raj Gupta







1. The following command describes the instances with a Env tag, regardless of the value of the tag

[root@ip-172-31-89-253 ~]# aws ec2 describe-instances --filters Name=tag-key,Values=Env


2. The following command describes the instances with the tag Env=Production.

[root@ip-172-31-89-253 ~]# aws ec2 describe-instances --filters Name=tag:Env,Values=Production

3. The following command describes the instances with a tag with the value production, regardless of the tag key

[root@ip-172-31-89-253 ~]# aws ec2 describe-instances --filters Name=tag-value,Values=Production

4. Add a tag to a resource

[root@ip-172-31-89-253 ~]# aws ec2 create-tags --resources i-06c71b2bc3588adb8 --tags Key=Raj,Value=Gupta

5. Add tags with special characters

This example adds the tag [Group]=test to an instance. The square brackets ([ and ]) are special characters, and must be escaped with enclose the entire key and value structure with single quotes ('), and then enclose the element with the special character with double quotes (")..

[root@ip-172-31-89-253 ~]# aws ec2 create-tags --resources i-06c71b2bc3588adb8 --tags 'Key="[Group]",Value=test'

6. Add tags to multiple resources

[root@ip-172-31-89-253 ~]# aws ec2 create-tags --resources i-070e26baf98bd7575 i-06c71b2bc3588adb8 --tags Key=Raj1,Value=Gupta1 Key=Raj2,Value=Gupta2

7. Create a volume and apply a tag

The following command creates a volume and applies two tags: purpose = production, and costcenter = cc123.

aws ec2 create-volume --availability-zone us-east-1a --volume-type gp2 --size 80 --tag-specifications 'ResourceType=volume,Tags=[{Key=purpose,Value=production},{Key=costcenter,Value=cc123}]'

8. Launch an instance and apply tags to the instance and volume

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

No comments:

Post a Comment