Query CloudFormation Templates
Query AWS CloudFormation and SAM templates with cnquery
Rely on cnquery to query and analyze AWS CloudFormation templates and AWS SAM (Serverless Application Model) templates. Explore resources, parameters, outputs, and mappings within your infrastructure-as-code configurations.
Requirements
To analyze CloudFormation templates with cnquery, you must have:
- cnquery installed on your workstation.
- An AWS CloudFormation or SAM template file.
Query a CloudFormation template
To open a cnquery shell and query a CloudFormation template:
cnquery shell cloudformation PATH| For... | Substitute... |
|---|---|
| PATH | The path to the CloudFormation template file |
For example:
cnquery shell cloudformation ./templates/infrastructure.yamlOr to query a SAM template:
cnquery shell cloudformation ./sam/template.yamlExample queries
Once connected, you can query CloudFormation template resources.
This query retrieves all resources defined in the template:
cnquery> cloudformation.template.resources
cloudformation.template.resources: [
0: cloudformation.resource type="AWS::EC2::Instance"
1: cloudformation.resource type="AWS::S3::Bucket"
...
]This query retrieves all parameters:
cnquery> cloudformation.template.parameters
cloudformation.template.parameters: [
0: cloudformation.parameter name="InstanceType"
1: cloudformation.parameter name="Environment"
...
]This query retrieves outputs:
cnquery> cloudformation.template.outputs
cloudformation.template.outputs: [
0: cloudformation.output name="InstanceId"
...
]This query retrieves mappings:
cnquery> cloudformation.template.mappings
cloudformation.template.mappings: {
...
}Exit the cnquery shell
To exit the cnquery shell, either press Ctrl + D or type exit.
Learn more
-
To learn more about how the MQL query language works, read Write Effective MQL.
-
For a list of all the CloudFormation resources and fields you can query, read the CloudFormation Resource Pack Reference.