Use TagResources with a CLI - AWS SDK Code Examples

There are more AWS SDK examples available in the AWS Doc SDK Examples GitHub repo.

Use TagResources with a CLI

The following code examples show how to use TagResources.

CLI
AWS CLI

To attach a tag to a resource

The following tag-resources example tags the specified resource with a key name and value.

aws resourcegroupstaggingapi tag-resources \ --resource-arn-list arn:aws:s3:::MyProductionBucket \ --tags Environment=Production,CostCenter=1234

Output:

{ "FailedResourcesMap": {} }

For more information, see TagResources in the Resource Groups Tagging API Reference.

  • For API details, see TagResources in AWS CLI Command Reference.

PowerShell
Tools for PowerShell

Example 1: This example adds the tag keys "stage" and "version" with values "beta" and "preprod_test" to an Amazon S3 bucket and an Amazon DynamoDB table. A single call is made to the service to apply the tags.

$arn1 = "arn:aws:s3:::amzn-s3-demo-bucket" $arn2 = "arn:aws:dynamodb:us-west-2:123456789012:table/mytable" Add-RGTResourceTag -ResourceARNList $arn1,$arn2 -Tag @{ "stage"="beta"; "version"="preprod_test" }

Example 2: This example adds the specified tags and values to an Amazon S3 bucket and an Amazon DynamoDB table. Two calls are made to the service, one for each resource ARN piped into the cmdlet.

$arn1 = "arn:aws:s3:::amzn-s3-demo-bucket" $arn2 = "arn:aws:dynamodb:us-west-2:123456789012:table/mytable" $arn1,$arn2 | Add-RGTResourceTag -Tag @{ "stage"="beta"; "version"="preprod_test" }
  • For API details, see TagResources in AWS Tools for PowerShell Cmdlet Reference.