Hypermodern Cloudformation

Getting Started

Posted by Levi on Friday, March 5, 2021
Cloudformation Logo

Posts in this Series

I recently read a blog post by Claudio Jolowicz called Hypermodern Python. In his series Claudio takes us through all the latest and greatest advancements in the Python ecosystem. Even though I would consider myself to be a Python veteran it completely changed the way I interact with Python on a day-to-day basis. As we approach Cloudformation’s 10th birthday, let us also take a fresh look at Cloudformation’s ecosystem so that we can create and deliver infrastructure faster and better than before.

Setup

Before we get started your going to need a Cloudformation template that you want to test. If you don’t have one available you can use follow along with this guide’s companion repository by starting on this branch.

git clone https://github.com/DontShaveTheYak/hypermodern-cloudformation.git
cd hypermodern-cloudformation

In the guide we will be following the same directory structure and naming scheme as the AWS quick-start developers.

Most of the tools that we are going to be using in the Hypermodern Cloudformation series will be Python based and pip installable. I will be using Pyenv to manage my Python version and virtual environments. Hypermodern Python covers Pyenv setup here. If your experienced in Python but not using Pyenv then it should be easy to follow along, just create a virtual environment with the tool of your choice.

Let’s first setup a virtual environment for Python 3.8.

pyenv virtualenv 3.8.0 hyper-cf
pyenv local hyper-cf 

Since we are not creating a full-fledged python project I’m going to deviate from Hypermodern Python and not use Poetry for dependency management but instead rely on pip. The first dependency we are going to have is pre-commit. pre-commit is an awesome pip installable package that will allow us to configure pre-requisites that have to be met before the changes to the code can be made.

pip install pre-commit

You can check that it’s working with pre-commit -h. It’s also a good practice to save these dependencies so they are reproducible by other teammates and in CI/CD.

pip freeze > requirements.txt

Lets setup pre-commit with some basic checks. Create .pre-commit-config.yaml with the following contents:

repos:
-   repo: https://github.com/pre-commit/pre-commit-hooks
    rev: v2.3.0
    hooks:
    -   id: end-of-file-fixer
    -   id: trailing-whitespace

We now can now “turn on” pre-commit in this repo.

pre-commit install
# Now would be a good time to commit our changes.
git add .
git commit -m "add pre-commit to repo"

One thing to keep in mind is that pre-commit will only run against files that are staged for commit. Let ask pre-commit to check our existing files.

pre-commit run --all-files

Ugh oh 😦. It appears I had some mistakes in my template already. Luckily these checks not only caught these issues but fixed them for me. Go ahead and inspect the changes with git diff and then commit them.

git commit -am "fixed white space issues"

If you have been following along with the companion repo then your branch should look similar to this. Now that your development environment is setup you are ready for static code analysis.