Skip to content

Module 6: Terraform

Prerequisites: Module 5 (AWS — understand EC2, VPC, Security Groups before automating them)

In a nutshell: You replace manual clicks in the AWS console with code. Terraform lets you describe your infrastructure in files, versioned in Git, reproducible and shareable. What took you 30 min by hand, Terraform does in 2 min. And at the end of the module you wire your Module 4 pipeline to the server: a git push updates the app on its own.

The problem: You just created your AWS infrastructure by clicking around in the console. It took 30 minutes. Now imagine: your boss tells you “do the same thing for the staging environment”. And also for pre-prod. And document what you created for your colleague. And if you make a mistake, roll back.

With clicks, it’s impossible to reproduce, impossible to version, impossible to share. Terraform solves this: you describe your infrastructure in code. A text file, versioned in Git, that anyone can read and execute.

The analogy: Terraform is the architect’s blueprint for your infrastructure.

  • terraform plan = reviewing the blueprint with the client (“here’s what we’ll build”)
  • terraform apply = sending the construction crew
  • terraform destroy = demolition
  • The state file = the “as-built” plan

In one sentence: Infrastructure as Code (IaC) — your infra is code, not clicks.

You created this infrastructure manually in Module 5 (AWS). Terraform automates exactly the same steps.

Fenêtre de terminal
# Linux / WSL (Ubuntu) — via the HashiCorp repo
sudo apt update && sudo apt install -y gnupg software-properties-common
wget -O- https://apt.releases.hashicorp.com/gpg | gpg --dearmor | sudo tee /usr/share/keyrings/hashicorp-archive-keyring.gpg > /dev/null
echo "deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] https://apt.releases.hashicorp.com $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/hashicorp.list
sudo apt update && sudo apt install terraform
# macOS
brew tap hashicorp/tap
brew install hashicorp/tap/terraform
terraform --version
# Terraform v1.x.x

Terraform is learned by repeating. You write three lines, you apply, you look at the result, you fix it, you start again. Twenty times.

On a real AWS account that isn’t comfortable: every apply creates real resources, every destroy removes them, and if you forget something, you pay for it.

So we’ll apply against the local AWS from Module 5. Same Terraform, same commands, same files — but free, instant, and consequence-free.

Fenêtre de terminal
cd ~/devops-project/floci && docker compose up -d && cd -

A provider is the module that knows how to talk to a service (AWS, GCP, GitHub…). By default the AWS provider sends everything to real AWS. We give it four pieces of information to redirect it:

provider "aws" {
region = "us-east-1"
access_key = "test" # dummy credentials: the emulator doesn't check them
secret_key = "test"
# ─── The 4 "skips": disable the checks that make no sense locally ───
skip_credentials_validation = true # don't ask AWS whether these keys are valid
skip_metadata_api_check = true # don't try to detect whether we run on EC2
skip_requesting_account_id = true # don't ask for the AWS account number
s3_use_path_style = true # URLs like .../my-bucket instead of my-bucket....
# ─── Where to send each service's requests ───
endpoints {
ec2 = "http://localhost:4566"
s3 = "http://localhost:4566"
iam = "http://localhost:4566"
}
}

Without the skips it doesn’t work: Terraform would start by calling real AWS to validate your credentials, and fail before creating anything at all.

s3_use_path_style deserves an explanation. Real AWS puts the bucket name in the domain name (my-bucket.s3.amazonaws.com). Locally there’s no DNS for that, so we ask for the other form: localhost:4566/my-bucket. Forget this line and S3 fails with baffling DNS resolution errors.

We obviously don’t want two different Terraform files. So we put the address in a variable:

variable "aws_endpoint" {
description = "AWS API address. Empty = real AWS."
type = string
default = ""
}
provider "aws" {
region = var.aws_region
# These settings only kick in when a local address is provided.
skip_credentials_validation = var.aws_endpoint != ""
skip_metadata_api_check = var.aws_endpoint != ""
skip_requesting_account_id = var.aws_endpoint != ""
s3_use_path_style = var.aws_endpoint != ""
access_key = var.aws_endpoint != "" ? "test" : null
secret_key = var.aws_endpoint != "" ? "test" : null
# dynamic = only generate this block IF the condition holds
dynamic "endpoints" {
for_each = var.aws_endpoint != "" ? [1] : []
content {
ec2 = var.aws_endpoint
s3 = var.aws_endpoint
iam = var.aws_endpoint
}
}
}
Fenêtre de terminal
# Locally
terraform apply -var="aws_endpoint=http://localhost:4566"
# On real AWS: pass nothing, the variable stays empty
terraform apply

condition ? value_if_true : value_if_false is called a ternary operator — an if/else written on one line. You’ll find it in almost every language.

What you’ve just seen is the heart of the job. One infrastructure codebase, several environments, and only the configuration changes. It’s exactly the dev / staging / prod principle from Module 1, applied to infrastructure. In a company, that’s how a single codebase is driven by a dev.tfvars, a staging.tfvars and a prod.tfvars.

Before (clicks)After (Terraform)
Reproducible?NoYes, terraform apply
Documented?No (who remembers the clicks?)Yes, it’s code
Versioned?NoYes, in Git
Reviewable?NoYes, pull request
Rollback?No (you click in reverse…)Yes, previous commit

Terraform uses HCL (HashiCorp Configuration Language). It’s not a traditional programming language — it’s declarative: you describe WHAT YOU WANT (“I want a server with 2 GB of RAM in this region”), and Terraform takes care of the HOW (which APIs to call, in what order, etc.). It’s the opposite of imperative where you describe each step yourself (“first create the network, then create the server, then attach it to the network…”).

A provider connects Terraform to a service (AWS, GCP, Azure…).

terraform {
required_providers {
aws = {
source = "hashicorp/aws" # Where to find the provider: "publisher/name"
version = "~> 5.0" # ~> = "compatible with": accepts 5.1, 5.2... but not 6.0
}
}
}
provider "aws" {
region = "eu-west-3" # Paris -- the AWS region where your resources will be created
}

A resource = something that Terraform creates/manages.

resource "aws_instance" "mon_serveur" {
ami = data.aws_ami.ubuntu.id # Retrieved automatically (see data source)
instance_type = "t3.micro"
tags = {
Name = "devops-server"
}
}

The syntax: resource "TYPE" "LOCAL_NAME" { ... }. The type comes from the provider. The local name is your choice (to reference it in the code).

variables.tf
variable "instance_type" {
description = "EC2 instance type"
default = "t3.micro"
}
variable "project_name" {
description = "Project name"
default = "devops"
}

Usage: var.instance_type, var.project_name.

Displays info after apply (public IP, URL, etc.).

outputs.tf
output "public_ip" {
value = aws_instance.mon_serveur.public_ip
description = "Server public IP"
}
Fenêtre de terminal
# 1. Initialize (downloads the provider)
terraform init
# Initializing provider plugins...
# Terraform has been successfully initialized!
# 2. Preview changes
terraform plan
# Plan: 3 to add, 0 to change, 0 to destroy.
# (shows what will be created/modified/deleted)
# 3. Apply
terraform apply
# Do you want to perform these actions? yes
# Apply complete! Resources: 3 added, 0 changed, 0 destroyed.
# 4. Destroy everything
terraform destroy
# Do you really want to destroy all resources? yes
# Destroy complete! Resources: 3 destroyed.

🧪 Practice: the loop that makes you improve

Section titled “🧪 Practice: the loop that makes you improve”

This is the highest-return exercise in the module. It doesn’t teach you a command — it teaches you a reflex.

Create a sandbox folder:

