AWS CloudFormation

WIP

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. I have created some example template files.

Stack

xxx

Changeset

xxx

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