“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.
- Clone the
app
andproxy
source code used in the post AWS Lightsail.
1 | git clone https://github.com/carlpaton/LightsailDemo |
- Create repositories at docker hub or what ever registry you are using. I used these names:
1 | carlpaton/containerservice43.proxy-image |
- Build, tag and push the images
1 | docker build -t app-image ./app/ |
- Clone the
template
source code
1 | cd.. |
- Ensure the template has the correct images under
ContainerServiceDeployment
->Containers
->Image
(note containers is an array)
1 | - ContainerName: proxy |
- 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 |
- This will create the Cloudformation stack
stackservice43
which will create the Lightsail container servicecontainerservice43
Boom - infrastructure as code :D