Introduction to AWS CloudFormation
What is CloudFormation?
AWS CloudFormation is a service that lets you model and provision AWS resources in an automated and repeatable way.
Basic principle
Key concepts
1. Template
A template is a YAML or JSON file that describes the AWS resources to create.
AWSTemplateFormatVersion: '2010-09-09'
Description: Mon premier template CloudFormation
Resources:
MonBucket:
Type: AWS::S3::Bucket
Properties:
BucketName: mon-bucket-unique-12345
2. Stack
A stack is a collection of AWS resources managed as a single unit.
3. Change Set
A change set lets you preview modifications before applying them.
4. Stack Events
Events track all operations on the stack.
CloudFormation vs Terraform
| Aspect | CloudFormation | Terraform |
|---|---|---|
| Provider | AWS only | Multi-cloud |
| Language | YAML/JSON | HCL |
| State | Managed by AWS | Local or remote file |
| Cost | Free | Free (Open Source) |
| Drift Detection | Built-in | Limited |
| Rollback | Automatic | Manual |
| Modules | Nested Stacks | Native modules |
When to use CloudFormation?
- 100% AWS infrastructure
- Need for automatic rollback
- Team already familiar with AWS
- Integration with AWS Organizations
CloudFormation workflow
Workflow steps
- Write: Create the YAML/JSON template
- Validate: Check the syntax
- Create/Update: Deploy the stack
- Monitor: Check the events
- Maintain: Manage the lifecycle
First template
Let's create a simple S3 bucket:
AWSTemplateFormatVersion: '2010-09-09'
Description: Création d'un bucket S3 simple
Resources:
MyS3Bucket:
Type: AWS::S3::Bucket
Properties:
BucketName: !Sub 'my-first-bucket-${AWS::AccountId}'
Tags:
- Key: Environment
Value: Development
- Key: ManagedBy
Value: CloudFormation
Deployment via CLI
# Valider le template
aws cloudformation validate-template \
--template-body file://template.yaml
# Créer la stack
aws cloudformation create-stack \
--stack-name my-first-stack \
--template-body file://template.yaml
# Vérifier le statut
aws cloudformation describe-stacks \
--stack-name my-first-stack
# Voir les events
aws cloudformation describe-stack-events \
--stack-name my-first-stack
Deployment via Console
- Open the CloudFormation console
- Click Create stack
- Upload the template
- Name the stack
- Configure the options
- Review and create
Stack states
| State | Description |
|---|---|
CREATE_IN_PROGRESS | Creation in progress |
CREATE_COMPLETE | Creation succeeded |
CREATE_FAILED | Creation failed |
UPDATE_IN_PROGRESS | Update in progress |
DELETE_IN_PROGRESS | Deletion in progress |
ROLLBACK_IN_PROGRESS | Rollback in progress |
Regions and availability
CloudFormation is available in all AWS regions. Each stack is created in a specific region.
# Créer dans une région spécifique
aws cloudformation create-stack \
--stack-name my-stack \
--template-body file://template.yaml \
--region eu-west-1
# Lister les stacks d'une région
aws cloudformation list-stacks \
--region eu-west-1 \
--stack-status-filter CREATE_COMPLETE UPDATE_COMPLETE
Limits and quotas
| Limit | Value |
|---|---|
| Max template size (S3) | 1 MB |
| Max template size (direct) | 51,200 bytes |
| Resources per stack | 500 |
| Stacks per account/region | 2000 |
| Parameters per template | 200 |
| Outputs per template | 200 |
| Mappings per template | 200 |
Essential CLI commands
# Créer une stack
aws cloudformation create-stack \
--stack-name NAME \
--template-body file://template.yaml
# Mettre à jour une stack
aws cloudformation update-stack \
--stack-name NAME \
--template-body file://template.yaml
# Supprimer une stack
aws cloudformation delete-stack \
--stack-name NAME
# Lister les stacks
aws cloudformation list-stacks
# Décrire une stack
aws cloudformation describe-stacks \
--stack-name NAME
# Créer un change set
aws cloudformation create-change-set \
--stack-name NAME \
--change-set-name my-changes \
--template-body file://template.yaml
# Exécuter un change set
aws cloudformation execute-change-set \
--stack-name NAME \
--change-set-name my-changes
Summary
- CloudFormation is AWS's native IaC service
- Templates describe the infrastructure
- Stacks group resources together
- Change sets allow you to preview
- Rollback is automatic on error