AWS CloudFormation

“AWS CloudFormation is a service that helps you automate the process of creating, updating, and deleting Amazon Web Services (AWS) resources. It does this by using templates written in JSON or YAML, which define the desired state of the resources in your infrastructure. The templates are processed by CloudFormation, which creates, updates, or deletes the resources as needed to reach the desired state.”

Terraform != CloudFormation

Terraform and CloudFormation are both infrastructure-as-code (IaC) tools. CloudFormation is developed by AWS and only manages AWS resources. Terraform is developed by HashiCorp and can manage resources across a wide range of cloud vendors. - https://www.toptal.com/terraform/terraform-vs-cloudformation

AWS CloudFormation terms

Template file

This is the definition file that describes the resources you want to create, it can be yml or json.

Examples:

Stack

“A CloudFormation Stack is a collection of AWS resources created and managed as a single unit. You create, update, and delete a stack as a single deployment, and CloudFormation automatically handles the dependencies between the different resources in your stack. Each stack has a unique name and is based on a single CloudFormation template.”

Changeset

“AWS CloudFormation Changesets are a feature that allows you to preview the changes that will be made to your CloudFormation stack before you choose to execute the changes. This way you can make sure the changes you’re about to make will not cause any unintended consequences, such as deleting resources or modifying critical settings.

A Changeset is a summary of changes that will be made to your CloudFormation stack. It includes the change type (e.g. create, update, or delete), the logical ID of the resource that will be affected, and the property values that will be changed. Once you have reviewed the Changeset, you can choose to execute or discard the changes.”

Example: Lightsail Containers

A simple lightsail containers example created though cloud formation. The Container Service and its Container Service Deployment is created with the deployment template but the images running in the container are pre-built (I did it on my PC) and then pushed to Docker Hub.

  1. Clone the app and proxy source code used in the post AWS Lightsail.
1
2
git clone https://github.com/carlpaton/LightsailDemo
cd LightsailDemo
  1. Create repositories at docker hub or what ever registry you are using. I used these names:
1
2
carlpaton/containerservice43.proxy-image
carlpaton/containerservice43.app-image
  1. Build, tag and push the images
1
2
3
4
5
6
7
docker build -t app-image ./app/ 
docker tag app-image carlpaton/containerservice43.app-image
docker push carlpaton/containerservice43.app-image

docker build -t proxy-image ./proxy/
docker tag proxy-image carlpaton/containerservice43.proxy-image
docker push carlpaton/containerservice43.proxy-image
  1. Clone the template source code
1
2
3
cd..
git clone https://github.com/carlpaton/AWSCloudFormation
cd AWSCloudFormation/lightsail
  1. Ensure the template has the correct images under ContainerServiceDeployment->Containers->Image (note containers is an array)
1
2
3
4
5
- ContainerName: proxy
Image: carlpaton/containerservice43.proxy-image:latest

- ContainerName: app
Image: carlpaton/containerservice43.app-image:latest
  1. Create the stack through the CLI, –profile carlos is local on my machine. This can also be done though the Console
1
aws --profile carlos cloudformation create-stack --stack-name stackservice43 --template-body file://containerservice43.yml

The user group will need the the following permissions added under IAM

1
AWSCloudFormationFullAccess
  1. This will create the Cloudformation stack stackservice43 which will create the Lightsail container service containerservice43

Boom - infrastructure as code :D