By using AWS re:Post, you agree to the AWS re:Post Terms of Use

Deploy a three tier web application using Code pipeline ( EC2 as well as with ECS)

0

Hi,

Am new to aws services. Unfortunately my project manager gave me a task to do the below, can any on please give me a steps and clear understandings.

Deploy a three tier web application using Code pipeline ( EC2 as well as with ECS)

Thanks, MahendraKumar V

3 Answers
1

Hello check these steps to complete the task

Here's a simplified breakdown of deploying a 3-tier web application on AWS using CodePipeline with both EC2 and ECS options:

Preparation:

Code Repository: Store your application code (web tier, app tier, database tier) in a version control system like Git. ECR Repository: Create an Amazon ECR repository to store Docker images for your application components.

CodePipeline Stages:

Source: Connect CodePipeline to your Git repository to trigger deployments on code changes.

Build (EC2): Use CodeBuild with a buildspec.yml file to: Build and package your application code for each tier. Create Docker images for each tier (if using).

Deploy the application on EC2 instances using tools like CodeDeploy. Build (ECS): (Similar to EC2 Build but with ECS specifics)

Use CodeBuild to build Docker images. Define ECS Cluster, Tasks, and Services to run containerized application on ECS. Approval (Optional): Add a manual approval stage for human review before deployment. Deploy: Configure CodeDeploy to deploy the application to the chosen environment (EC2 instances or ECS cluster).

Considerations:

EC2 deployment is simpler but less scalable. ECS offers better scalability and containerization benefits. Secure your deployments with IAM roles and permissions.

profile picture
EXPERT
answered 5 months ago
1

Hello,

Deploying a Three-Tier Web Application on AWS with CodePipeline:

  • A three-tier web application with separate presentation, application, and data tiers.
  • Familiarity with AWS services like EC2, ECS, CodePipeline, CodeBuild, CodeDeploy, and RDS.

Choosing Your Deployment Method:

EC2: Suitable for simpler applications or those requiring direct control over underlying infrastructure.

ECS: Ideal for containerized applications or those aiming for greater scalability and flexibility. Deployment Steps (Choose EC2 or ECS):

1. VPC and Subnets:

  • Create a Virtual Private Cloud (VPC) to isolate your application resources.
  • Define two public subnets for the presentation tier and application tier within the VPC.
  • Create a private subnet for the database instance (optional for EC2, mandatory for ECS).

2. Security Groups:

Presentation Tier Security Group (SG1):

  • Inbound: Allow HTTP/HTTPS traffic from the internet (0.0.0.0/0) on port 80/443.
  • Outbound: Allow all outbound traffic.

Application Tier Security Group (SG2):

  • Inbound: Allow HTTP traffic from the presentation tier SG (SG1) on port 80.
  • Inbound: Allow SSH access from your IP address (replace <YOUR_IP> with your actual IP) on port 22 (optional).
  • Outbound: Allow all outbound traffic.

Database Security Group (SG3 - for ECS only):

  • Inbound: Allow database access only from the application tier SG (SG2) on the designated database port (e.g., 3306 for MySQL).
  • Outbound: Allow all outbound traffic.

3. RDS Database:

  • Create an RDS database instance (e.g., MySQL) in a private subnet with appropriate storage and instance type.
  • Configure database credentials and security settings within the RDS instance.

4. CodeBuild Project (Optional):

  • Create a CodeBuild project to automate building your application code.
  • Configure build commands and environment variables specific to your application.

5. CodePipeline:

Create a CodePipeline with three stages: Source, Build (optional), and Deploy.

Source Stage:

  • Connect your code repository (e.g., GitHub) to the pipeline.

Build Stage (Optional - for EC2 & mandatory for ECS):

  • Use CodeBuild project created in step 4 to build your application code (EC2) or Docker images (ECS).

Deploy Stage: 1. EC2 Deployment:

  • Use CodeDeploy to deploy the built artifact (AMI) to EC2 instances.
  • Configure CodeDeploy with the following:
  • Deployment Group: Create a deployment group with the application tier EC2 instances and the appropriate security group (SG2).
  • Deployment Configuration: Use a CodeDeploy agent pre-installed on the EC2 instances.
  • Deployment Type: Choose "In-place" deployment.

