LEARNING PATH  ›  Terraform AdvancedLEVEL 03 / ADVANCED   ·   ~3–5 HOURS
●  AUTOMATE INFRASTRUCTURE DELIVERY

Build a modular cloud environment with CI/CD.

Bring reusable Terraform modules, automated checks, secure credentials and approval-gated deployments together in a controlled infrastructure delivery workflow.

TERRAFORM
Terraform modulesCI/CD pipelineAWS environmentPolicy checks
03Reusable modules
08Pipeline stages
Dev → ProdControlled promotion

Understand the delivery architecture

Terraform code is reviewed and tested before a protected pipeline applies approved changes to AWS.

Developer
Git commit / pull request
→
Git repository
Versioned Terraform code
→
CI pipeline
Format · Validate · Test
Security checks
Scan · Policy · Review
→
Plan artifact
Reviewed change proposal
→
Approval gate
Authorized promotion
↓
AWS CLOUD ENVIRONMENT
Network moduleVPC · subnets · routing
Workload moduleEC2 · security group
Remote stateS3 · state locking
Separate environment state and access boundaries for development and production
Learning environment only. This site explains the workflow and provides starter code. It does not execute Terraform, connect to GitHub or AWS, or run a CI/CD pipeline. You must configure your repository, cloud account, secrets and pipeline permissions yourself.

Before you start

Security: Do not commit AWS access keys, private keys, state files or secret values. Prefer short-lived OIDC role sessions. Restrict who can approve production deployments.

Organize the project

terraform-advanced-cicd/
├── modules/
│   ├── network/       # VPC, subnet, routes, gateway
│   └── workload/      # security group and EC2 instance
├── environments/
│   ├── dev/           # root module + dev backend
│   └── prod/          # isolated prod root + backend
├── .github/workflows/
│   └── terraform.yml  # CI checks and gated deployment
└── README.md

Each environment is a separate root configuration with its own backend key and variable values. Reusable modules contain resource definitions but do not configure the backend.

Explore the Terraform modules

Review the network and workload modules, then see how an environment root calls them.

Implementation note: This starter workload uses an Amazon Linux AMI lookup and a small EC2 instance. Review AMI availability, instance pricing, security group ingress and account quotas for your chosen region before applying.

Build the CI/CD workflow

A safe pipeline separates validation and planning from the protected apply stage.

01 · CheckoutFetch the reviewed commit.
02 · SetupInstall pinned Terraform version.
03 · FormatRun terraform fmt -check.
04 · ValidateInitialize and validate config.
05 · Security scanRun IaC scanning and policy checks.
06 · PlanGenerate and retain a reviewed plan.
07 · ApprovalProtected environment reviewer gate.
08 · ApplyApply only the approved plan.
For a real production workflow, bind the AWS role to the expected repository, branch and environment using OIDC trust conditions. Pin third-party actions to verified commit SHAs. Ensure the saved plan is tied to the same commit and configuration that passed review.

Checks to include before deployment

Terraform quality
  • Formatting and validation
  • Reusable module input validation
  • Plan review and drift checks
Security controls
  • Static infrastructure scanning
  • Least-privilege IAM and OIDC
  • Encrypted, private remote state
Release governance
  • Protected branches and reviews
  • Approval before production apply
  • Deployment logs and rollback plan

Run the workflow locally first

Before connecting automation, confirm the development environment works from your terminal.

LOCAL DEVELOPMENT COMMANDS
cd environments/dev
terraform init
terraform fmt -recursive
terraform validate
terraform plan -out=tfplan
terraform show tfplan
# Apply only after reviewing the plan:
terraform apply tfplan
CLEANUP
terraform plan -destroy
terraform destroy
Cost and safety: EC2, networking and storage can incur charges. Do not apply this starter in a production account. The sample pipeline is a teaching pattern, not a complete production-hardened deployment.

Test your understanding

YOU'VE REACHED THE ADVANCED LEVEL

From writing infrastructure to operating it safely.

Next, extend the lab with multiple Availability Zones, private workload placement, centralized logging, policy-as-code, automated tests and controlled promotion across accounts.