CloudFormation
Introduction to CloudFormation
CloudFormation is an AWS service that allows you to codify infrastructure on AWS. That means instead of clicking through the AWS console or using the AWS CLI to spin up resources, you instead describe the resources using CloudFormation templates that are written or JSON or YAML format. A CloudFormation stack is the group of resources that are created with one template.
Infrastructure as Code
CloudFormation is a type of Infrastructure as Code. Instead of physically allocating servers or manually creating a database, the desired infrastructure is described and configured in a document. When the infrastructure needs to be created, the stack is launched with one click or command and all of the resources are created. When the resources are no longer needed, the stack can be deleted with one click, and all of the resources will be deleted. If a problem with the infrastructure arises, it can be deleted and recreated very quickly. Errors in the infrastructure can be resolved during development time, and as long as the template file is saved, the stack can be recreated as needed.
CloudFormation Template
CloudFormation Templates can be written in YAML or JSON. YAML is a better choice because it's cleaner and you can add comments.
The following is an example of a very minimal CloudFormation template that creates an S3 Bucket called create_bucket.yaml.
AWSTemplateFormatVersion: "2010-09-09"
Description: Creates S3 Bucket
Resources:
MyS3Bucket:
Type: AWS::S3::Bucket
CloudFormation Using AWS CLI
To run the CloudFormation template and create a CloudFormation stack, use this AWS CLI command.
aws cloudformation create-stack --stack-name my-test-stack --template-body create_bucket.yaml
To check to see if the stack is created, use the command following command or click through the AWS CloudFormation console.
aws cloudformation describe-stacks --stack-name my-test-stack
It is very helpful to use the CloudFormation console for troubleshooting because you can click through the Events and Resources screens to see which of your resources have been successfully created or reasons for failures.
To delete your CloudFormation stack, use the following command.
aws cloudformation delete-stacks --stack-name my-test-stack
Additional Resources
CloudFormation Deep Dive Course on A Cloud Guru
CloudFormation Master Class on Udemy
StelligentU CloudFormation Training