Add tags to Amazon EC2 resources using an Amazon SDK - 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).

Add tags to Amazon EC2 resources using an Amazon SDK

The following code example shows how to add tags to Amazon EC2 resources.

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.

For a complete list of Amazon SDK developer guides and code examples, see Using this service with an Amazon SDK. This topic also includes information about getting started and details about previous SDK versions.