3

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:

mounika
  • 75
  • 9
  • I tried 'Values':['Jenkins'] and ended up getting this error message: Invalid type for parameter ResourceQuery.Query, value: [{'Key': 'name', 'Value': 'Jenkins'}], type: , valid types: . Based on this message looks like It is expecting a string whereas we are trying to pass a list. Tried just passing the dictionary and got similar message except for this time it said it is expecting not a dictionary. Not sure how to pass a key: value pairs as string – mounika Aug 20 '19 at 00:17
  • After a quick read of the boto3 docs, the `Query` parameter is actually expected to be a JSON string representing the collection of tag filters rather than a native Python collection of tag filters. – jarmod Aug 20 '19 at 00:49
  • Correction: the Query parameter is actually expected to be a JSON string representing a GetResources query (see https://docs.aws.amazon.com/resourcegroupstagging/latest/APIReference/API_GetResources.html). – jarmod Aug 23 '19 at 16:39

1 Answers1

3

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 :)

zenum
  • 46
  • 1