How to reduce your CloudFormation template size

Recently, I came across a limit which I haven’t known before: CloudFormation just allows a maximum size of 51,200 bytes per template. If you have ever reached this limit, you might have encountered this error message:

So, what are the possible solutions to reduce a CloudFormation template size? In my opinion, there are two relatively easy solutions to overcome this problem: using a preprocessor and/or using AWS::Include.

a) Use a preprocessor

There are some CloudFormation template preprocessors available. One of them is cfn-include (a CLI tool) which does a good job in my opinion: it reads your template file (JSON or YAML) and can produce a minified JSON template file. Using this tool, it can reduce your template size e.g. from 50 kb to 40 kb (Note: it always depends on the content you have). Here is a small example how to use it:

This is a first and easy step to come around the size limitation, but won’t save you forever. Other preprocessor alternatives are StackFormation or awsboxen. You might want to check them out as well!

b) Use AWS::Include

Another option would be to use the new AWS::Include command. Using such a simple snippet like below, you can import another template:

It’s important to note that you must only enter the actual parts in the included template which you want to include. Nothing else like parameter declarations, outputs, etc. You have to add such things to the main template! As you also might derive from the code example, all included templates must be uploaded to S3 first. Uploading it first adds some more work around a stack deployment. This can be automated in a deployment script, so it shouldn’t be a big deal. Here is an example on how to achieve this using a certain parameter to hand over the S3 Url:

Now, when deploying the CloudFormation stack, CloudFormation resolves the included templates by downloading them from S3 and inserts them directly into your main template. Each of the included ones (and of course the main template as well) may not exceed the aforementioned byte size limit, otherwise this will fail again.

Although it seems to be nice that such kind of import is possible, it comes with a few drawbacks:

  • In the beginning, I did not know that the included template may not contain the short version of an intrinsic function. This was an issue for me, because I had to change all functions to use the full notation.
  • If you’re using a code completion plugin for CloudFormation templates like I do for IntelliJ, then you’ll probably see many errors in your templates, because it can’t resolve parameters or other stack resources anymore. That’s not a problem of AWS::Include directly. But the point is you’re bringing in some more complexity into your stack setup. So keep in mind that you structure your code and include templates in a good way.
  • You can’t export parameters into an include template.

For a more detailed view on this, please read this excellent blog post:

Interesting to know: AFAIK you can not outsource Lambda functions using Serverless Application Model (SAM) into separate templates. The reason is that they need to be transformed first. In that case you’d need to add other lines into your template, but then it’s not an include template anymore.


Finally, I want to say that I prefer the AWS::Include solution, because I think you just postpone the problem by using a preprocessor. At some point, you will reach the limit again and then you need to change an even bigger stack. So try to be prepared for this problem by building a modular stack from the beginning! Also take a look at my GitHub repository aws-cloudformation-templates where I have included an example using AWS::Include to reduce your CloudFormation template size.