MODULE 15 - Infrastructure as Code (CloudFormation)
Overview
This final week introduces Infrastructure as Code (IaC) with AWS CloudFormation. You will learn to define your infrastructure in versionable and reproducible configuration files.
Final Project
this module includes the Final Project (30%): creation of a complete CloudFormation template.
LocalStack Supports CloudFormation!
LocalStack emulates CloudFormation (basic). You can test your templates locally!
Learning Objectives
- Understand the principles of Infrastructure as Code
- Write CloudFormation templates (YAML)
- Deploy and manage stacks
- Diagnose deployment errors
- Apply IaC best practices
Weekly Content
1. Why Infrastructure as Code?
┌─────────────────────────────────────────────────────────────┐
│ INFRASTRUCTURE AS CODE │
├─────────────────────────────────────────────────────────────┤
│ │
│ BEFORE (Manual) AFTER (IaC) │
│ ────────────── ────────── │
│ │
│ • Clicks in the console • Code files │
│ • Outdated documentation • Code = Documentation │
│ • "It worked yesterday..." • Reproducible │
│ • Different environments • Identical │
│ • Hard to audit • Version control │
│ • Manual rollback • Automatic rollback │
│ │
│ Benefits: │
│ ✅ Reproducibility ✅ Versioning (Git) │
│ ✅ Consistency ✅ Code review │
│ ✅ Automation ✅ Easy rollback │
│ ✅ Living documentation ✅ Possible tests │
│ │
└─────────────────────────────────────────────────────────────┘
2. CloudFormation - Concepts
┌─────────────────────────────────────────────────────────────┐
│ AWS CLOUDFORMATION │
├─────────────────────────────────────────────────────────────┤
│ │
│ TEMPLATE (YAML/JSON) │
│ ┌─────────────────────────────────────────────────────┐ │
│ │ AWSTemplateFormatVersion: '2010-09-09' │ │
│ │ Description: My infrastructure │ │
│ │ │ │
│ │ Parameters: │ │
│ │ Environment: │ │
│ │ Type: String │ │
│ │ │ │
│ │ Resources: │ │
│ │ MyVPC: │ │
│ │ Type: AWS::EC2::VPC │ │
│ │ Properties: │ │
│ │ CidrBlock: 10.0.0.0/16 │ │
│ │ │ │
│ │ Outputs: │ │
│ │ VPCId: │ │
│ │ Value: !Ref MyVPC │ │
│ └─────────────────────────────────────────────────────┘ │
│ │ │
│ ▼ │
│ ┌─────────────────────────────────────────────────────┐ │
│ │ STACK │ │
│ │ (Set of created resources) │ │
│ │ │ │
│ │ VPC ─── Subnet ─── Security Group ─── EC2 │ │
│ │ │ │
│ └─────────────────────────────────────────────────────┘ │
│ │
└─────────────────────────────────────────────────────────────┘
3. Structure of a Template
AWSTemplateFormatVersion: '2010-09-09'
Description: Basic template for the AWS course
# Input parameters
Parameters:
EnvironmentName:
Type: String
Default: dev
AllowedValues:
- dev
- staging
- prod
# Resources to create
Resources:
# VPC
MyVPC:
Type: AWS::EC2::VPC
Properties:
CidrBlock: 10.0.0.0/16
EnableDnsHostnames: true
Tags:
- Key: Name
Value: !Sub ${EnvironmentName}-vpc
# Public Subnet
PublicSubnet:
Type: AWS::EC2::Subnet
Properties:
VpcId: !Ref MyVPC
CidrBlock: 10.0.1.0/24
MapPublicIpOnLaunch: true
Tags:
- Key: Name
Value: !Sub ${EnvironmentName}-public-subnet
# Outputs
Outputs:
VPCId:
Description: ID of the created VPC
Value: !Ref MyVPC
Export:
Name: !Sub ${EnvironmentName}-vpc-id
4. Useful Intrinsic Functions
| Function | Usage | Example |
|---|---|---|
!Ref | Reference a resource/parameter | !Ref MyVPC |
!Sub | Variable substitution | !Sub ${AWS::StackName}-vpc |
!GetAtt | Resource attribute | !GetAtt MyEC2.PublicIp |
!Join | Concatenate values | !Join ['-', [a, b, c]] |
!If | Condition | !If [IsProd, m5.large, t3.micro] |
5. Lab with LocalStack
# Deploy a stack on LocalStack
awslocal cloudformation create-stack \
--stack-name my-stack \
--template-body file://template.yaml \
--parameters ParameterKey=EnvironmentName,ParameterValue=dev
# Check the status
awslocal cloudformation describe-stacks \
--stack-name my-stack
# List created resources
awslocal cloudformation list-stack-resources \
--stack-name my-stack
# Delete the stack
awslocal cloudformation delete-stack \
--stack-name my-stack
Final Project (30%)
Objective
Create a CloudFormation template deploying a simple web architecture:
- 1 VPC with public subnet
- 1 Security Group (HTTP + SSH)
- 1 EC2 Instance with user-data installing a web server
Deliverables
-
CloudFormation Template (YAML)
- Well-structured and commented
- Parameters for the environment
- Useful outputs
-
Documentation
- Description of the architecture
- Deployment instructions
- Validation tests
-
Demonstration
- Deployment on LocalStack
- Proof of functionality
- Questions/Answers
Evaluation Grid
| Criteria | Points |
|---|---|
| Valid and functional template | 10 |
| Best practices (params, outputs) | 5 |
| Clear documentation | 5 |
| Tests and validation | 5 |
| Demonstration and Q/A | 5 |
| Total | 30 |
Navigation
- IaC and CloudFormation Concepts
- Validation Quiz - 30 questions to validate your knowledge
- Lab: CloudFormation
🎓 Congratulations!
You have completed the AWS Compute Services course!
Acquired Skills
- ✅ Cloud and AWS concepts
- ✅ EC2 and EBS storage
- ✅ VPC networking and security
- ✅ High availability and scaling
- ✅ Serverless with Lambda
- ✅ Monitoring and observability
- ✅ FinOps and optimization
- ✅ Infrastructure as Code
Next Steps
- 📚 AWS Certifications (Cloud Practitioner, Solutions Architect)
- 🔧 Practice on a real AWS account (Free Tier)
- 🚀 Personal projects
- 💼 Professional experience
Good luck on your cloud journey!