This project implements a scalable web server infrastructure using AWS CDK (Cloud Development Kit) that automatically adjusts capacity based on demand. It creates a secure, highly available web hosting environment with automated scaling capabilities and performance monitoring.
The infrastructure deploys a web server environment in AWS using Infrastructure as Code principles. It provisions a VPC with public subnets, configures security groups, sets up an Auto Scaling group with customizable capacity, and implements CPU-based scaling policies. The web servers are automatically configured with Apache and PHP, and deploy a sample web application from an S3 bucket.
.
├── app.py # Main CDK application entry point
├── hawa_poc/ # Core infrastructure code
│ └── hawa_poc_stack.py # Main stack definition with AWS resources
├── tests/ # Test directory
│ └── unit/ # Unit tests for the stack
├── cdk.json # CDK configuration and context
├── requirements.txt # Production dependencies
├── requirements-dev.txt # Development dependencies
└── source.bat # Windows virtual environment activation script
- Python 3.6 or later
- AWS CLI configured with appropriate credentials
- AWS CDK CLI installed (
npm install -g aws-cdk) - An AWS account with necessary permissions
- An EC2 key pair for SSH access
- Clone the repository and create a virtual environment:
# Linux/MacOS
python3 -m venv .venv
source .venv/bin/activate
# Windows
python -m venv .venv
source.bat- Install dependencies:
pip install -r requirements.txt
pip install -r requirements-dev.txt- Bootstrap CDK in your AWS account (if not already done):
cdk bootstrap- Deploy the stack:
cdk deploy --parameters KeyPairName=your-key-pair-name- The deployment will create:
- A VPC with public subnets
- Security groups for web traffic
- An Auto Scaling group of EC2 instances
- Scaling policies based on CPU utilization
- Customize the deployment with different instance types:
# In hawa_poc_stack.py
instance_type=ec2.InstanceType.of(ec2.InstanceClass.T3, ec2.InstanceSize.SMALL)- Modify scaling thresholds:
asg.scale_on_cpu_utilization(
"CpuScaling",
target_utilization_percent=70, # Change from default 50%
estimated_instance_warmup=Duration.seconds(180)
)Common Issues:
- Deployment Failure
- Error: "VPC limit exceeded"
- Solution: Check AWS VPC limits in your account
- Run:
aws service-quotas get-service-quota --service-code ec2 --quota-code L-F678F1CE
- Instance Launch Failures
- Check EC2 instance launch logs:
aws ec2 get-console-output --instance-id <instance-id>
- Verify security group settings in AWS Console
- Auto Scaling Issues
- Enable detailed monitoring:
aws autoscaling describe-auto-scaling-groups --auto-scaling-group-name <asg-name>
- Check CloudWatch metrics for CPU utilization
The infrastructure processes web requests through a multi-tier architecture, scaling automatically based on CPU utilization metrics.
Internet --> Security Group --> Auto Scaling Group --> EC2 Instances
^
|
CloudWatch Metrics
Component Interactions:
- Security Group controls inbound HTTP traffic on port 80
- Auto Scaling Group monitors instance health and CPU metrics
- CloudWatch collects and processes metrics every minute
- Scaling policies adjust capacity based on CPU utilization
- EC2 instances run Apache web server with PHP support
- User data script configures web server and deploys application
- Instances launch in public subnets with internet access
- VPC with 2 Availability Zones
- Public subnets with internet connectivity
- Security group allowing HTTP traffic
- Launch Template:
- Amazon Linux 2 AMI
- t2.micro instance type
- 8GB EBS volume
- User data for web server setup
- Auto Scaling Group:
- Min: 1 instance
- Max: 3 instances
- Health check grace period: 60 seconds
- Scaling Policies:
- Target tracking: 50% CPU utilization
- Step scaling for optimized availability
- Detailed instance monitoring enabled
- CloudWatch metrics for CPU utilization
- Auto Scaling group metrics
- Prerequisites:
- AWS credentials configured
- CDK bootstrapped in target account
- EC2 key pair created
- Deployment Steps:
cdk deploy --parameters KeyPairName=your-key-pair-name- Environment Variables:
- CDK_DEFAULT_ACCOUNT: AWS account ID
- CDK_DEFAULT_REGION: Target AWS region
- Monitoring:
- Access CloudWatch dashboards for metrics
- Check Auto Scaling group activity
- Monitor EC2 instance health