Code Style guide for AWS Cloudformation and CDK


I am looking to standardize the work of my team, is there an official code style guide for cloudformation made by AWS?

In the case of using CDK with python, what code style guide do you recommend and why?

Do you know any libraries that can help me to order my templates automatically?

asked a year ago263 views
4 Answers
Accepted Answer

I'm not aware of any official style guides for CDK or CloudFormation.
For CDK, start with PEP8 and it's style checker (many IDEs will also apply PEP8 formatting). From there it is really up to your team to devise (or borrow) a coding standard as you would for any other Python project.

For CloudFormation a great policy enforcement tool is CloudFormation Guard. It has basic canned rules and you can also add your own. As for "ordering" (keys?), CFn templates are simply data structures so you could read them into Python as dictionaries, sort by key, then re-export.

answered a year ago

Beyond CloudFormation Guard, which was mentioned on the previous answer, here are a couple of tools that I use on a daily basis with CloudFormation templates:

  • cfn-lint CloudFormation linter
  • cfn-nag CloudFormation security linter
  • cfn-include CloudFormation pre-processor. This is a great simple way to break up a template into more manageable, smaller files.
answered a year ago

There is an open source CFN formatter by AWS that you could use to pre-process templates into a standard format before committing. cfn-format.

That can be used in conjunction with a tool like pre-commit to automatically format templates into a standard format.

An example of that is here:

- id: cfn-format
  name: Format CloudFormation templates
  entry: cfn-format -w
  language: golang
  files: \.template$
  description: Format CloudFormation templates
answered a year ago

We're in the same boat... and an official CDK style guide is sorely lacking.

Naming conventions are not merely opinionated as some styles will hang up @ synth.

This unofficial guide is heading in the right direction -->

Ironically it's from a former AWS'er that they probibly should have kept around.

Good luck on your own project!

profile picture
answered a year ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions