There are more AWS SDK examples available in the AWS Doc SDK Examples
Resource Groups 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.
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 create-group.
- AWS CLI
-
Example 1: To create a tag-based resource group
The following
create-groupexample creates a tag-based resource group of Amazon EC2 instances in the current region. It's based on a query for resources that are tagged with the keyName, and the valueWebServers. The group name istbq-WebServer. The query is in a separate JSON file that is passed to the command.aws resource-groups create-group \ --nametbq-WebServer\ --resource-queryfile://query.jsonContents of
query.json:{ "Type": "TAG_FILTERS_1_0", "Query": "{\"ResourceTypeFilters\":[\"AWS::EC2::Instance\"],\"TagFilters\":[{\"Key\":\"Name\", \"Values\":[\"WebServers\"]}]}" }Output:
{ "Group": { "GroupArn": "arn:aws:resource-groups:us-west-2:123456789012:group/tbq-WebServer", "Name": "tbq-WebServer" }, "ResourceQuery": { "Type": "TAG_FILTERS_1_0", "Query": "{\"ResourceTypeFilters\":[\"AWS::EC2::Instance\"],\"TagFilters\":[{\"Key\":\"Name\", \"Values\":[\"WebServers\"]}]}" } }Example 2: To create a CloudFormation stack-based resource group
The following
create-groupexample creates an AWS CloudFormation stack-based resource group namedsampleCFNstackgroup. The query includes all resources in the specified CloudFormation stack that are supported by AWS Resource Groups.aws resource-groups create-group \ --namecbq-CFNstackgroup\ --resource-queryfile://query.jsonContents of
query.json:{ "Type": "CLOUDFORMATION_STACK_1_0", "Query": "{\"ResourceTypeFilters\":[\"AWS::AllSupported\"],\"StackIdentifier\":\"arn:aws:cloudformation:us-west-2:123456789012:stack/MyCFNStack/1415z9z0-z39z-11z8-97z5-500z212zz6fz\"}" }Output:
{ "Group": { "GroupArn": "arn:aws:resource-groups:us-west-2:123456789012:group/cbq-CFNstackgroup", "Name": "cbq-CFNstackgroup" }, "ResourceQuery": { "Type": "CLOUDFORMATION_STACK_1_0", "Query": "{\"ResourceTypeFilters\":[\"AWS::AllSupported\"],\"StackIdentifier\":\"arn:aws:cloudformation:us-east-2:123456789012:stack/MyCFNStack/1415z9z0-z39z-11z8-97z5-500z212zz6fz\"}"}' } }For more information, see Create Groups in the AWS Resource Groups User Guide.
-
For API details, see CreateGroup
in AWS CLI Command Reference.
-
The following code example shows how to use delete-group.
- AWS CLI
-
To update the description for a resource group
The following
delete-groupexample updates the specified resource group.aws resource-groups delete-group \ --group-nametbq-WebServerOutput:
{ "Group": { "GroupArn": "arn:aws:resource-groups:us-west-2:1234567890:group/tbq-WebServer", "Name": "tbq-WebServer" } }For more information, see Delete Groups in the AWS Resource Groups User Guide.
-
For API details, see DeleteGroup
in AWS CLI Command Reference.
-
The following code example shows how to use get-group-query.
- AWS CLI
-
To get the query attached to a resource group
The following
get-group-queryexample displays query attached to the specified resource group.aws resource-groups get-group-query \ --group-nametbq-WebServerOutput:
{ "GroupQuery": { "GroupName": "tbq-WebServer", "ResourceQuery": { "Type": "TAG_FILTERS_1_0", "Query": "{\"ResourceTypeFilters\":[\"AWS::EC2::Instance\"],\"TagFilters\":[{\"Key\":\"Name\", \"Values\":[\"WebServers\"]}]}" } } }-
For API details, see GetGroupQuery
in AWS CLI Command Reference.
-
The following code example shows how to use get-group.
- AWS CLI
-
To get information about a resource group
The following
get-groupexample displays details about the specified resource group. To get the query attached to the group, useget-group-query.aws resource-groups get-group \ --group-nametbq-WebServerOutput:
{ "Group": { "GroupArn": "arn:aws:resource-groups:us-west-2:123456789012:group/tbq-WebServer", "Name": "tbq-WebServer", "Description": "A tag-based query resource group of WebServers." } }-
For API details, see GetGroup
in AWS CLI Command Reference.
-
The following code example shows how to use get-tags.
- AWS CLI
-
To retrieve the tags attached to a resource group
The following
get-tagsexample displays the tag key and value pairs attached to the specified resource group (the group itself, not its members).aws resource-groups get-tags \ --arnarn:aws:resource-groups:us-west-2:123456789012:group/tbq-WebServerOutput:
{ "Arn": "arn:aws:resource-groups:us-west-2:123456789012:group/tbq-WebServer", "Tags": { "QueryType": "tags", "QueryResources": "ec2-instances" } }-
For API details, see GetTags
in AWS CLI Command Reference.
-
The following code example shows how to use list-group-resources.
- AWS CLI
-
To list all of the resources in a resource group
Example 1: The following
list-resource-groupsexample lists all of the resources that are part of the specified resource group.aws resource-groups list-group-resources \ --group-nametbq-WebServerOutput:
{ "ResourceIdentifiers": [ { "ResourceArn": "arn:aws:ec2:us-west-2:123456789012:instance/i-09f77fa38c12345ab", "ResourceType": "AWS::EC2::Instance" } ] }Example 2: The following example lists all of the resources in the group that also have a 'resource-type' of the 'AWS::EC2::Instance'. :
aws resource-groups list-group-resources --group-name tbq-WebServer --filters Name=resource-type,Values=AWS::EC2::Instance
-
For API details, see ListGroupResources
in AWS CLI Command Reference.
-
The following code example shows how to use list-groups.
- AWS CLI
-
To list the available resource groups
The following
list-groupsexample displays a list of all of the resource groups.aws resource-groups list-groupsOutput:
{ "GroupIdentifiers": [ { "GroupName": "tbq-WebServer", "GroupArn": "arn:aws:resource-groups:us-west-2:123456789012:group/tbq-WebServer3" }, { "GroupName": "cbq-CFNStackQuery", "GroupArn": "arn:aws:resource-groups:us-west-2:123456789012:group/cbq-CFNStackQuery" } ], "Groups": [ { "GroupArn": "arn:aws:resource-groups:us-west-2:123456789012:group/tbq-WebServer", "Name": "tbq-WebServer" }, { "GroupArn": "arn:aws:resource-groups:us-west-2:123456789012:group/cbq-CFNStackQuery", "Name": "cbq-CFNStackQuery" } ] }-
For API details, see ListGroups
in AWS CLI Command Reference.
-
The following code example shows how to use list-resource-groups.
- AWS CLI
-
To list all of the resources in a resource group
The following
list-resource-groupsexample lists all of the resources that are part of the specified resource group.aws resource-groups list-group-resources \ --group-nametbq-WebServerOutput:
{ "ResourceIdentifiers": [ { "ResourceArn": "arn:aws:ec2:us-west-2:123456789012:instance/i-09f77fa38c12345ab", "ResourceType": "AWS::EC2::Instance" } ] }-
For API details, see ListResourceGroups
in AWS CLI Command Reference.
-
The following code example shows how to use put-group-configuration.
- AWS CLI
-
To attach a service configuration to a resource group
Example 1: The following
put-group-configurationexample specifies that the resource group is to contain only Amazon EC2 capacity reservations for instances in theC5orM5families.aws resource-groups put-group-configuration \ --groupMyTestGroup\ --configurationfile://config.jsonContents of
config.json:[ { "Type": "AWS::EC2::HostManagement", "Parameters": [ { "Name": "allowed-host-families", "Values": [ "c5", "m5" ] }, { "Name": "any-host-based-license-configuration", "Values": [ "true" ] } ] }, { "Type": "AWS::ResourceGroups::Generic", "Parameters": [ { "Name": "allowed-resource-types", "Values": [ "AWS::EC2::Host" ] }, { "Name": "deletion-protection", "Values": [ "UNLESS_EMPTY" ] } ] } ]This command produces no output if successful.
For more information, see Service configurations for resource groups in the Resource Groups API Reference Guide.
-
For API details, see PutGroupConfiguration
in AWS CLI Command Reference.
-
The following code example shows how to use search-resources.
- AWS CLI
-
To find resources that match a query
The following
search-resourcesexample retrieves a list of all AWS resources that match the specified query.aws resource-groups search-resources \ --resource-queryfile://query.jsonContents of
query.json:{ "Type": "TAG_FILTERS_1_0", "Query": "{\"ResourceTypeFilters\":[\"AWS::EC2::Instance\"],\"TagFilters\":[{\"Key\":\"Patch Group\", \"Values\":[\"Dev\"]}]}" }Output:
{ "ResourceIdentifiers": [ { "ResourceArn": "arn:aws:ec2:us-west-2:123456789012:instance/i-01a23bc45d67890ef", "ResourceType": "AWS::EC2::Instance" } ] }-
For API details, see SearchResources
in AWS CLI Command Reference.
-
The following code example shows how to use tag.
- AWS CLI
-
To attach a tag to a resource group
The following
tagexample attaches the specified tag key and value pairs to the specified resource group (the group itself, not its members).aws resource-groups tag \ --tagsQueryType=tags,QueryResources=ec2-instances\ --arnarn:aws:resource-groups:us-west-2:128716708097:group/tbq-WebServerOutput:
{ "Arn": "arn:aws:resource-groups:us-west-2:128716708097:group/tbq-WebServer", "Tags": { "QueryType": "tags", "QueryResources": "ec2-instances" } }For more information, see Manage tags in the AWS Resource Groups User Guide.
-
For API details, see Tag
in AWS CLI Command Reference.
-
The following code example shows how to use untag.
- AWS CLI
-
To remove tags from a resource group
The following
untagsexample removes any tag with the specified key from the resource group itself, not its members.aws resource-groups untag \ --arnarn:aws:resource-groups:us-west-2:123456789012:group/tbq-WebServer\ --keysQueryTypeOutput:
{ "Arn": "arn:aws:resource-groups:us-west-2:123456789012:group/tbq-WebServer", "Keys": [ "QueryType" ] }For more information, see Manage tags in the AWS Resource Groups User Guide.
-
For API details, see Untag
in AWS CLI Command Reference.
-
The following code example shows how to use update-group-query.
- AWS CLI
-
Example 1: To update the query for a tag-based resource group
The following
update-group-queryexample updates the query attached to the specified tag-based resource group.aws resource-groups update-group-query \ --group-nametbq-WebServer\ --resource-query '{"Type":"TAG_FILTERS_1_0", "Query":"{\"ResourceTypeFilters\":[\"AWS::EC2::Instance\"],\"TagFilters\":[{\"Key\":\"Name\", \"Values\":[\"WebServers\"]}]}"}'Output:
{ "Group": { "GroupArn": "arn:aws:resource-groups:us-east-2:123456789012:group/tbq-WebServer", "Name": "tbq-WebServer" }, "ResourceQuery": { "Type": "TAG_FILTERS_1_0", "Query": "{\"ResourceTypeFilters\":[\"AWS::EC2::Instance\"],\"TagFilters\":[{\"Key\":\"Name\", \"Values\":[\"WebServers\"]}]}" } }For more information, see Update Groups in the AWS Resource Groups User Guide.
Example 2: To update the query for a CloudFormation stack-based resource group
The following
update-group-queryexample updates the query attached to the specified AWS CloudFormation stack-based resource group.aws resource-groups update-group-query \ --group-namecbq-CFNstackgroup\ --resource-query '{"Type": "CLOUDFORMATION_STACK_1_0", "Query": "{\"ResourceTypeFilters\":[\"AWS::AllSupported\"],\"StackIdentifier\":\"arn:aws:cloudformation:us-west-2:123456789012:stack/MyCFNStack/1415z9z0-z39z-11z8-97z5-500z212zz6fz\"}"}'Output:
{ "Group": { "GroupArn": "arn:aws:resource-groups:us-west-2:123456789012:group/cbq-CFNstackgroup", "Name": "cbq-CFNstackgroup" }, "ResourceQuery": { "Type": "CLOUDFORMATION_STACK_1_0", "Query": "{\"ResourceTypeFilters\":[\"AWS::AllSupported\"],\"StackIdentifier\":\"arn:aws:cloudformation:us-west-2:123456789012:stack/MyCFNStack/1415z9z0-z39z-11z8-97z5-500z212zz6fz\"}" } }For more information, see Update Groups in the AWS Resource Groups User Guide.
-
For API details, see UpdateGroupQuery
in AWS CLI Command Reference.
-
The following code example shows how to use update-group.
- AWS CLI
-
To update the description for a resource group
The following
update-groupexample updates the description for the specified resource group.aws resource-groups update-group \ --group-nametbq-WebServer\ --description"Resource group for all web server resources."Output:
{ "Group": { "GroupArn": "arn:aws:resource-groups:us-west-2:123456789012:group/tbq-WebServer", "Name": "tbq-WebServer" "Description": "Resource group for all web server resources." } }For more information, see Update Groups in the AWS Resource Groups User Guide.
-
For API details, see UpdateGroup
in AWS CLI Command Reference.
-