3.2.1. AWS CloudFormation Fundamentals
š” First Principle: AWS CloudFormation provides a declarative Infrastructure as Code (IaC) service, enabling SysOps Administrators to model, provision, and manage AWS resources in a consistent, repeatable, and automated manner.
Scenario: You need to provision a new application environment, including EC2 instances, an RDS database, and specific networking configurations. You want this entire setup to be consistent and repeatable across development and staging environments.
AWS CloudFormation is a core AWS service that allows you to define your AWS infrastructure as code. You create templates in JSON or YAML that describe the AWS resources you want to provision (e.g., EC2 instances, S3 buckets, VPCs, databases).
Key CloudFormation Fundamentals:
- Declarative Language: You describe the desired state of your infrastructure, and CloudFormation figures out the steps to provision and update resources.
- Templates: (Text files (JSON or YAML) that define the AWS resources you want to provision.) These are the blueprint of your infrastructure.
- Stacks: (A collection of AWS resources that you can manage as a single unit.) When you deploy a CloudFormation template, CloudFormation creates a stack, managing all the defined resources as a single unit.
- Dependencies: CloudFormation automatically understands and manages dependencies between resources (e.g., ensuring a VPC exists before deploying an EC2 instance into it).
- Rollback: If any resource fails to provision or update during a stack operation, CloudFormation automatically rolls back all changes to the previous stable state.
- Drift Detection: Identifies when actual resource configurations deviate from the template's defined state.
ā ļø Common Pitfall: Not using CloudFormation parameters for environment-specific values, leading to duplicate templates or manual edits.
Key Trade-Offs: Declarative IaC (CloudFormation, consistent, auditable) versus imperative scripting (more flexible, but less consistent, harder to manage state).
Practical Implementation: Creating a CloudFormation stack via CLI:
aws cloudformation create-stack \
--stack-name MyWebAppStack \
--template-body file://my-webapp-template.yaml \
--parameters ParameterKey=InstanceType,ParameterValue=t3.micro \
--capabilities CAPABILITY_IAM
Reflection Question: How does AWS CloudFormation, by allowing you to define your infrastructure declaratively in a template and manage it as a single "stack," fundamentally enable repeatable provisioning and consistent management of AWS resources?