Skip to content
EgyKode
04 · Infrastructure as CodeLab 27 / 59
Guided labterraform

Jenkins EC2 Instance, S3 Backend & AWS Backup Vault

Move Terraform state off your laptop into locked remote storage, and put the build server under a backup plan.

Time
23 min
Level
Intermediate
Objectives
4 objectives
Cost
Low cost

Where this fits in the platform

Before you start

CostLow cost

— a `t3.micro` EC2 instance and S3 storage are inside the 12-month allowance. Outside it, expect ~$8/month for the instance if left running. AWS Backup charges for stored recovery points.

How to clean up

The scenario#

Terraform state is a file on one laptop. Two people ran apply at the same time last week and the state now disagrees with reality in ways nobody has fully mapped.

The Jenkins server has no backups and its public IP changes every time it is stopped, which breaks every webhook.

Hands-on environment

Run it on AWS

This lab builds real cloud infrastructure, so it needs your own AWS account. Follow the cost and cleanup notes above — the resources are yours, and so is the bill.

Anything you tick here is your own record. EgyKode cannot see inside that terminal, so the success criteria stay self-assessed even when the environment checks your work for you.

The backend, created locally

Step 1 of 4

What you are building#

The bootstrap problem first: the backend that stores state cannot itself be created by the configuration that uses it. So this is two stages, and that is not an accident of tooling.

text
  stage 1  →  create the bucket + lock table with LOCAL state
  stage 2  →  every other stack uses them as a remote backend

Verify it worked#

Terminal
# State is remote, and locking works
terraform state list | head
terraform plan &                       # hold a lock
terraform plan                         # must report: Error acquiring the state lock
wait
 
# A deleted state file is recoverable
aws s3api list-object-versions --bucket <state-bucket> --prefix production/terraform.tfstate \
  --query 'Versions[].[VersionId,LastModified]' --output table
 
# The address survives a stop/start
aws ec2 stop-instances --instance-ids <id> && aws ec2 wait instance-stopped --instance-ids <id>
aws ec2 start-instances --instance-ids <id> && aws ec2 wait instance-running --instance-ids <id>
aws ec2 describe-addresses --query 'Addresses[].[PublicIp,InstanceId]' --output table
 
# A recovery point actually exists — not just a plan that says it will
aws backup list-recovery-points-by-backup-vault --backup-vault-name platform \
  --query 'RecoveryPoints[].[CreationDate,Status,ResourceArn]' --output table

That last one is the difference between "backups are configured" and "backups happened". A plan with no recovery points is a plan that has never run, and you find that out either now or during a restore.


Clean up#

DestructiveThis removes real resources. Check which environment you are in first.

Terminal
# Backups outlive the instance — delete recovery points first
aws backup list-recovery-points-by-backup-vault --backup-vault-name platform \
  --query 'RecoveryPoints[].RecoveryPointArn' --output text
terraform destroy -auto-approve
aws s3 rm s3://<state-bucket> --recursive        # only when finished for good

Cost of this lab: Billable. A t3.medium is about $0.042/hour (~$30/month). S3, DynamoDB on-demand and the EIP while attached are cents. Recovery points bill until deleted and survive terraform destroy.

Success criteria

0 of 4

The concept behind it

Ready to try it without help?Do the challenge

Next up

Lab 27 of 59 on the project path

Terraform Validation, Linting & CIBuild the gate that runs before every apply: format, validate, lint, scan, and a plan a human approves.50 minIntermediate

Previous: Amazon RDS PostgreSQL & AWS Secrets Manager Integration