Welcome to Terraform Infrastructure as Code Lab! π
Infrastructure as Code (IaC) is a key practice in DevOps, enabling teams to manage and provision infrastructure through code rather than manual processes. In this lab, you'll use HashiCorp Terraform to deploy a Multi-AZ AWS VPC environment.
Declarative Layout
Define the 'what', not the 'how'. Terraform handles the complex provisioning steps automatically.
State Management
Terraform keeps track of your infrastructure state to ensure consistency and enable updates.
AWS Best Practices
Deploy multi-AZ networking with isolation between public and private resources.
π Learning Objectives
- Understand HCL (HashiCorp Configuration Language) syntax and file structure.
- Configure AWS Providers and resource dependencies.
- Implement VPC, Subnets, Internet Gateway, and NAT Gateway.
- Master the Terraform workflow: Init, Plan, Apply, and Destroy.
- Learn state management and troubleshooting principles.
- Estimated Time: 90 - 120 Minutes
- Difficulty: Intermediate
- Resources: ~15 AWS Resources
ποΈ Multi-AZ VPC Architecture
Designing for high availability and security requires a multi-AZ deployment with segregated networking tiers. Below is the architecture you will implement today.
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β AWS Cloud (us-east-1) β β β β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β β β VPC: 10.0.0.0/16 β β β β (terraform-lab-vpc) β β β β β β β β ββββββββββββββββββββββββββββ ββββββββββββββββββββββββββββ β β β β β Availability Zone A β β Availability Zone B β β β β β β β β β β β β β β ββββββββββββββββββββββ β β ββββββββββββββββββββββ β β β β β β β Public Subnet β β β β Public Subnet β β β β β β β β 10.0.1.0/24 β β β β 10.0.2.0/24 β β β β β β β ββββββββββ¬ββββββββββββ β β ββββββββββ¬ββββββββββββ β β β β β β β β β β β β β β β β ββββββββββ΄ββββββββββββ β β ββββββββββ΄ββββββββββββ β β β β β β β Private Subnet β β β β Private Subnet β β β β β β β β 10.0.11.0/24 β β β β 10.0.12.0/24 β β β β β β β ββββββββββββββββββββββ β β ββββββββββββββββββββββ β β β β β ββββββββββββββββββββββββββββ ββββββββββββββββββββββββββββ β β β β β β β β βββββββββββββββββββββββ ββββββββββββββββββββββββ β β β β β Internet Gateway β β NAT Gateway β β β β β β (igw) ββββββββββΆβ (nat-gw) β β β β β βββββββββββββββββββββββ ββββββββββββββββββββββββ β β β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
Resource Inventory
| Resource | Purpose | Configuration |
|---|---|---|
| VPC | Isolated Cloud Network | 10.0.0.0/16, DNS Support Enabled |
| Internet Gateway | External Connectivity | Attached to VPC |
| Public Subnets | DMZ Tier (Web/ALB) | Map Public IP on Launch, 2 AZs |
| Private Subnets | Application/Data Tier | No Direct Inbound, 2 AZs |
| NAT Gateway | Outbound for Private | 1x Elastic IP required |
π Environment Setup
Ensure you have the following installed before proceeding:
- Terraform CLI (version 1.0+)
- AWS account with IAM permissions
- AWS CLI configured or Env variables set
1. Install Terraform
Run the following command to verify your installation:
terraform version
2. AWS Credentials
Setting up your credentials as environment variables is the most common practice:
export AWS_ACCESS_KEY_ID="your_access_key" export AWS_SECRET_ACCESS_KEY="your_secret_key" export AWS_DEFAULT_REGION="us-east-1"
βοΈ Configuration Breakdown
Terraform uses HCL files to define the desired state of your infrastructure. Select a file to review its purpose and code.
Loading code...
π The Terraform Workflow
Deploying infrastructure follows a standard lifecycle. Use these commands in sequence.
Initialization
Downloads providers and initializes state storage.
terraform init
Plan (Dry Run)
Compares desired state with current state to show proposed changes.
terraform plan
Apply (Execute)
Executes the plan to create or modify resources.
terraform apply -auto-approve
terraform destroy -auto-approve
β Verification Steps
After a successful apply, use these steps to verify your infrastructure.
Terraform Outputs
Review the calculated values generated by Terraform:
terraform output
AWS CLI Inspection
Confirm the VPC exists in your account:
aws ec2 describe-vpcs --filters "Name=tag:Name,Values=terraform-lab-vpc"
Resource Checklist
- β VPC created with 10.0.0.0/16 CIDR.
- β 4 Subnets correctly tagged (Public 1/2, Private 1/2).
- β NAT Gateway has an Elastic IP assigned.
- β Public Route Table has a default route to IGW.
π§ Common Issues
Troubleshooting Terraform often involves separating configuration errors from provider/cloud errors.
| Error Category | Possible Cause | Resolution |
|---|---|---|
| Auth Errors | Invalid AWS Credentials | Run aws sts get-caller-identity |
| Provider Errors | Version mismatch | Run terraform init -upgrade |
| Resource Conflict | Duplicate naming or overlapping CIDRs | Check variables.tf values |
| State Lock | Interrupted apply |
Wait or manually release lock via UI/CLI |
π‘ Pro Debugging Tip
Enable verbose logging by setting the TF_LOG environment variable to
DEBUG before running commands to see exact API requests and responses.
π§ Knowledge Check
Test your understanding of Terraform and AWS networking basics.