Fenêtre de terminal
mkdir -p ~/tf-sandbox && cd ~/tf-sandbox
cat > main.tf <<'EOF'
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.0"
}
}
}
provider "aws" {
region = "us-east-1"
access_key = "test"
secret_key = "test"
skip_credentials_validation = true
skip_metadata_api_check = true
skip_requesting_account_id = true
s3_use_path_style = true
endpoints {
ec2 = "http://localhost:4566"
s3 = "http://localhost:4566"
}
}
resource "aws_vpc" "test" {
cidr_block = "10.0.0.0/16"
tags = { Name = "my-vpc" }
}
EOF
terraform init
terraform apply -auto-approve

Now run these experiments one by one, and each time read what terraform plan says before applying:

ExperimentWhat you should observe
Run terraform apply again with no changesNo changes. — Terraform doesn’t redo what already exists
Change the tags = { Name = ... }1 to change — modified in place, the VPC id stays the same
Change cidr_block to 10.1.0.0/161 to add, 1 to destroy — Terraform destroys and recreates: some attributes can’t be modified
Add an aws_subnet referencing the VPC1 to add — and Terraform creates it after the VPC, on its own
Remove the resource from the file1 to destroy — the code is the truth: what’s no longer in it gets deleted
Delete the resource by hand (awslocal ec2 delete-vpc ...) then plan1 to add — Terraform notices reality no longer matches the code. That’s called drift

The “modify in place” vs “destroy and recreate” distinction is an interview classic. In production, a plan announcing an unexpected destroy on a database is a disaster narrowly avoided. Always read the plan before applying — that’s THE professional reflex.

Fenêtre de terminal
# Clean up when you're done
terraform destroy -auto-approve

Run this loop as often as you like: it costs nothing and takes two seconds each time. That’s exactly what local AWS makes possible.

The terraform.tfstate file records the current state of your infrastructure — it’s Terraform’s memory. It knows “I created a server with ID i-abc123, a VPC with ID vpc-def456, etc.”. When you rerun terraform apply, it compares this file with your code to know what to create, modify, or delete.

⚠️ NEVER modify the state file by hand. ⚠️ NEVER commit the state file to Git (it can contain secrets).

In a team, you store the state on a remote backend (S3 for example) so everyone works on the same state.

So far the state is a file on your machine. That causes three problems as soon as there’s more than one of you:

  1. Your colleague can’t see your state. They think nothing exists and recreate everything twice.
  2. If you lose your disk, you lose the state. Terraform no longer knows what it created — the resources still exist on AWS, but it doesn’t recognise them.
  3. Two applys at the same time can collide and corrupt the state.

The fix: store the state in a remote backend, usually an S3 bucket. Let’s do it for real, locally.

Fenêtre de terminal
# The bucket already exists (created when Floci starts), otherwise:
awslocal s3 mb s3://taskflow-tfstate

💡 awslocal is a shortcut defined in the Floci / AWS locally appendix (it’s aws --endpoint-url http://localhost:4566). It points to us-east-1 by default — if your Terraform aws_region is eu-west-3, add --region eu-west-3 to your awslocal commands to see the resources.

Add a backend block inside terraform:

terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.0"
}
}
backend "s3" {
bucket = "taskflow-tfstate" # which bucket
key = "formation/terraform.tfstate" # which path inside it
region = "us-east-1"
# ─── Local AWS only ───
endpoints = {
s3 = "http://localhost:4566"
}
access_key = "test"
secret_key = "test"
skip_credentials_validation = true
skip_metadata_api_check = true
skip_requesting_account_id = true
skip_region_validation = true
skip_s3_checksum = true
use_path_style = true
}
}

⚠️ The backend block doesn’t accept variables. Unlike the rest of your Terraform code, you can’t write var.something in it: it’s read very early, before variables exist. In a company you therefore pass those values separately: terraform init -backend-config=dev.hcl.

The endpoints field in a backend requires Terraform 1.6 or later (terraform --version to check).

Fenêtre de terminal
# Re-initialise: Terraform detects the new backend
terraform init
# If a local state exists and isn't empty, Terraform asks:
# Do you want to copy existing state to the new backend? → yes
terraform apply

💡 If the local state was empty (for example after a destroy), Terraform asks nothing — that’s normal: there’s nothing to migrate.

Check that it worked:

Fenêtre de terminal
# The state now lives in the bucket
awslocal s3 ls s3://taskflow-tfstate/formation/
# 2026-08-22 14:54:37 1617 terraform.tfstate
# Terraform does read the remote state
terraform state list
# aws_vpc.main
# aws_subnet.public
# ...

⚠️ The local terraform.tfstate file may subsist. Depending on the Terraform version, after the migration a local terraform.tfstate remains (empty, 0 bytes, or stale) — useless, but not always deleted. Don’t rely on ls terraform.tfstate to verify: look at terraform state list and the contents of the S3 bucket instead.

What you just did is exactly what happens in a company. The state lives in a shared bucket, every team member works against the same state, and the bucket has versioning enabled so you can roll back. When a recruiter asks “where do you store your Terraform state?”, the expected answer is: “in a remote backend, S3 with versioning enabled, never in Git”.

A module = a reusable block of Terraform code. Like a function in programming. If you often create a VPC + EC2 + Security Group, you put that in a module and call it with different parameters.

We won’t create one in this course, but know that they exist.

Hands-on Project: Recreate the AWS infrastructure with Terraform

Section titled “Hands-on Project: Recreate the AWS infrastructure with Terraform”

We’ll recreate exactly what we did by hand in Module 5, but in code.

Do it locally first. Write all the code, run terraform apply against local AWS, fix your syntax and dependency mistakes — for free. Once it passes cleanly, do it again on real AWS by simply dropping the -var="aws_endpoint=...".

One caveat: the emulator doesn’t validate everything. An apply that passes locally can still fail on real AWS (IAM permissions, quotas, bucket names already taken worldwide). Local removes 90% of the errors — not 100%.

Fenêtre de terminal
mkdir -p ~/devops-terraform
cd ~/devops-terraform

Create main.tf:

