I’ll go over a useful way to summarize CloudFormation templates resources with jq
. This is useful when you are looking at a CloudFormation template and trying to understand it. We’ll download an example CloudFormation template and use run through some useful jq
commands summarize the resources defined in a CloudFormation template. Note, I’ll only show the output that is useful for understanding.
First, download a sample template. We’ll download a typical Auto Scaling template:
wget https://s3.amazonaws.com/cloudformation-templates-us-east-1/AutoScalingMultiAZWithNotifications.template
mv AutoScalingMultiAZWithNotifications.template autoscaling.json
Colorize the output with jq
:
cat autoscaling.json | jq '.'
Summarize top-level sections:
$ cat autoscaling.json | jq 'keys[]'
"AWSTemplateFormatVersion"
"Description"
"Mappings"
"Outputs"
"Parameters"
"Resources"
Show the leaf paths of the template structure:
$ cat autoscaling.json | jq --compact-output '. | leaf_paths' | sed -e 's/,[0-9][0-9]*,/,1000,/g' | sed -e 's/,[0-9][0-9]*\]/,1000\]/g' | sort -u
...
["Resources","WebServerScaleDownPolicy","Properties","AdjustmentType"]
["Resources","WebServerScaleDownPolicy","Properties","AutoScalingGroupName","Ref"]
["Resources","WebServerScaleDownPolicy","Properties","Cooldown"]
["Resources","WebServerScaleDownPolicy","Properties","ScalingAdjustment"]
["Resources","WebServerScaleDownPolicy","Type"]
["Resources","WebServerScaleUpPolicy","Properties","AdjustmentType"]
["Resources","WebServerScaleUpPolicy","Properties","AutoScalingGroupName","Ref"]
["Resources","WebServerScaleUpPolicy","Properties","Cooldown"]
["Resources","WebServerScaleUpPolicy","Properties","ScalingAdjustment"]
["Resources","WebServerScaleUpPolicy","Type"]
Check out the Resources only:
$ cat autoscaling.json | jq '.Resources'
{
"NotificationTopic": {
"Type": "AWS::SNS::Topic",
"Properties": {
"Subscription": [
{
"Endpoint": {
"Ref": "OperatorEMail"
},
"Protocol": "email"
...
This still a lot of information at this point. Also, notice that the structure of the Resources section has a key that varies. This is called the Logical Id. This structure makes it harder to go down and inspect it further. But jq
has a useful to_entries
command that we can use to restructure the format to make it easier to traverse.
cat autoscaling.json | jq '.Resources | to_entries'
[
{
"key": "NotificationTopic",
"value": {
"Type": "AWS::SNS::Topic",
"Properties": {
"Subscription": [
{
"Endpoint": {
"Ref": "OperatorEMail"
...
The to_entries
methods changed the structure so that there is a consistent key and value path for each resource in the structure now. We can now list the resources in the CloudFormation template.
$ cat autoscaling.json | jq '.Resources | to_entries | .[].value.Type'
"AWS::SNS::Topic"
"AWS::AutoScaling::AutoScalingGroup"
"AWS::AutoScaling::LaunchConfiguration"
"AWS::AutoScaling::ScalingPolicy"
"AWS::AutoScaling::ScalingPolicy"
"AWS::CloudWatch::Alarm"
"AWS::CloudWatch::Alarm"
"AWS::ElasticLoadBalancingV2::LoadBalancer"
"AWS::ElasticLoadBalancingV2::Listener"
"AWS::ElasticLoadBalancingV2::TargetGroup"
"AWS::EC2::SecurityGroup"
Finally, I like to sort the output and count them so we get a nice summary:
$ cat autoscaling.json | jq -r '.Resources | to_entries | .[].value.Type' | sort | uniq -c | sort -r
2 AWS::CloudWatch::Alarm
2 AWS::AutoScaling::ScalingPolicy
1 AWS::SNS::Topic
1 AWS::ElasticLoadBalancingV2::TargetGroup
1 AWS::ElasticLoadBalancingV2::LoadBalancer
1 AWS::ElasticLoadBalancingV2::Listener
1 AWS::EC2::SecurityGroup
1 AWS::AutoScaling::LaunchConfiguration
1 AWS::AutoScaling::AutoScalingGroup
Hope this helps! 🎉 This is also even shorter and sweeter with lono inspect summary.
Related Posts: