Resource Groups Tagging API examples using Amazon CLI - Amazon Command Line Interface
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).

Resource Groups Tagging API examples using Amazon CLI

The following code examples show you how to perform actions and implement common scenarios by using the Amazon Command Line Interface with Resource Groups Tagging API.

Actions are code excerpts from larger programs and must be run in context. While actions show you how to call individual service functions, you can see actions in context in their related scenarios and cross-service examples.

Scenarios are code examples that show you how to accomplish a specific task by calling multiple functions within the same service.

Each example includes a link to GitHub, where you can find instructions on how to set up and run the code in context.

Topics

Actions

The following code example shows how to use get-resources.

Amazon CLI

To get a list of tagged resources

The following get-resources example displays a list of resources in the account that are tagged with the specified key name and value.

aws resourcegroupstaggingapi get-resources \ --tag-filters Key=Environment,Values=Production \ --tags-per-page 100

Output:

{ "ResourceTagMappingList": [ { "ResourceARN": " arn:aws:inspector:us-west-2:123456789012:target/0-nvgVhaxX/template/0-7sbz2Kz0", "Tags": [ { "Key": "Environment", "Value": "Production" } ] } ] }

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

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

The following code example shows how to use get-tag-keys.

Amazon CLI

To get a list of all tag keys

The following get-tag-keys example retrieves the list of all tag key names used by resources in the account.

aws resourcegroupstaggingapi get-tag-keys

Output:

{ "TagKeys": [ "Environment", "CostCenter", "Department" ] }

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

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

The following code example shows how to use get-tag-values.

Amazon CLI

To get a list of all tag values

The following get-tag-values example displays all of the values used for the specified key for all resources in the

aws resourcegroupstaggingapi get-tag-values \ --key=Environment

Output:

{ "TagValues": [ "Alpha", "Gamma", "Production" ] }

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

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

The following code example shows how to use tag-resources.

Amazon 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 Amazon CLI Command Reference.

The following code example shows how to use untag-resources.

Amazon CLI

To remove a tag from a resource

The following untag-resources example removes the specified tag keys and any associated values from the specified resource.

aws resourcegroupstaggingapi untag-resources \ --resource-arn-list arn:aws:s3:::awsexamplebucket \ --tag-keys Environment CostCenter

Output:

{ "FailedResourcesMap": {} }

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

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