AWS CloudFormation
AWS CloudFormation is an infrastructure-as-code service on Amazon Web Services that collects AWS and third-party resources and manages them throughout their lifecycle by launching them together as a single stack. It lets teams reliably provision, manage, and update infrastructure from reusable JSON or YAML templates instead of configuring resources one by one, and the same templates can be deployed repeatably across multiple AWS regions to keep development and production environments consistent. Unlike AWS Elastic Beanstalk, which deploys applications onto Amazon EC2 and cannot consume CloudFormation templates, CloudFormation can itself launch an Elastic Beanstalk environment.
Key points
- Stacks — the unit CloudFormation manages: a collection of AWS and third-party resources created, updated, and deleted together as a single unit rather than managed individually. Stacks can be created via the CloudFormation console or the AWS Command Line Interface (CLI).
- Nested stacks — stacks created inside another stack using the
AWS::CloudFormation::Stackresource; the containing stack is the parent stack and the nested ones are child stacks, referenced with the!Refvariable. - Templates — JSON or YAML documents that define the resources to create, update, or delete as a single unit; templates can be reused to set up the same resources repeatedly, and support maintaining a development environment and a production environment in a repeatable fashion.
- Templates can be deployed across multiple AWS regions.
- AWS CloudFormation vs. AWS Elastic Beanstalk:
- CloudFormation deploys infrastructure from YAML/JSON template files and can deploy Elastic Beanstalk environments.
- Elastic Beanstalk deploys applications onto EC2 and cannot deploy CloudFormation templates.
- Distinct from AWS Cloud Development Kit (CDK), which lets developers define resources using programming languages such as TypeScript, Python, Java, and .NET; CDK code synthesizes into CloudFormation templates that CloudFormation then provisions.
- StackSets — extends stacks across multiple AWS accounts and Regions from a single operation: an administrator (or delegated administrator) account creates a stack set and deploys, updates, or deletes the resulting stack instances across all target accounts and Regions together.
- Change sets — a preview of how a proposed template or parameter update will change a running stack’s resources (create, update, replace, or delete) before it is executed, so the impact can be reviewed and approved first.
- Drift detection — checks whether a stack’s actual resource configuration still matches its template, flagging resources that were modified outside of CloudFormation (for example, directly in the console or CLI) as
DRIFTED. - Example CloudFormation template (YAML) for an EC2 instance:
Resources:
EC2Instance:
Type: AWS::EC2::Instance
Properties:
ImageId: 1234xyz
KeyName: aws-keypair
InstanceType: t2.micro
SecurityGroups:
- !Ref EC2SecurityGroup
BlockDeviceMappings:
- DeviceName: /dev/sda1
Ebs:
VolumeSize: 50Pricing
- AWS does not charge for using AWS CloudFormation itself; charges are applied for the underlying resources the template provisions.