Search code examples
aws-sdkboto3aws-resource-group

creating AWS resource group using boto3


I would like to create AWS resource group using boto3. In the resource group I would like to add ec2 instances having tags "name":"Jenkins".Below is the syntax suggested in boto3 documentation.

response = client.create_group(
    Name='string',
    Description='string',
    ResourceQuery={
        'Type': 'TAG_FILTERS_1_0'|'CLOUDFORMATION_STACK_1_0',
        'Query': 'string'
    },
    Tags={
        'string': 'string'
    }
)

I read the documentation but I did not understand what query is in my case and couldn't find any example of creating resource groups using boto3 online. In the ResourceQuery dictionary, I can use 'Type' as 'TAG_FILTERS_1_0' but not sure what 'Query' would be. It would be great if I can get an example explanation of creating a resource group. Thank you

Update After following @Jarmod suggestion, I tried the following code

client = boto3.client('resource-groups', **conn_args)
    response = client.create_group(
        Name='JenkinsResource',
        Description='JenkinsResourceGrp',
        ResourceQuery={
            'Type': 'TAG_FILTERS_1_0',
            'Query': [{"Key": "name", "Values": "Jenkins"}]
        }

    )

I ended up with the following error.

Invalid type for parameter ResourceQuery.Query, value: [{'Key': 'name', 'Values': 'Jenkins'}], type: , valid types:


Solution

  • I was able to get it to work with the Query object being:

    {
        'ResourceTypeFilters': ['AWS::AllSupported'],
        'TagFilters': [{
            'Values': ['Jenkins'],
            'Key': 'name'
        }]
    }
    

    And then as it's expecting a string and not a json object I did a json.dumps(query).

    I discovered this by generating it through the web console and then having a look at the CloudTrail logs to see what the console did :)