Skip to content

How to build a tag query-based resourcegroup using Cloudformation?

0

I am trying to create a resource group of EC2 instances using cloudformation, but I keep getting this error message: Resource handler returned message: "Model validation failed (#/ResourceQuery/Query: expected type: JSONObject, found: String)" (RequestToken: aaa-bbb-ccc, HandlerErrorCode: InvalidRequest) When I create the resource group using the AWS console and then ask for the query using CLI command <aws resource-groups get-group-query>, I get exactly the same syntax.

What is wrong with the below json template?

{ "Resources": { "rgrname": { "Type": "AWS::ResourceGroups::Group", "Properties": { "Description": "Resource group containing all ec2 instances with name tag prod", "Name": "rgr_prod_servers", "ResourceQuery": { "Type": "TAG_FILTERS_1_0", "Query": "{"ResourceTypeFilters":["AWS::EC2::Instance"],"TagFilters":[{"Key":"Name","Values":["prod"]}]}" } } } } }

asked 10 months ago79 views
1 Answer
1
Accepted Answer

The issue with your CloudFormation template is in the Query property of the ResourceQuery. Instead of providing a JSON string, you need to provide a JSON object directly. Here's the corrected version of your template:

{ "Resources": { "rgrname": { "Type": "AWS::ResourceGroups::Group", "Properties": { "Description": "Resource group containing all ec2 instances with name tag prod", "Name": "rgr_prod_servers", "ResourceQuery": { "Type": "TAG_FILTERS_1_0", "Query": { "ResourceTypeFilters": ["AWS::EC2::Instance"], "TagFilters": [ { "Key": "Name", "Values": ["prod"] } ] } } } } } }

The key difference is that the Query property is now a JSON object instead of a JSON-encoded string. This matches the expected format for the CloudFormation template.

The reason you're seeing a different format when using the AWS CLI is that the CLI often requires JSON to be passed as a string, which is why it appears encoded. However, in CloudFormation templates, you should provide the actual JSON structure directly.

This corrected template should resolve the "Model validation failed" error you were encountering. It creates a resource group that includes all EC2 instances tagged with the key "Name" and value "prod".
Sources
AWS::ResourceGroups::Group - AWS CloudFormation
Build a tag-based query and create a group - AWS Resource Groups

answered 10 months ago
EXPERT
reviewed 10 months ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.