This documentation is for Version 1 of the Amazon CLI only.
We announced the upcoming end-of-support for the Amazon CLI version 1. We recommend that you migrate to the Amazon CLI version 2. For dates, additional details, and information on how to migrate, see the announcement
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.
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.
- Amazon CLI
-
To get a list of tagged resources
The following
get-resourcesexample displays a list of resources in the account that are tagged with the specified key name and value.aws resourcegroupstaggingapi get-resources \ --tag-filtersKey=Environment,Values=Production\ --tags-per-page100Output:
{ "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-keysexample retrieves the list of all tag key names used by resources in the account.aws resourcegroupstaggingapi get-tag-keysOutput:
{ "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-valuesexample displays all of the values used for the specified key for all resources in theaws resourcegroupstaggingapi get-tag-values \ --key=EnvironmentOutput:
{ "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-resourcesexample tags the specified resource with a key name and value.aws resourcegroupstaggingapi tag-resources \ --resource-arn-listarn:aws:s3:::MyProductionBucket\ --tagsEnvironment=Production,CostCenter=1234Output:
{ "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-resourcesexample removes the specified tag keys and any associated values from the specified resource.aws resourcegroupstaggingapi untag-resources \ --resource-arn-listarn:aws:s3:::amzn-s3-demo-bucket\ --tag-keysEnvironmentCostCenterOutput:
{ "FailedResourcesMap": {} }For more information, see UntagResources
in the Resource Groups Tagging API Reference. -
For API details, see UntagResources
in Amazon CLI Command Reference.
-