terraform {
required_providers {
aws = {
source = "hashicorp/aws" # Where to find the provider: "publisher/name"
version = "~> 5.0" # ~> = "compatible with": accepts 5.1, 5.2... but not 6.0
}
}
}
provider "aws" {
region = var.aws_region # The AWS region (defined in variables.tf)
# --- Redirect to the local emulator (Floci) ---
# These lines make the SAME code usable locally AND on real AWS:
# - without -var, aws_endpoint is "" → we talk to real AWS
# - with -var="aws_endpoint=http://localhost:4566" → everything goes to Floci
skip_credentials_validation = var.aws_endpoint != ""
skip_metadata_api_check = var.aws_endpoint != ""
skip_requesting_account_id = var.aws_endpoint != ""
s3_use_path_style = var.aws_endpoint != ""
access_key = var.aws_endpoint != "" ? "test" : null
secret_key = var.aws_endpoint != "" ? "test" : null
dynamic "endpoints" {
for_each = var.aws_endpoint != "" ? [1] : []
content {
ec2 = var.aws_endpoint
s3 = var.aws_endpoint
iam = var.aws_endpoint
}
}
}
# --- VPC ---
# Doc: https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/vpc
resource "aws_vpc" "main" {
cidr_block = "10.0.0.0/16" # Network IP address range (65,536 addresses)
enable_dns_hostnames = true # Allows instances to have a DNS name (e.g.: ec2-13-38-xx.eu-west-3.compute.amazonaws.com)
tags = { Name = "${var.project_name}-vpc" } # ${var.xxx} = inserts the value of a Terraform variable
}
# Doc: https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/subnet
resource "aws_subnet" "public" {
vpc_id = aws_vpc.main.id # Attach this subnet to the VPC created just above
# aws_vpc.main.id = "the ID of the aws_vpc resource named main"
cidr_block = "10.0.1.0/24" # Sub-range of 256 addresses in the VPC
map_public_ip_on_launch = true # Each instance launched in this subnet automatically gets a public IP
availability_zone = "${var.aws_region}a" # Availability zone (e.g.: "eu-west-3a")
tags = { Name = "${var.project_name}-public" }
}
# Doc: https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/internet_gateway
resource "aws_internet_gateway" "gw" {
vpc_id = aws_vpc.main.id # The "front door" that connects the VPC to the Internet
tags = { Name = "${var.project_name}-igw" }
}
# Doc: https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/route_table
resource "aws_route_table" "public" {
vpc_id = aws_vpc.main.id # Route table = the network's "traffic rules"
route {
cidr_block = "0.0.0.0/0" # "All traffic going to the Internet..."
gateway_id = aws_internet_gateway.gw.id # "...goes through the Internet Gateway"
}
tags = { Name = "${var.project_name}-rt" }
}
# Doc: https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/route_table_association
resource "aws_route_table_association" "public" {
subnet_id = aws_subnet.public.id # Associate the route table with the public subnet
route_table_id = aws_route_table.public.id # Without this, the subnet has no route to the Internet
}
# --- Security Group (firewall) ---
# Doc: https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/security_group
resource "aws_security_group" "web" {
name = "${var.project_name}-sg"
vpc_id = aws_vpc.main.id
# ingress = INBOUND traffic rules (who is allowed to access your server)
ingress {
description = "SSH"
from_port = 22 # Start port
to_port = 22 # End port (same value = a single port)
protocol = "tcp" # TCP = reliable protocol (verifies that data arrives)
cidr_blocks = ["0.0.0.0/0"] # From any IP (0.0.0.0/0 = the entire world)
}
ingress {
description = "HTTP"
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
description = "Backend"
from_port = 8000
to_port = 8000
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
# egress = OUTBOUND traffic rules (what your server is allowed to send)
egress {
from_port = 0
to_port = 0
protocol = "-1" # "-1" = all protocols (TCP, UDP, etc.)
cidr_blocks = ["0.0.0.0/0"] # To anywhere -- the server can access the entire Internet
}
tags = { Name = "${var.project_name}-sg" }
}
# --- AMI (automatically fetch the latest Ubuntu 24.04) ---
# "data" = a data source. Unlike "resource" which CREATES something,
# "data" FETCHES information that already exists on AWS.
# Here, we look for the most recent Ubuntu AMI (image) instead of hardcoding its ID.
# Doc: https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/ami
data "aws_ami" "ubuntu" {
most_recent = true # Take the most recent if multiple match
owners = ["099720109477"] # Canonical (the company that publishes Ubuntu) -- this is their AWS ID
filter {
name = "name" # Filter by AMI name
values = ["ubuntu/images/hvm-ssd-gp3/ubuntu-noble-24.04-amd64-server-*"]
# The * at the end = any build date (the AMI is updated regularly)
}
}
# --- EC2 ---
# Doc: https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/instance
resource "aws_instance" "web" {
ami = data.aws_ami.ubuntu.id # The Ubuntu image fetched by the data source above
instance_type = var.instance_type # Instance type (t3.micro = free)
subnet_id = aws_subnet.public.id # Which subnet to launch the instance in
vpc_security_group_ids = [aws_security_group.web.id] # Which firewall to apply (the [] = a list)
key_name = var.key_name # SSH key name to connect
# user_data = a script that runs automatically on the server's first boot
# This is how you automate Docker installation without connecting via SSH
# <<-EOF ... EOF = "heredoc" -- a way to write a long multi-line text
# Everything between <<-EOF and EOF is the script content
user_data = <<-EOF
#!/bin/bash
apt-get update
apt-get install -y docker.io docker-compose-v2 git
usermod -aG docker ubuntu
systemctl enable docker
systemctl start docker
mkdir -p /home/ubuntu/devops-project
cd /home/ubuntu/devops-project
# ${var.github_user} = inserts the value of the "github_user" variable
# This is Terraform's syntax for inserting a variable into text
# (GitHub Actions, by contrast, uses its own double-brace syntax)
# ⚠️ Do NOT write a dollar sign followed by a brace in this block: inside a
# heredoc, Terraform interprets these sequences, even within comments.
git clone https://github.com/${var.github_user}/devops-project.git .
# ⚠️ If your repo is private, git clone will fail.
# Solution: make it public or use a GitHub token in the URL:
# git clone https://[email protected]/user/repo.git .
docker compose up -d --build
EOF
tags = { Name = "${var.project_name}-server" }
}

Create variables.tf:

variable "aws_region" {
default = "eu-west-3"
# ⚠️ Locally (Floci), prefer "us-east-1": the emulator and your awslocal alias
# are on us-east-1. A resource created in eu-west-3 is NOT visible unless you
# add --region eu-west-3 to the awslocal commands.
}
variable "aws_endpoint" {
description = "URL of the local AWS emulator. Leave empty for real AWS."
default = ""
}
variable "project_name" {
default = "devops"
}
variable "instance_type" {
default = "t3.micro"
}
variable "key_name" {
description = "EC2 key pair name"
}
variable "github_user" {
description = "Your GitHub username"
}

Create outputs.tf:

output "public_ip" {
value = aws_instance.web.public_ip
}
output "ssh_command" {
value = "ssh -i ~/devops-key.pem ubuntu@${aws_instance.web.public_ip}"
}
output "app_url" {
value = "http://${aws_instance.web.public_ip}"
}

Passing variables with -var="..." on the command line is tedious and doesn’t version easily. In practice, you use a .tfvars file:

Create terraform.tfvars:

key_name = "devops-key"
github_user = "YOUR_USER"

Terraform automatically loads terraform.tfvars if it exists. Otherwise, you can specify a file:

Fenêtre de terminal
terraform apply -var-file="production.tfvars"

This is how you manage multiple environments: a dev.tfvars, a staging.tfvars, a prod.tfvars, each with different values (instance size, project name, etc.).

⚠️ Don’t commit .tfvars files that contain secrets. Add *.tfvars to .gitignore if needed. Non-sensitive variables (region, instance type) can be committed.

Fenêtre de terminal
terraform init
# Terraform has been successfully initialized!
# Option A — test locally on the emulator (free, no AWS account)
terraform plan -var="aws_endpoint=http://localhost:4566"
terraform apply -var="aws_endpoint=http://localhost:4566"
# Plan: 7 to add, 0 to change, 0 to destroy.
# Apply complete! Resources: 7 added
# Option B — on real AWS (needs your credentials: aws configure)
terraform plan
# Plan: 7 to add, 0 to change, 0 to destroy.
terraform apply
# Apply complete! Resources: 7 added
# Outputs:
# app_url = "http://13.38.x.x"
# public_ip = "13.38.x.x"
# ssh_command = "ssh -i ~/devops-key.pem [email protected]"

⚠️ terraform plan without -var queries real AWS. With no credentials configured, it fails with Error: No valid credential sources found. That’s normal: start with Option A (the Floci emulator), then move to Option B once everything is green.

Wait 2-3 minutes (user_data installs Docker and launches the app), then open the URL.

What you just did by hand in 30 min, Terraform did in 2 min. And you can redo it identically with a single terraform apply.

Fenêtre de terminal
terraform destroy
# Destroy complete! Resources: 7 destroyed.

If you followed the The same code for local AND production section, you can now do this:

Fenêtre de terminal
# Create the infra locally, test it, destroy it -- in 10 seconds
terraform apply -var="aws_endpoint=http://localhost:4566" -auto-approve
terraform destroy -var="aws_endpoint=http://localhost:4566" -auto-approve
# Then the real one, once you're confident
terraform apply

One codebase, two environments. That’s the concrete outcome of this whole module.

Your server is running. But look at what happens when you change the code:

  1. you push to GitHub -> the Module 4 pipeline checks the code, builds the images and pushes them to Docker Hub;
  2. and… nothing. Your server keeps running the old version.

To update it, you reconnect over SSH (ssh -i ~/devops-key.pem ubuntu@...), run git pull, then docker compose up -d --build. By hand. Every time.

The user_data you wrote above only runs on the instance’s first boot — it doesn’t re-run anything when you push new code.

Now you have everything you need to close the loop: a server (this module) and a pipeline (Module 4). All that’s missing is one job: deploy.

💡 Why didn’t we do this in Module 4? Because at that point you didn’t have a server yet. And Floci’s EC2 can’t be the target: Floci does start a real Ubuntu container, but you can’t run Docker inside it (nested storage limitation), and it has no public IP — so a GitHub runner, which runs in the cloud, can’t reach it. Automatic deployment is tested against a real server (Option B above). See Floci / AWS locally.

Open ~/devops-project/.github/workflows/ci.yml and add this job at the end of the file:

# --- Stage 5: Deploy to the server ---
deploy:
name: Deploy
runs-on: ubuntu-latest
needs: push # We only deploy if the image was actually pushed (so if everything is green)
if: github.ref == 'refs/heads/main' && github.event_name == 'push' && vars.DEPLOY_ENABLED == 'true'
environment: production # Can require a manual approval (see below)
steps:
- name: Deploy over SSH to the server
uses: appleboy/ssh-action@v1
with:
host: ${{ secrets.EC2_HOST }}
username: ubuntu
key: ${{ secrets.EC2_SSH_KEY }}
script: |
set -e # Stop the script at the first error
cd ~/devops-project
git pull
docker compose up -d --build
- name: Check that the app responds
run: curl -fsS "http://${{ secrets.EC2_HOST }}/api/health"

Let’s break it down:

LineRole
needs: pushWe only deploy if the delivery succeeded. We don’t deploy a version whose tests failed
if: ... vars.DEPLOY_ENABLED == 'true'A switch: until you create the variable, the job is skipped and your pipeline stays green
environment: productionAttaches the job to a protected environment -> you can require a manual approval before it starts
appleboy/ssh-action@v1An action that opens an SSH connection to your server and runs the script there
set -eWithout it, a failed git pull wouldn’t stop the rest: the job would show “green” while nothing was updated
git pull + docker compose up -d --buildPulls the new code and restarts the app
curl ... /api/healthChecks that the app responds after the deployment — otherwise the job fails and you know right away

The job assumes the repository is already cloned on the server in ~/devops-project — that’s what your user_data did on first boot.

In your GitHub repo’s Settings -> Secrets and variables -> Actions:

TypeNameValue
SecretEC2_HOSTYour server’s public IP — the public_ip output of terraform output
SecretEC2_SSH_KEYThe full contents of your private key: cat ~/devops-key.pem (including the -----BEGIN/-----END lines)
VariableDEPLOY_ENABLEDtrue

⚠️ Don’t mix up the tabs: DEPLOY_ENABLED is a variable (the Variables tab), not a secret. It’s what flips the switch in the if:.

Fenêtre de terminal
cd ~/devops-project
# Change the <h1> in frontend/src/App.jsx for example
git add .
git commit -m "feat: new title"
git push

Go to the Actions tab: Lint -> Test -> ... -> Push -> Deploy. When it’s green, reload http://$YOUR_IP: your change is live, and you never went through SSH. 🎉

By default, deploy runs on its own as soon as the pipeline is green. On a team, you often want a human to validate before touching production. That’s what environment is for:

Settings -> Environments -> New environment -> production -> check Required reviewers and add yourself. Now the deploy job waits for your click. That’s exactly the Delivery (manual button) / Deployment (automatic) difference from Module 4.

Fenêtre de terminal
# Revert the faulty commit and push again -> the pipeline redeploys the previous version
git revert <commit_sha>
git push

That’s why we keep a clean Git history: rolling back is just going back to a commit.

💡 Going further: deploy the exact image built by CI. Here, the server rebuilds the image (--build): it deploys your code, not the image the CI tested. The holy grail of CD is “build once, run the same artifact”: add image: <your_dockerhub_user>/devops-backend:latest to the services in docker-compose.yml, then on the server run docker compose pull && docker compose up -d --no-build. The server then runs exactly the image the CI validated. We keep the --build version here, as it’s simpler to understand when you’re starting out.

Q: What is Terraform? A: An Infrastructure as Code tool. You describe your infra in HCL files, Terraform creates/modifies/deletes it. Versionable, reproducible, collaborative.

Q: What is Infrastructure as Code? A: Managing infrastructure (servers, networks, databases) via code instead of manual clicks. Benefits: reproducible, versioned, auditable.

Q: Explain plan, apply, destroy. A: plan shows what will change without doing anything. apply executes the changes. destroy deletes everything. You always do plan before apply to verify.

Q: What is the state file? A: A JSON file that records the current state of the infrastructure managed by Terraform. It allows comparing the real state with the code to know what to create/modify/delete.

Q: Why not commit the state file? A: It can contain secrets (passwords, keys). You store it on a remote backend (S3 + DynamoDB for locking).

Q: Terraform vs CloudFormation? A: Terraform is multi-cloud (AWS, GCP, Azure). CloudFormation is AWS-specific. Terraform has a larger community and more readable syntax.

Q: What is a Terraform module? A: A reusable block of Terraform code. Instead of copy-pasting the same config for each environment, you create a module and call it with different parameters. It’s like a function in programming.

Q: What is a Terraform provider? A: A plugin that connects Terraform to a service (AWS, GCP, Azure, GitHub…). The AWS provider allows Terraform to create EC2s, S3 buckets, RDS instances. Without a provider, Terraform can’t talk to anything.

  • Always plan before apply. Read the plan. Check what will be destroyed. An accidental destroy of a production database happens.
  • Remote state from day 1. In a team, local state is a nightmare (conflicts, data loss). Use an S3 backend + DynamoDB for locking.
  • One .tfvars per environment. dev.tfvars, staging.tfvars, prod.tfvars. Same code, different values.
  • Don’t commit the state or secrets. .gitignore should contain *.tfstate, *.tfstate.backup, .terraform/. .tfvars with secrets too.
  • Format your code. terraform fmt before each commit. It’s the equivalent of a linter for Terraform.
  • Name your resources consistently. ${var.project_name}-${var.environment}-resource. Example: devops-prod-sg. When you have 100 resources in the AWS console, names are the only way to find your way around.
  • No manual modifications. If someone modifies the infra in the AWS console, the next terraform apply will overwrite their changes. Everything goes through code.
  • Forgetting terraform init -> “Provider not found”. You need to init for every new project or after adding a provider.
  • Modifying the state file by hand -> It breaks everything. Use terraform state if needed.
  • Committing terraform.tfstate -> Add *.tfstate to .gitignore.
  • Forgetting to destroy after testing -> Unexpected AWS cost.
  • Hardcoding values -> Use variables for everything that changes between environments.
  • Passing variables with -var on every command -> Use a .tfvars file, it’s cleaner and more reproducible.
  • Modules: writing reusable modules — essential once your Terraform code exceeds 200 lines
  • Import: terraform import to import manually created resources into the state — you’ll need this when taking over an existing infrastructure
  • Workspaces: managing multiple environments (dev, staging, prod) with the same Terraform code
  • Terragrunt: a wrapper for managing Terraform at scale — useful when you have 20+ modules and 5+ environments
  • You can explain what Infrastructure as Code is, and what it brings
  • You know the 4 commands: init, plan, apply, destroy
  • You know what the state is, and why you never commit it to Git
  • You’ve run the apply/plan/destroy loop locally, and can explain the difference between “modify in place” and “destroy and recreate”
  • You’ve put your state on an S3 backend and verified there’s no local file left
  • You know how to point the AWS provider at a local endpoint, and why the skip_* flags are needed
  • You’ve recreated the Module 5 infrastructure in Terraform
  • You’ve wired the Module 4 pipeline to your server: a git push updates the app on its own
  • You know why this automatic deployment can’t be tested on Floci’s EC2
  • You’ve run terraform destroy on real AWS to avoid costs