DEV Community

Prithvi Jethwa
Prithvi Jethwa

Posted on

CloudFormation Template does count whitespaces in Template Size...

Context

We hit the limit for AWS CloudFormation Template Size of 1 MB which caused our deployments into alpha, staging, and production to fail. Ideally the suggestion is:

  • Use Nested Stacks
  • Create more CloudFormation Templates
  • Remove unnecessary resources, or consider consolidating IAM Roles and Policies

We were going to follow our next steps to move the monitoring resources (example: AWS CloudWatch Alarms) into the Nested Stack, but doing a quick analysis using Claude led me to an interesting yet questionable find.

Analysis

I found that 50% of the CloudFormation Template Size was just whitespaces, which seems innocent but considering the 1 MB limit, that's a lot.

To experiment, I ended up running jq to remove all the whitespaces using

jq -c '.' cloudformation-stack-update.json > minified.json
Enter fullscreen mode Exit fullscreen mode

Within the Infrastructure Composer, I copied both the CloudFormation Templates, and clicked on the Validate Template button. The Regular Template showed that the file has exceeded the 1 MB limit, meanwhile the minified CloudFormation Template successfully validated.

Next Steps

The Platform Team has a Deployment Pipeline that handles all the packaging of CloudFormation Templates using the Serverless Framework, so I ended up making a Feature Request about the very issue about minifying things.

While they would get to it eventually based on priorities, I had an inner rush to resolve it. I used to be a part of that Platform Team, so it was conveniently easy to get back and contribute to those Deployment Pipelines.

Opened a PR into the Repository, added in my testing notes and got it reviewed and merged. After retrying our service related builds, we saw a 50% cut on the CloudFormation Template Size, helping us unblock our deployments! 🎉

Why is this important

It is an interesting gotcha, cause you would not expect whitespaces to eat into the CloudFormation Template Size, but I ran into this issue, and I am hoping others do not run into it. So consider minifying those templates.

While I haven't seen an option in the Serverless Framework, adding a provider.minify would be cool to have.

Reference

Top comments (0)