

There are more AWS SDK examples available in the [AWS Doc SDK Examples](https://github.com/awsdocs/aws-doc-sdk-examples) GitHub repo.

# Code examples for Resource Groups Tagging API using AWS SDKs
<a name="resource-groups-tagging-api_code_examples"></a>

The following code examples show you how to use AWS Resource Groups Tagging API with an AWS software development kit (SDK).

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

**More resources**
+  **[ Resource Groups Tagging API User Guide](https://docs.aws.amazon.com/tag-editor/latest/userguide/tagging.html)** – More information about Resource Groups Tagging API.
+ **[Resource Groups Tagging API API Reference](https://docs.aws.amazon.com/resourcegroupstagging/latest/APIReference/overview.html)** – Details about all available Resource Groups Tagging API actions.
+ **[AWS Developer Center](https://aws.amazon.com/developer/code-examples/?awsf.sdk-code-examples-product=product%23)** – Code examples that you can filter by category or full-text search.
+ **[AWS SDK Examples](https://github.com/awsdocs/aws-doc-sdk-examples)** – GitHub repo with complete code in preferred languages. Includes instructions for setting up and running the code.

**Contents**
+ [Basics](resource-groups-tagging-api_code_examples_basics.md)
  + [Actions](resource-groups-tagging-api_code_examples_actions.md)
    + [`GetResources`](resource-groups-tagging-api_example_resource-groups-tagging-api_GetResources_section.md)
    + [`GetTagKeys`](resource-groups-tagging-api_example_resource-groups-tagging-api_GetTagKeys_section.md)
    + [`GetTagValues`](resource-groups-tagging-api_example_resource-groups-tagging-api_GetTagValues_section.md)
    + [`TagResources`](resource-groups-tagging-api_example_resource-groups-tagging-api_TagResources_section.md)
    + [`UntagResources`](resource-groups-tagging-api_example_resource-groups-tagging-api_UntagResources_section.md)

# Basic examples for Resource Groups Tagging API using AWS SDKs
<a name="resource-groups-tagging-api_code_examples_basics"></a>

The following code examples show how to use the basics of AWS Resource Groups Tagging API with AWS SDKs. 

**Contents**
+ [Actions](resource-groups-tagging-api_code_examples_actions.md)
  + [`GetResources`](resource-groups-tagging-api_example_resource-groups-tagging-api_GetResources_section.md)
  + [`GetTagKeys`](resource-groups-tagging-api_example_resource-groups-tagging-api_GetTagKeys_section.md)
  + [`GetTagValues`](resource-groups-tagging-api_example_resource-groups-tagging-api_GetTagValues_section.md)
  + [`TagResources`](resource-groups-tagging-api_example_resource-groups-tagging-api_TagResources_section.md)
  + [`UntagResources`](resource-groups-tagging-api_example_resource-groups-tagging-api_UntagResources_section.md)

# Actions for Resource Groups Tagging API using AWS SDKs
<a name="resource-groups-tagging-api_code_examples_actions"></a>

The following code examples demonstrate how to perform individual Resource Groups Tagging API actions with AWS SDKs. Each example includes a link to GitHub, where you can find instructions for setting up and running the code. 

 The following examples include only the most commonly used actions. For a complete list, see the [AWS Resource Groups Tagging API API Reference](https://docs.aws.amazon.com/resourcegroupstagging/latest/APIReference/overview.html). 

**Topics**
+ [`GetResources`](resource-groups-tagging-api_example_resource-groups-tagging-api_GetResources_section.md)
+ [`GetTagKeys`](resource-groups-tagging-api_example_resource-groups-tagging-api_GetTagKeys_section.md)
+ [`GetTagValues`](resource-groups-tagging-api_example_resource-groups-tagging-api_GetTagValues_section.md)
+ [`TagResources`](resource-groups-tagging-api_example_resource-groups-tagging-api_TagResources_section.md)
+ [`UntagResources`](resource-groups-tagging-api_example_resource-groups-tagging-api_UntagResources_section.md)

# Use `GetResources` with a CLI
<a name="resource-groups-tagging-api_example_resource-groups-tagging-api_GetResources_section"></a>

The following code examples show how to use `GetResources`.

------
#### [ CLI ]

**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](https://docs.aws.amazon.com/resourcegroupstagging/latest/APIReference/API_GetResources.html) in the *Resource Groups Tagging API Reference*.  
+  For API details, see [GetResources](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/resourcegroupstaggingapi/get-resources.html) in *AWS CLI Command Reference*. 

------
#### [ PowerShell ]

**Tools for PowerShell V4**  
**Example 1: Returns all the tagged resources in a region and the tag keys associated with the resource. If no -Region parameter is supplied to the cmdlet it will attempt to infer region from the shell or EC2 instance metadata.**  

```
Get-RGTResource
```
**Output:**  

```
ResourceARN                                                      Tags          
-----------                                                      ----            
arn:aws:dynamodb:us-west-2:123456789012:table/mytable            {stage, version}   
arn:aws:s3:::amzn-s3-demo-bucket                                            {stage, version, othertag}
```
**Example 2: Returns all the tagged resources of the specified type in a region. The string for each service name and resource type is the same as that embedded in a resource's Amazon Resource Name (ARN).**  

```
Get-RGTResource -ResourceType "s3"
```
**Output:**  

```
ResourceARN                                                      Tags          
-----------                                                      ----            
arn:aws:s3:::amzn-s3-demo-bucket                                            {stage, version, othertag}
```
**Example 3: Returns all the tagged resources of the specified type in a region. Note that when the resource types are piped into the cmdlet, one call to the service is made for each supplied resource type.**  

```
"dynamodb","s3" | Get-RGTResource
```
**Output:**  

```
ResourceARN                                                      Tags          
-----------                                                      ----            
arn:aws:dynamodb:us-west-2:123456789012:table/mytable            {stage, version}   
arn:aws:s3:::amzn-s3-demo-bucket                                            {stage, version, othertag}
```
**Example 4: Returns all the tagged resources that match the specified filter.**  

```
Get-RGTResource -TagFilter @{ Key="stage" }
```
**Output:**  

```
ResourceARN                                                      Tags          
-----------                                                      ----            
arn:aws:s3:::amzn-s3-demo-bucket                                            {stage, version, othertag}
```
**Example 5: Returns all the tagged resources that match the specified filter and resource type.**  

```
Get-RGTResource -TagFilter @{ Key="stage" } -ResourceType "dynamodb"
```
**Output:**  

```
ResourceARN                                                      Tags          
-----------                                                      ----            
arn:aws:dynamodb:us-west-2:123456789012:table/mytable            {stage, version}
```
**Example 6: Returns all the tagged resources that match the specified filter.**  

```
Get-RGTResource -TagFilter @{ Key="stage"; Values=@("beta","gamma") }
```
**Output:**  

```
ResourceARN                                                      Tags          
-----------                                                      ----            
arn:aws:dynamodb:us-west-2:123456789012:table/mytable            {stage, version}
```
+  For API details, see [GetResources](https://docs.aws.amazon.com/powershell/v4/reference) in *AWS Tools for PowerShell Cmdlet Reference (V4)*. 

**Tools for PowerShell V5**  
**Example 1: Returns all the tagged resources in a region and the tag keys associated with the resource. If no -Region parameter is supplied to the cmdlet it will attempt to infer region from the shell or EC2 instance metadata.**  

```
Get-RGTResource
```
**Output:**  

```
ResourceARN                                                      Tags          
-----------                                                      ----            
arn:aws:dynamodb:us-west-2:123456789012:table/mytable            {stage, version}   
arn:aws:s3:::amzn-s3-demo-bucket                                            {stage, version, othertag}
```
**Example 2: Returns all the tagged resources of the specified type in a region. The string for each service name and resource type is the same as that embedded in a resource's Amazon Resource Name (ARN).**  

```
Get-RGTResource -ResourceType "s3"
```
**Output:**  

```
ResourceARN                                                      Tags          
-----------                                                      ----            
arn:aws:s3:::amzn-s3-demo-bucket                                            {stage, version, othertag}
```
**Example 3: Returns all the tagged resources of the specified type in a region. Note that when the resource types are piped into the cmdlet, one call to the service is made for each supplied resource type.**  

```
"dynamodb","s3" | Get-RGTResource
```
**Output:**  

```
ResourceARN                                                      Tags          
-----------                                                      ----            
arn:aws:dynamodb:us-west-2:123456789012:table/mytable            {stage, version}   
arn:aws:s3:::amzn-s3-demo-bucket                                            {stage, version, othertag}
```
**Example 4: Returns all the tagged resources that match the specified filter.**  

```
Get-RGTResource -TagFilter @{ Key="stage" }
```
**Output:**  

```
ResourceARN                                                      Tags          
-----------                                                      ----            
arn:aws:s3:::amzn-s3-demo-bucket                                            {stage, version, othertag}
```
**Example 5: Returns all the tagged resources that match the specified filter and resource type.**  

```
Get-RGTResource -TagFilter @{ Key="stage" } -ResourceType "dynamodb"
```
**Output:**  

```
ResourceARN                                                      Tags          
-----------                                                      ----            
arn:aws:dynamodb:us-west-2:123456789012:table/mytable            {stage, version}
```
**Example 6: Returns all the tagged resources that match the specified filter.**  

```
Get-RGTResource -TagFilter @{ Key="stage"; Values=@("beta","gamma") }
```
**Output:**  

```
ResourceARN                                                      Tags          
-----------                                                      ----            
arn:aws:dynamodb:us-west-2:123456789012:table/mytable            {stage, version}
```
+  For API details, see [GetResources](https://docs.aws.amazon.com/powershell/v5/reference) in *AWS Tools for PowerShell Cmdlet Reference (V5)*. 

------

# Use `GetTagKeys` with a CLI
<a name="resource-groups-tagging-api_example_resource-groups-tagging-api_GetTagKeys_section"></a>

The following code examples show how to use `GetTagKeys`.

------
#### [ CLI ]

**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](https://docs.aws.amazon.com/resourcegroupstagging/latest/APIReference/API_GetTagKeys.html) in the *Resource Groups Tagging API Reference*.  
+  For API details, see [GetTagKeys](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/resourcegroupstaggingapi/get-tag-keys.html) in *AWS CLI Command Reference*. 

------
#### [ PowerShell ]

**Tools for PowerShell V4**  
**Example 1: Returns all tag keys in the specified region. If the -Region parameter is not specified the cmdlet will attempt to infer the region from the default shell region or EC2 instance metadata. Note that the tag keys are not returned in any specific order.**  

```
Get-RGTTagKey -region us-west-2
```
**Output:**  

```
version
stage
```
+  For API details, see [GetTagKeys](https://docs.aws.amazon.com/powershell/v4/reference) in *AWS Tools for PowerShell Cmdlet Reference (V4)*. 

**Tools for PowerShell V5**  
**Example 1: Returns all tag keys in the specified region. If the -Region parameter is not specified the cmdlet will attempt to infer the region from the default shell region or EC2 instance metadata. Note that the tag keys are not returned in any specific order.**  

```
Get-RGTTagKey -region us-west-2
```
**Output:**  

```
version
stage
```
+  For API details, see [GetTagKeys](https://docs.aws.amazon.com/powershell/v5/reference) in *AWS Tools for PowerShell Cmdlet Reference (V5)*. 

------

# Use `GetTagValues` with a CLI
<a name="resource-groups-tagging-api_example_resource-groups-tagging-api_GetTagValues_section"></a>

The following code examples show how to use `GetTagValues`.

------
#### [ CLI ]

**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](https://docs.aws.amazon.com/resourcegroupstagging/latest/APIReference/API_GetTagValues.html) in the *Resource Groups Tagging API Reference*.  
+  For API details, see [GetTagValues](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/resourcegroupstaggingapi/get-tag-values.html) in *AWS CLI Command Reference*. 

------
#### [ PowerShell ]

**Tools for PowerShell V4**  
**Example 1: Returns the value for the specified tag in a region. If the -Region parameter is not specified the cmdlet will attempt to infer the region from the default shell region or EC2 instance metadata.**  

```
Get-RGTTagValue -Key "stage" -Region us-west-2
```
**Output:**  

```
beta
```
+  For API details, see [GetTagValues](https://docs.aws.amazon.com/powershell/v4/reference) in *AWS Tools for PowerShell Cmdlet Reference (V4)*. 

**Tools for PowerShell V5**  
**Example 1: Returns the value for the specified tag in a region. If the -Region parameter is not specified the cmdlet will attempt to infer the region from the default shell region or EC2 instance metadata.**  

```
Get-RGTTagValue -Key "stage" -Region us-west-2
```
**Output:**  

```
beta
```
+  For API details, see [GetTagValues](https://docs.aws.amazon.com/powershell/v5/reference) in *AWS Tools for PowerShell Cmdlet Reference (V5)*. 

------

# Use `TagResources` with a CLI
<a name="resource-groups-tagging-api_example_resource-groups-tagging-api_TagResources_section"></a>

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](https://docs.aws.amazon.com/resourcegroupstagging/latest/APIReference/API_TagResources.html) in the *Resource Groups Tagging API Reference*.  
+  For API details, see [TagResources](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/resourcegroupstaggingapi/tag-resources.html) in *AWS CLI Command Reference*. 

------
#### [ PowerShell ]

**Tools for PowerShell V4**  
**Example 1: This example adds the tag keys "stage" and "version" with values "beta" and "preprod\$1test" 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](https://docs.aws.amazon.com/powershell/v4/reference) in *AWS Tools for PowerShell Cmdlet Reference (V4)*. 

**Tools for PowerShell V5**  
**Example 1: This example adds the tag keys "stage" and "version" with values "beta" and "preprod\$1test" 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](https://docs.aws.amazon.com/powershell/v5/reference) in *AWS Tools for PowerShell Cmdlet Reference (V5)*. 

------

# Use `UntagResources` with a CLI
<a name="resource-groups-tagging-api_example_resource-groups-tagging-api_UntagResources_section"></a>

The following code examples show how to use `UntagResources`.

------
#### [ CLI ]

**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:::amzn-s3-demo-bucket \
    --tag-keys Environment CostCenter
```
Output:  

```
{
    "FailedResourcesMap": {}
}
```
For more information, see [UntagResources](https://docs.aws.amazon.com/resourcegroupstagging/latest/APIReference/API_UntagResources.html) in the *Resource Groups Tagging API Reference*.  
+  For API details, see [UntagResources](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/resourcegroupstaggingapi/untag-resources.html) in *AWS CLI Command Reference*. 

------
#### [ PowerShell ]

**Tools for PowerShell V4**  
**Example 1: Removes the tag keys "stage" and "version", and the associated values, from an Amazon S3 bucket and an Amazon DynamoDB table. A single call is made to the service to remove the tags. Before the tags are removed the cmdlet will prompt for confirmation. To bypass confirmation add the -Force parameter.**  

```
$arn1 = "arn:aws:s3:::amzn-s3-demo-bucket"
$arn2 = "arn:aws:dynamodb:us-west-2:123456789012:table/mytable"

Remove-RGTResourceTag -ResourceARNList $arn1,$arn2 -TagKey "stage","version"
```
**Example 2: Removes the tag keys "stage" and "version", and the associated values, from 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. Before each call the cmdlet will prompt for confirmation. To bypass confirmation add the -Force parameter.**  

```
$arn1 = "arn:aws:s3:::amzn-s3-demo-bucket"
$arn2 = "arn:aws:dynamodb:us-west-2:123456789012:table/mytable"

$arn1,$arn2 | Remove-RGTResourceTag -TagKey "stage","version"
```
+  For API details, see [UntagResources](https://docs.aws.amazon.com/powershell/v4/reference) in *AWS Tools for PowerShell Cmdlet Reference (V4)*. 

**Tools for PowerShell V5**  
**Example 1: Removes the tag keys "stage" and "version", and the associated values, from an Amazon S3 bucket and an Amazon DynamoDB table. A single call is made to the service to remove the tags. Before the tags are removed the cmdlet will prompt for confirmation. To bypass confirmation add the -Force parameter.**  

```
$arn1 = "arn:aws:s3:::amzn-s3-demo-bucket"
$arn2 = "arn:aws:dynamodb:us-west-2:123456789012:table/mytable"

Remove-RGTResourceTag -ResourceARNList $arn1,$arn2 -TagKey "stage","version"
```
**Example 2: Removes the tag keys "stage" and "version", and the associated values, from 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. Before each call the cmdlet will prompt for confirmation. To bypass confirmation add the -Force parameter.**  

```
$arn1 = "arn:aws:s3:::amzn-s3-demo-bucket"
$arn2 = "arn:aws:dynamodb:us-west-2:123456789012:table/mytable"

$arn1,$arn2 | Remove-RGTResourceTag -TagKey "stage","version"
```
+  For API details, see [UntagResources](https://docs.aws.amazon.com/powershell/v5/reference) in *AWS Tools for PowerShell Cmdlet Reference (V5)*. 

------