AWS CloudFormation is an Infrastructure as Code (IaC) service that helps users create and manage AWS resources using code templates.
It allows you to define AWS resources like EC2 instances, S3 buckets, RDS databases, and networking components in code templates.
Automates the provisioning and deployment process, eliminating the need to manually configure resources in the AWS Console.
Ensures infrastructure consistency by creating identical environments across multiple regions or accounts using the same template.
Supports version control and change management, allowing you to track modifications, roll back updates, and maintain reliable infrastructure.
Templates and Stacks
CloudFormation works using two main concepts:
Template: A JSON or YAML file that defines the AWS resources to be created.
Stack: The collection of AWS resources created and managed from the template.
AWS CloudFormation Working
Amazon Web Services (AWS) provides cloud services such as EC2, S3, Auto Scaling, and Load Balancing. AWS CloudFormation helps automate the provisioning of these services using Infrastructure as Code (IaC), reducing manual configuration.
Automation: Automates the creation and management of AWS resources.
Consistency: Creates identical infrastructure using reusable templates.
Cost Savings: Reduces manual effort and reuses existing templates.
Security: Applies security policies and configurations consistently.
Scalability: Easily scales resources based on demand.
Getting Started with AWS CloudFormation
CloudFormation templates are written in JSON or YAML format. In this article, we focus on the JSON template format.
JSON (JavaScript Object Notation) is a text-based format used to represent structured data. In AWS CloudFormation, JSON templates define AWS resources and their configurations in a structured way, which CloudFormation uses to create and manage the AWS infrastructure.
Structure of CloudFormation JSON Template
Format Version: Defines the version of the CloudFormation template.
Description: Contains additional details or comments about the template.
Metadata: Provides extra information using JSON objects.
Parameters: Accepts custom or dynamic input values during stack creation.
Mappings: Maps keys to corresponding values for conditional use.
Conditions: Controls whether specific resources or properties are created.
Transform: Helps reuse template components using declarative macros.
Resources: Defines AWS resources like EC2, S3, and Lambda in the stack.
Outputs: Displays values generated after stack creation, such as IDs or URLs.
Template Terms and Concepts
Understanding The Core Concepts That CloudFormation templates use to organize resources, settings, and functions is key to managing AWS infrastructure efficiently.
Template: A CloudFormation template is a JSON or YAML file that defines the AWS resources and configurations to be created.
Stacks: A stack is the collection of AWS resources created from a CloudFormation template and managed as a single unit.
Formatting: CloudFormation templates use JSON or YAML format. YAML is commonly preferred because it is simpler and more readable.
Change Sets: Change Sets allow you to preview the changes CloudFormation will make before updating a stack, helping reduce deployment risks.
Functions: CloudFormation provides built-in functions such as Fn::Sub and Fn::Join to create dynamic and flexible configurations.
Parameters: Parameters allow users to provide custom input values during stack deployment, making templates reusable and flexible.
Conditions: Conditions control whether specific resources are created or configured based on certain criteria, such as development or production environments.
Template Anatomy
A CloudFormation template consists of several standard sections. The most important are:
1. Resources (Mandatory)
The Resources section defines the AWS resources to be created in the stack.
In the designer page, you will see the template, including an EC2 instance with Apache and MySQL installed. The security groups associated with the instance will also be visible here.
You can modify this template as needed to meet your specific requirements.
Select the instance type (e.g., t2.micro, m5.large) and choose an available key pair that will allow you to make an SSH connection to the EC2 instance.
Finally, click Create Stack. CloudFormation will start creating the instance, and the LAMP stack (Linux, Apache, MySQL, PHP) will be automatically installed on the EC2 instance.
AWS CloudFormation is a very Famous infrastructure-as-code tool for managing AWS resources several alternatives offer different features and support for multi-cloud environments. Listing out some of very popular alternatives.
Terraform:An open-source Infrastructure as Code (IaC) tool used to automate cloud infrastructure deployment. It supports multiple cloud providers such as AWS, Azure, and Google Cloud, making it suitable for multi-cloud environments.
Pulumi:An Infrastructure as Code tool used to automate and manage cloud resources efficiently. It supports cloud platforms like AWS, Azure, OCI, and Google Cloud using programming languages such as Python and JavaScript.
CloudFormation vs. Terraform
A common question for DevOps engineers is which tool to use.
AWS CloudFormation
Terraform
AWS Only. Deep integration with AWS features.
Multi-Cloud. Supports AWS, Azure, Google, etc.
Managed. AWS manages the state file for you.
Manual. You must manage the state file (e.g., in S3).
JSON or YAML.
HCL (HashiCorp Configuration Language).
Official AWS Support plan covers it.
Community support (unless you pay for Terraform Cloud).
Built-in feature.
Requires running terraform plan.
Advanced Features
1. CloudFormation Hooks
Hooks are a governance feature that invokes logic before a resource is created, updated, or deleted.
Use Case: "Stop the deployment if someone tries to create an S3 bucket that is NOT encrypted." Hooks act as a proactive compliance guardrail.
2. Drift Detection
Over time, someone might manually change a resource (e.g., changing a Security Group rule via the Console). This causes the actual infrastructure to "drift" from the CloudFormation template.
Drift Detection scans your resources and reports any differences, allowing you to fix the manual changes or update your template to match reality.
3. Change Sets
Before updating a live stack, you should create a Change Set. This is a "preview" that tells you exactly what CloudFormation will do (e.g., "I will modify this EC2 instance and DELETE this database") before you click execute.