Use CreateTags with an Amazon SDK or CLI - Amazon Elastic Compute Cloud
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China (PDF).

Use CreateTags with an Amazon SDK or CLI

The following code examples show how to use CreateTags.

C++
SDK for C++
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

Aws::EC2::EC2Client ec2Client(clientConfiguration); Aws::EC2::Model::Tag nameTag; nameTag.SetKey("Name"); nameTag.SetValue(instanceName); Aws::EC2::Model::CreateTagsRequest createRequest; createRequest.AddResources(instanceID); createRequest.AddTags(nameTag); Aws::EC2::Model::CreateTagsOutcome createOutcome = ec2Client.CreateTags( createRequest); if (!createOutcome.IsSuccess()) { std::cerr << "Failed to tag ec2 instance " << instanceID << " with name " << instanceName << ":" << createOutcome.GetError().GetMessage() << std::endl; return false; }
  • For API details, see CreateTags in Amazon SDK for C++ API Reference.

CLI
Amazon CLI

Example 1: To add a tag to a resource

The following create-tags example adds the tag Stack=production to the specified image, or overwrites an existing tag for the AMI where the tag key is Stack.

aws ec2 create-tags \ --resources ami-1234567890abcdef0 \ --tags Key=Stack,Value=production

For more information, see This is the topic title in the Amazon Elastic Compute Cloud User Guide for Linux Instances.

Example 2: To add tags to multiple resources

The following create-tags example adds (or overwrites) two tags for an AMI and an instance. One of the tags has a key (webserver) but no value (value is set to an empty string). The other tag has a key (stack) and a value (Production).

aws ec2 create-tags \ --resources ami-1a2b3c4d i-1234567890abcdef0 \ --tags Key=webserver,Value= Key=stack,Value=Production

For more information, see This is the topic title in the Amazon Elastic Compute Cloud User Guide for Linux Instances.

Example 3: To add tags containing special characters

The following create-tags example adds the tag [Group]=test for an instance. The square brackets ([ and ]) are special characters, and must be escaped. The following examples also use the line continuation character appropriate for each environment.

If you are using Windows, surround the element that has special characters with double quotes ("), and then precede each double quote character with a backslash (\) as follows:

aws ec2 create-tags ^ --resources i-1234567890abcdef0 ^ --tags Key=\"[Group]\",Value=test

If you are using Windows PowerShell, surround the element the value that has special characters with double quotes ("), precede each double quote character with a backslash (\), and then surround the entire key and value structure with single quotes (') as follows:

aws ec2 create-tags ` --resources i-1234567890abcdef0 ` --tags 'Key=\"[Group]\",Value=test'

If you are using Linux or OS X, surround the element that has special characters with double quotes ("), and then surround the entire key and value structure with single quotes (') as follows:

aws ec2 create-tags \ --resources i-1234567890abcdef0 \ --tags 'Key="[Group]",Value=test'

For more information, see This is the topic title in the Amazon Elastic Compute Cloud User Guide for Linux Instances.

  • For API details, see CreateTags in Amazon CLI Command Reference.

PowerShell
Tools for PowerShell

Example 1: This example adds a single tag to the specified resource. The tag key is 'myTag' and the tag value is 'myTagValue'. The syntax used by this example requires PowerShell version 3 or higher.

New-EC2Tag -Resource i-12345678 -Tag @{ Key="myTag"; Value="myTagValue" }

Example 2: This example updates or adds the specified tags to the specified resource. The syntax used by this example requires PowerShell version 3 or higher.

New-EC2Tag -Resource i-12345678 -Tag @( @{ Key="myTag"; Value="newTagValue" }, @{ Key="test"; Value="anotherTagValue" } )

Example 3: With PowerShell version 2, you must use New-Object to create the tag for the Tag parameter.

$tag = New-Object Amazon.EC2.Model.Tag $tag.Key = "myTag" $tag.Value = "myTagValue" New-EC2Tag -Resource i-12345678 -Tag $tag
  • For API details, see CreateTags in Amazon Tools for PowerShell Cmdlet Reference.

For a complete list of Amazon SDK developer guides and code examples, see Create Amazon EC2 resources using an Amazon SDK. This topic also includes information about getting started and details about previous SDK versions.