This project deploys a scalable web application infrastructure on AWS using Terraform. It includes key AWS services and uses LocalStack for local testing to avoid AWS costs during development. A GitHub Actions pipeline automates deployment to dev, staging, and prod environments.
The project sets up:
- VPC: Custom network with public and private subnets.
- EC2 Instances: Run NGINX web servers (using Docker NGINX image).
- Application Load Balancer (ALB): Distributes traffic to EC2 instances.
- Auto Scaling Group: Scales EC2 instances based on CPU utilization.
- SQS: Queues messages for asynchronous processing.
- Lambda: Processes SQS messages, e.g., writing to DynamoDB.
- DynamoDB: Stores data processed by Lambda.
- SNS: Sends notifications for scaling events or errors.
- IAM Roles/Policies: Secures service access.
This project covers DVA-C02 exam topics, including:
- EC2, ALB, Auto Scaling
- SQS, Lambda, DynamoDB, SNS
- IAM, VPC
- Infrastructure as Code with Terraform
- CI/CD with GitHub Actions
- Terraform (>= 1.5.0)
- AWS CLI (>= 2.0)
- LocalStack (for local testing)
- awslocal (Wrapper around AWS CLI for local testing with LocalStack)
- Docker (for LocalStack and NGINX)
- Node.js (for Lambda function packaging)
- GitHub Account (for CI/CD pipeline)
- AWS Account (for staging/prod deployment)
git clone https://github.com/MartinLupa/infrascale.git
cd infrascaleinfrascale/
├── terraform/
│ ├── main.tf # Core infrastructure
│ ├── variables.tf # Environment variables
│ ├── outputs.tf # Output values
│ ├── dev.tfvars # Dev environment config
│ ├── staging.tfvars # Staging environment config
│ ├── prod.tfvars # Prod environment config
├── lambda/
│ ├── index.js # Lambda function code
├── .github/
│ ├── workflows/
│ │ ├── deploy.yml # GitHub Actions pipeline
├── README.md
Ensure LocalStack is running:
localstack statusSet environment variables for Terraform to use LocalStack:
export AWS_ACCESS_KEY_ID=test
export AWS_SECRET_ACCESS_KEY=test
export AWS_DEFAULT_REGION=us-east-1
export TF_VAR_localstack_enabled=truecd terraform
terraform initterraform apply -var-file=dev.tfvarsAccess the NGINX web server at http://localhost:4566 (LocalStack endpoint).
Update dev.tfvars, staging.tfvars, or prod.tfvars with your AWS account details. Disable LocalStack:
export TF_VAR_localstack_enabled=falseApply for a specific environment:
terraform apply -var-file=staging.tfvarsThe pipeline (deploy.yml) automates deployment to dev, staging, and prod based on branch pushes:
- dev:
devbranch - staging:
stagingbranch - prod:
mainbranch
Setup:
- Add AWS credentials to GitHub Secrets:
AWS_ACCESS_KEY_IDAWS_SECRET_ACCESS_KEY
- Push changes to trigger the pipeline:
git push origin dev
Pipeline Snippet:
name: Deploy Infrastructure
on:
push:
branches:
- dev
- staging
- main
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: hashicorp/setup-terraform@v2
- run: terraform init
- run: terraform apply -var-file=${{ github.ref_name }}.tfvars -auto-approve
env:
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}- LocalStack: Check NGINX at
http://localhost:4566. - AWS: Get ALB DNS from Terraform outputs:
terraform output alb_dns_name
- SQS/Lambda: Send a message to SQS and verify Lambda updates DynamoDB.
Destroy resources to avoid AWS charges:
terraform destroy -var-file=dev.tfvarsmain.tf (simplified):
provider "aws" {
region = var.region
endpoints {
ec2 = var.localstack_enabled ? "http://localhost:4566" : null
}
}
resource "aws_vpc" "main" {
cidr_block = "10.0.0.0/16"
}
resource "aws_instance" "web" {
ami = "ami-12345678" # Update with valid AMI
instance_type = "t2.micro"
user_data = <<-EOF
#!/bin/bash
docker run -d -p 80:80 nginx
EOF
}- Replace placeholder AMIs and configurations in
.tfvarsfiles. - Ensure LocalStack is running for local testing.
- Monitor AWS costs for staging/prod environments.
- Refer to DVA-C02 Exam Guide for additional topics.
For issues, open a GitHub issue or check Terraform AWS Provider Docs.