Skip to main content

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

AspectCloudFormationTerraform
ProviderAWS onlyMulti-cloud
LanguageYAML/JSONHCL
StateManaged by AWSLocal or remote file
CostFreeFree (Open Source)
Drift DetectionBuilt-inLimited
RollbackAutomaticManual
ModulesNested StacksNative 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

  1. Write: Create the YAML/JSON template
  2. Validate: Check the syntax
  3. Create/Update: Deploy the stack
  4. Monitor: Check the events
  5. 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

  1. Open the CloudFormation console
  2. Click Create stack
  3. Upload the template
  4. Name the stack
  5. Configure the options
  6. Review and create

Stack states

StateDescription
CREATE_IN_PROGRESSCreation in progress
CREATE_COMPLETECreation succeeded
CREATE_FAILEDCreation failed
UPDATE_IN_PROGRESSUpdate in progress
DELETE_IN_PROGRESSDeletion in progress
ROLLBACK_IN_PROGRESSRollback 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

LimitValue
Max template size (S3)1 MB
Max template size (direct)51,200 bytes
Resources per stack500
Stacks per account/region2000
Parameters per template200
Outputs per template200
Mappings per template200

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

← Table of contents | Template Basics →