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
. //! Add or overwrite only the specified tags for the specified Amazon Elastic Compute Cloud (Amazon EC2) resource or resources. /*! \param resources: The resources for the tags. \param tags: Vector of tags. \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ bool AwsDoc::EC2::createTags(const Aws::Vector<Aws::String> &resources, const Aws::Vector<Aws::EC2::Model::Tag> &tags, const Aws::Client::ClientConfiguration &clientConfiguration) { Aws::EC2::EC2Client ec2Client(clientConfiguration); Aws::EC2::Model::CreateTagsRequest createTagsRequest; createTagsRequest.SetResources(resources); createTagsRequest.SetTags(tags); Aws::EC2::Model::CreateTagsOutcome outcome = ec2Client.CreateTags(createTagsRequest); if (outcome.IsSuccess()) { std::cout << "Successfully created tags for resources" << std::endl; } else { std::cerr << "Failed to create tags for resources, " << outcome.GetError().GetMessage() << std::endl; } return outcome.IsSuccess(); }
-
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 tagStack=production
to the specified image, or overwrites an existing tag for the AMI where the tag key isStack
.aws ec2 create-tags \ --resources
ami-1234567890abcdef0
\ --tagsKey=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
\ --tagsKey=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
^
--resourcesi-1234567890abcdef0
^
--tags Key=\"[Group]\",Value=testIf 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
`
--resourcesi-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.
-
- Rust
-
- SDK for Rust
-
Note
There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository
. This example applies the Name tag After creating an instance.
pub async fn create_instance<'a>( &self, image_id: &'a str, instance_type: InstanceType, key_pair: &'a KeyPairInfo, security_groups: Vec<&'a SecurityGroup>, ) -> Result<String, EC2Error> { let run_instances = self .client .run_instances() .image_id(image_id) .instance_type(instance_type) .key_name( key_pair .key_name() .ok_or_else(|| EC2Error::new("Missing key name when launching instance"))?, ) .set_security_group_ids(Some( security_groups .iter() .filter_map(|sg| sg.group_id.clone()) .collect(), )) .min_count(1) .max_count(1) .send() .await?; if run_instances.instances().is_empty() { return Err(EC2Error::new("Failed to create instance")); } let instance_id = run_instances.instances()[0].instance_id().unwrap(); let response = self .client .create_tags() .resources(instance_id) .tags( Tag::builder() .key("Name") .value("From SDK Examples") .build(), ) .send() .await; match response { Ok(_) => tracing::info!("Created {instance_id} and applied tags."), Err(err) => { tracing::info!("Error applying tags to {instance_id}: {err:?}"); return Err(err.into()); } } tracing::info!("Instance is created."); Ok(instance_id.to_string()) }
-
For API details, see CreateTags
in Amazon SDK for Rust API 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.