Summarize CloudFormation Resources with jq Tip
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:
Thanks for reading this far. If you found this article useful, I'd really appreciate it if you share this article so others can find it too! Thanks 😁 Also follow me on Twitter.
Got questions? Check out BoltOps.
You might also like
More tools:
-
Kubes
Kubes: Kubernetes Deployment Tool
Kubes is a Kubernetes Deployment Tool. It builds the docker image, creates the Kubernetes YAML, and runs kubectl apply. It automates the deployment process and saves you precious finger-typing energy.
-
Jets
Jets: The Ruby Serverless Framework
Ruby on Jets allows you to create and deploy serverless services with ease, and to seamlessly glue AWS services together with the most beautiful dynamic language: Ruby. It includes everything you need to build an API and deploy it to AWS Lambda. Jets leverages the power of Ruby to make serverless joyful for everyone.
-
Lono
Lono: The CloudFormation Framework
Building infrastructure-as-code is challenging. Lono makes it much easier and fun. It includes everything you need to manage and deploy infrastructure-as-code.