Using tags via AWS CLI

Overview

In this step, you’ll familiarize yourself with performing tagging operations from the AWS CLI.

To do this step, you need to have AWS CLI installed on your machine. Please refer to this link for installation instructions.

Content:

Adding tags to an existing EC2 resource

From the CLI, invoke ec2 create-tags with flags —resources and —tags

aws ec2 create-tags —resources <ResourceID> —tags Key=<Key>, value=<Value>

For example, if you wanted to create a tag of “Key=Environment, Value=Test” for an EC2 instance resource, your parameters will be:

aws ec2 create-tags —resources i-01234example56789 —tags Key=Environment, Value=Test

1.2_TagResource

Adding a tag to a new resource

Add tag to new virtual machines

From the CLI, invoke ec2 run-instances to create a new virtual machine and use the flag —tag-specifications to declare tag information:

aws ec2 run-instances\

—image-id <image-id> \

—count 1\

—instance-type t2.micro\

—key-name <YourKeyPair> \

—subnet-id <YouSubnetID> \

—tag-specifications resourceType=Instance, Tags= [{Key=Environment, Value=Test}] resourceType=Volume, Tags= [{Key=Environment, Value=Test}] 
#Volume created with the instance will also have the tag "Key=Environment, Value=Test"

Note: You will have to replace the appropriate parameters for your account.
The image below illustrates sample output:

1.2_CreateEC2

Add tag to new drive

From the CLI, invoke ec2 create-volume to create a new volume and use the flag —tag-specifications to declare the following tag information:

aws ec2 create-volume\

—availability-zone ap-southeast-1a\

—volume-type gp2\

—size 80\

—tag-specifications resourceType=Volume, Tags= [{Key=Environment, Value=Test}, {key=Cost-Center, value=CC123}]
#Volume will be assigned 2 tags as "Key=Environment, Value=Test" and "Key=Cost-Center, Value=CC123"

Note: You will have to replace the appropriate parameters for your account.
The picture below illustrates sample output:

1.2_CreateVolume

Description of tagged resources

From the CLI, invoke ec2 derscibe-instances with the flag —filters:

aws ec2 descripbe-instances —filters name=tag-key, Values=<SampleTagKey>

The picture below illustrates sample output:

1.2_Describe