2. ECS Deployment:

  • Use CodeDeploy to deploy the built Docker images to your ECS cluster.
  • Configure CodeDeploy with the following:
  • Deployment Group: Create a deployment group with your ECS cluster and service definition (explained later).
  1. EC2 Instances (for EC2 Deployment):
  • Launch EC2 instances in a public subnet with the application tier AMI (if using a Build Stage) or your base application image.
  • Configure the instances with the application tier security group (SG2).
  • Install a web server (e.g., Apache) and configure it to serve your application.

7. ECS Cluster and Service (for ECS Deployment):

  • Create an ECS cluster to manage your containerized application.
  • Define an ECS Task Definition with your container image (from the Build Stage) and resource requirements (CPU, memory).
  • Create an ECS Service that runs the Task Definition on the ECS cluster.
  • Configure the service with desired task count, launch type (e.g., Fargate), and load balancer (ALB in your VPC).

8. Application Load Balancer (ALB):

  • Create an Application Load Balancer (ALB) in your VPC.
  • Configure the ALB to route traffic to the presentation tier security group (SG1).
  • For EC2 deployment, register your EC2 instances with the ALB target group.
  • For ECS deployment, the ECS service automatically registers tasks with

https://docs.aws.amazon.com/codepipeline/latest/userguide/ecs-cd-pipeline.html

profile picture
EXPERT
answered 5 months ago
1

Hi Mahendra Kumar V,

Please go through the below steps I hope it will helps to resolve your issue.

1. Set Up Your AWS Environment

IAM Roles:

  • Create an IAM role for EC2 with AmazonEC2FullAccess.
  • Create an IAM role for ECS tasks with AmazonECSTaskExecutionRolePolicy.
  • Create an IAM role for Code Pipeline with AWSCodePipelineFullAccess.

VPC:

  • Use the VPC wizard to create a VPC with public and private subnets.
  • Ensure you have subnets in at least two availability zones.

S3 Bucket:

  • Create an S3 bucket for storing build artifacts.
  • Configure bucket policies to allow access from Code Pipeline.

2. Create and Configure the Code Pipeline

Source Stage:

  • In the AWS CodePipeline console, create a new pipeline.
  • Select the source provider (e.g., GitHub) and connect your repository.

Build Stage:

  • Create a CodeBuild project.
  • Define a buildspec.yml in your repository:

version: 0.2

phases:
  install:
    runtime-versions:
      nodejs: 14
  build:
    commands:
      - npm install
      - npm run build
artifacts:
  files:
    - '**/*'
  discard-paths: yes

3. Deploy the Application Using EC2

EC2 Instances:

  • Launch EC2 instances in your private subnets.
  • Install necessary software (e.g., Apache, Nginx, Node.js).

RDS Instance:

  • Create an RDS instance in your private subnets.
  • Configure security groups to allow access from the application instances.

Application Load Balancer:

  • Create an ALB in the public subnets.
  • Register EC2 instances as targets.

Deploy Using CodeDeploy:

  • Create a CodeDeploy application and deployment group.
  • Define an appspec.yml in your repository:
version: 0.0
os: linux
files:
  - source: /
    destination: /var/www/html
hooks:
  AfterInstall:
    - location: scripts/restart_server.sh
      timeout: 300
  • Integrate CodeDeploy into CodePipeline.

4. Deploy the Application Using ECS

ECS Cluster:

  • Create an ECS cluster using Fargate or EC2 launch type.
  • Define task definitions and services for your application.

Application Load Balancer:

  • Set up an ALB with listeners and target groups for your ECS services.

ECR Repository:

  • Create an ECR repository and push your Docker images:
$(aws ecr get-login --no-include-email --region <your-region>)
docker build -t <your-image> .
docker tag <your-image>:latest <account-id>.dkr.ecr.<region>.amazonaws.com/<your-repo>:latest
docker push <account-id>.dkr.ecr.<region>.amazonaws.com/<your-repo>:latest

Deploy Using CodePipeline:

  • Add an ECS deployment stage to your pipeline.
EXPERT
answered 5 months ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions