ject: Scalable Web Application with Serverless Backend, Docker, CI/CD, and Mu
This document provides a detailed guide for building a scalable web application using AWS Cloud
services. The project includes the setup of VPC, EC2 instances with Docker containers, serverless
backend with AWS Lambda, CI/CD pipeline with AWS CodePipeline, multi-region deployment, and
advanced security features such as AWS WAF and Shield.
1. Set up AWS Environment and VPC Configuration
1.1 Create a VPC:
- Go to the VPC Dashboard in the AWS Console.
- Click 'Create VPC' and choose the following:
- IPv4 CIDR block: 10.0.0.0/16
- Tenancy: Default
- Name tag: MyVPC
1.2 Create Subnets:
- Create Public Subnet with CIDR block 10.0.1.0/24.
- Create Private Subnet with CIDR block 10.0.2.0/24.
1.3 Configure Internet Gateway:
- Go to the Internet Gateways section and create an Internet Gateway.
- Attach it to the VPC.
1.4 Set up Route Tables:
- Create a route table for the public subnet and add a route to the Internet Gateway.
2. Set up EC2 with Docker for Backend Services
2.1 Launch EC2 Instance:
- Go to EC2 Dashboard, click 'Launch Instance'.
- Select Amazon Linux 2 or Ubuntu AMI and instance type t2.micro.
- Configure instance to use the public subnet.
2.2 Install Docker on EC2:
- SSH into your EC2 instance and run:
sudo yum install docker -y
sudo service docker start
sudo usermod -a -G docker ec2-user
2.3 Deploy Dockerized Application:
- Create a Dockerfile for your backend.
- Build and run the Docker container using:
docker build -t your-backend .
docker run -d -p 80:80 your-backend
3. Set up Serverless Backend with AWS Lambda
3.1 Create Lambda Function:
- Go to AWS Lambda, click 'Create function'.
- Choose 'Node.js 14.x' runtime and create an execution role.
3.2 Write Lambda Code:
- Write your function code in the Lambda console.
exports.handler = async (event) => {
const response = {
statusCode: 200,
body: JSON.stringify('Hello from Lambda!'),
};
return response;
};
3.3 Set Up API Gateway:
- Go to API Gateway, create a REST API and add methods for routing to Lambda.
4. Implement CI/CD with AWS CodePipeline
4.1 Set up GitHub Repository:
- Push your application code to GitHub.
4.2 Create CodePipeline:
- In AWS Console, go to CodePipeline and create a new pipeline.
- Connect to GitHub as the source and create a buildspec.yml file in your repository.
4.3 CodeDeploy Setup:
- Create a CodeDeploy configuration to deploy Docker containers to EC2 or ECS.
buildspec.yml example:
version: 0.2
phases:
install:
commands:
- docker build -t your-backend .
build:
commands:
- docker push your-repository-url/your-backend
5. Multi-Region Deployment
5.1 Deploy in Multiple Regions:
- Deploy EC2 instances in different regions (e.g., US East, EU West).
5.2 Set up Route 53 for Traffic Distribution:
- Go to Route 53, create a hosted zone for your domain.
- Use Geo-proximity or Latency-based routing for multi-region traffic management.
6. Security with AWS WAF, Shield, and IAM
6.1 Set up AWS WAF:
- Create a Web ACL and add rules for SQL Injection and IP Blocking.
- Attach the Web ACL to API Gateway.
6.2 Enable AWS Shield:
- Shield is automatically enabled, but Shield Advanced can be configured for extra protection.
6.3 Set up IAM Roles:
- Create IAM roles for Lambda functions with the minimum necessary permissions.