Hands-on project
Build reusable modules:
Modules
├── VPC Module
├── EC2 Module
└── S3 Module
Wire them together with:
- Local and remote state (S3 backend)
- Workspaces for
devandprod - Variables, outputs, and resource references from Days 1–2
Optional stretch goals as you grow:
- Conditionals (
count/ ternary expressions) - Built-in functions
- Loops (
for_each/for)
Assignment 1 — EC2 module
Create an ec2 module and launch 2 EC2 instances from it (two module blocks, or one module that accepts a list — your choice).
Assignment 2 — S3 backend
Create an S3 bucket for state and configure:
backend "s3" { ... }Migrate state with:
terraform init -migrate-stateConfirm the .tfstate object appears in the bucket.
Assignment 3 — Workspaces
terraform workspace new dev
terraform workspace new prodDeploy separate infrastructure in each workspace (for example different Name tags via ${terraform.workspace}).
Verify both environments exist at the same time.
Clean up
Destroy each workspace before deleting it:
terraform workspace select prod
terraform destroy
terraform workspace select dev
terraform destroy
terraform workspace select default
terraform workspace delete prod
terraform workspace delete devDay 3 summary
Terraform modules
↓
Module structure
↓
Terraform state
↓
State commands
↓
Remote state (S3)
↓
Terraform workspaces
↓
Multi-environment deployment