Resource Groups Tagging API examples using AWS CLI - AWS SDK Code Examples

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

Resource Groups Tagging API examples using AWS CLI

The following code examples show you how to perform actions and implement common scenarios by using the AWS 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.

Each example includes a link to the complete source code, 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.

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

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

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

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

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

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

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.

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

AWS 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.