The Basics of Cloudformation

Created:

What is Cloudformation?

From the AWS docs:

Cloudformation is an infrastructure as code (IaC) service that allows you to easily model, provision, and manage AWS and third-party resources"

In other words, you can configure the services you want to set up in AWS through code - either with a JSON or a YAML file. It's the AWS equivalent to Terraform, if you're familiar with that.

How it works

In practical terms, this means you would have a file describing what resources you want plus some other information required for that (a template of sorts) and you could then apply that to your AWS account - under "Cloudformation" in the AWS UI (called "console") or through the CLI.

The parent properties of a JSON Cloudformation file would be something like this:

{
  "AWSTemplateFormatVersion": "2010-09-09",

  // The input that will be given to this template
  // and that can be referenced in Resources and Conditions
  "Parameters": {},

  // The conditions and dependencies to create your resources
  "Conditions": {},

  // This is what will be output if you run this with the CLI or with the SDK
  "Outputs": {},

  // The AWS resources you want to create
  "Resources": {}
}

This file is describing what AWS calls a stack. Ultimately, the goal of this template is to create whatever has been configured in "Resources". It's good to note that AWS tries to create all the resources concurrently, except if there's a dependency either through a (a) DependsOn property or if you are referencing another service.

ocean

Something else to note is that within this Cloudformation stack you'll also need to configure the Roles and Permissions of the Resources you're creating using the IAM roles, but that's an entirely different and complex topic on its own.

ocean

If you want to create this stack in AWS, you can use the AWS console to do so. You'll need to look for "Cloudformation" in the search bar, then click on "Create stack" and you'll be taken to the following page:

Once you have uploaded the Cloudformation file and gone through the steps, AWS will create the stack with the desired resources, which you can inspect. The interesting thing is that you can see some of the parent properties from the JSON file in UI once you have created the stack.

The actual details of the Cloudformation "language" are very lengthy and would not be in the scope of this small walkthrough. You can see below some resources to get started with it.

Useful to know

AWS Templates

AWS has good documentation on the templates for Cloudformation.

Visualising the template

If you check the list of sample solutions provided by AWS, you'll see there's an option to "View in Designer". This will open the template in a visualizer where you can see which resources would be created with a given template. You can also substitute the example template with one you've created to see what resources are configured.