Examples for provisioning AWS with Terraform using simple environment layouts.
If this repo helps you, please ⭐ it.
- Prerequisites
- Init with S3 Backend
- VPC Provisioning
- RDS Provisioning
- ALB + ASG Provisioning
- EC2 Provisioning
- AWS Organization Tag Policy
- Terraform Command Reference
- Notes
- Terraform installed
- AWS credentials configured (e.g.,
aws configure) - S3 bucket and DynamoDB table for remote state and locking (if using the backend example)
Use this when your workspace uses an S3 remote backend with DynamoDB locking.
terraform init \
-backend-config="key=dev/vpc.tfstate" \
-backend-config="bucket=dcube-terraform-state" \
-backend-config="region=us-west-2" \
-backend-config="dynamodb_table=terraform-state-lock" \
-var-file=../../../vars/dev/vpc.tfvarsAdjust
key,bucket,region, anddynamodb_tableto your setup.
From environments/dev/vpc:
# 1) Initialize
terraform init
# 2) Preview
terraform plan -var-file=../../../vars/dev/vpc.tfvars
# 3) Apply
terraform apply -var-file=../../../vars/dev/vpc.tfvars
# 4) Destroy (when needed)
terraform destroy -var-file=../../../vars/dev/vpc.tfvarsFrom environments/dev/rds:
terraform init
terraform plan -var-file=../../../vars/dev/rds.tfvars
terraform apply -var-file=../../../vars/dev/rds.tfvars
terraform destroy -var-file=../../../vars/dev/rds.tfvarsFrom environments/dev/alb-asg:
terraform init
terraform plan -var-file=../../../vars/dev/alb-asg.tfvars
terraform apply -var-file=../../../vars/dev/alb-asg.tfvars
terraform destroy -var-file=../../../vars/dev/alb-asg.tfvarsFrom environments/dev/ec2 (edit vars/dev/ec2.tfvars first):
terraform init
terraform plan -var-file=../../../vars/dev/ec2.tfvars
terraform apply -var-file=../../../vars/dev/ec2.tfvars
terraform destroy -var-file=../../../vars/dev/ec2.tfvarsAlways review the plan before applying.
From environments/dev/tag-policy (edit vars/dev/tag-policy.tfvars as needed):
terraform init
terraform plan -var-file=../../../vars/dev/tag-policy.tfvars
terraform apply -var-file=../../../vars/dev/tag-policy.tfvars
terraform destroy -var-file=../../../vars/dev/tag-policy.tfvarsThis section covers deploying an Amazon EKS cluster with Karpenter for dynamic node provisioning.
- AWS CLI configured with necessary permissions
kubectlinstalled- AWS IAM permissions for EKS and Karpenter operations
From infra/eks-cluster:
# Initialize with backend config
terraform init
# Plan and review changes
terraform plan -var-file=../../vars/dev/eks.tfvars
# Apply the configuration
terraform apply -var-file=../../vars/dev/eks.tfvarsFrom infra/eks-karpenter:
# Initialize with backend config
terraform init
# Plan and review changes
terraform plan -var-file=../../vars/dev/karpenter.tfvars
# Apply the configuration
terraform apply -var-file=../../vars/dev/karpenter.tfvarsAfter deployment:
# Update kubeconfig
aws eks update-kubeconfig --region <your-region> --name <cluster-name>
# Verify Karpenter pods
kubectl get pods -n karpenterRefresh state (re-reads remote objects and updates outputs):
terraform refreshShow current state and outputs:
terraform show- Run Terraform inside an environment folder (e.g.,
environments/dev/vpc). - Variable files live under
vars/dev/*.tfvars. - Backend config values in examples are placeholders—change them to match your infra.