Deploy Java Application on AWS 3-Tier Architecture
Contents
1 Project Overview 2
1.1 Goal .....................................................................................................................................2
2 Pre-Requisites 2
2.1 AWS Free Tier Account ................................................................................................... 2
2.2 GitHub Account and Repository ........................................................................................2
2.3 SonarCloud Account ...........................................................................................................2
2.4 JFrog Cloud Account ........................................................................................................ 2
3 Pre-Deployment 2
3.1 Create Global AMI ............................................................................................................ 2
3.2 Install CloudWatch and SSM Agents .............................................................................. 2
3.3 Create Golden AMIs ...........................................................................................................2
4 VPC Deployment 3
4.1 Network Setup ....................................................................................................................3
4.2 NAT and Internet Gateway ............................................................................................. 3
4.3 Transit Gateway and Routing ........................................................................................... 3
4.4 Bastion Host Setup.............................................................................................................4
5 Maven (Build) 4
6 3-Tier Architecture 4
6.1 Database Tier ......................................................................................................................4
6.2 Backend Tier (Tomcat) ..................................................................................................... 4
6.3 Frontend Tier (Nginx) ....................................................................................................... 4
7 Application Deployment 4
7.1 User Data Script for Tomcat............................................................................................ 4
7.2 Database Setup ...................................................................................................................4
8 Post-Deployment 4
8.1 Cron Job for Logs ............................................................................................................. 4
8.2 CloudWatch Alarms ............................................................................................................5
9 Validation
10 Overview of AWS 3-Tier Architecture 5
1
Java Application Deployment AWS 3-Tier Architecture Deployment
1 Project Overview
1.1 Goal
The primary objective of this project is to deploy a scalable, highly available, and secure Java
application using a 3-tier architecture. The application will be hosted on AWS, utilizing services
like EC2, RDS, and VPC to ensure its availability, scalability, and security. The application
will be accessible to end-users via the public internet.
2 Pre-Requisites
2.1 AWS Free Tier Account
• Sign up for an AWS Free Tier account.
• Set up and configure AWS CLI.
2.2 GitHub Account and Repository
• Fork Java source code repository to your GitHub account.
2.3 SonarCloud Account
• Register on SonarCloud and generate access token.
2.4 JFrog Cloud Account
• Set up Maven repository on JFrog Cloud.
3 Pre-Deployment
3.1 Create Global AMI
Install necessary agents and software on EC2:
curl "https://awscli.amazonaws.com/AWSCLIV2.pkg" -o "AWSCLIV2.pkg"
sudo installer -pkg AWSCLIV2.pkg -target /
aws --version
3.2 Install CloudWatch and SSM Agents
sudo yum install amazon-cloudwatch-agent
sudo yum install amazon-ssm-agent
sudo systemctl start amazon-ssm-agent
sudo systemctl enable amazon-ssm-agent
3.3 Create Golden AMIs
Nginx Installation
sudo amazon-linux-extras install nginx1.12
sudo systemctl start nginx
sudo systemctl enable nginx
Custom Memory Metric Script
2
Java Application Deployment AWS 3-Tier Architecture Deployment
#!/bin/bash
while true; do
memory_usage=$(free | grep Mem | awk ’{print␣$3/$2␣*␣100.0}’)
aws cloudwatch put-metric-data --metric-name MemoryUsage --namespace Custom --value
$memory_usage --dimensions InstanceId=$(curl http://169.254.169.254/latest/meta-
data/instance-id)
sleep 60
done &
Apache Tomcat Setup
sudo yum install java-11-openjdk-devel
wget https://dlcdn.apache.org/tomcat/tomcat-9/v9.0.53/bin/apache-tomcat-9.0.53.tar.gz
sudo tar -xvzf apache-tomcat-9.0.53.tar.gz -C /opt/
sudo ln -s /opt/apache-tomcat-9.0.53 /opt/tomcat
sudo sh /opt/tomcat/bin/startup.sh
Tomcat Systemd Service Configuration
[Unit]
Description=Apache Tomcat Web Application Container
After=network.target
[Service]
Type=forking
Environment=JAVA_HOME=/usr/lib/jvm/jre
...
[Install]
WantedBy=multi-user.target
Maven Build Tool Setup
sudo yum install git java-11-openjdk-devel
wget https://.../apache-maven-3.8.4-bin.tar.gz
sudo tar -xvzf apache-maven-3.8.4-bin.tar.gz -C /opt/
sudo ln -s /opt/apache-maven-3.8.4 /opt/maven
4 VPC Deployment
4.1 Network Setup
aws ec2 create-vpc --cidr-block 192.168.0.0/16
aws ec2 create-vpc --cidr-block 172.32.0.0/16
4.2 NAT and Internet Gateway
aws ec2 create-internet-gateway
aws ec2 attach-internet-gateway ...
4.3 Transit Gateway and Routing
aws ec2 create-transit-gateway
aws ec2 create-route --route-table-id ...
3
Java Application Deployment AWS 3-Tier Architecture Deployment
4.4 Bastion Host Setup
aws ec2 run-instances --image-id ami-xxxx ...
aws ec2 authorize-security-group-ingress --protocol tcp --port 22 --cidr YOUR_IP/32
5 Maven (Build)
git clone https://github.com/YOUR_USERNAME/YOUR_REPO.git
cd YOUR_REPO
mvn clean install -s settings.xml
mvn sonar:sonar -Dsonar.login=YOUR_SONAR_TOKEN ...
6 3-Tier Architecture
6.1 Database Tier
aws rds create-db-instance --engine mysql ...
6.2 Backend Tier (Tomcat)
aws elbv2 create-load-balancer --type network ...
aws autoscaling create-auto-scaling-group ...
6.3 Frontend Tier (Nginx)
aws elbv2 create-load-balancer --name public-nlb ...
7 Application Deployment
7.1 User Data Script for Tomcat
#!/bin/bash
wget https://jfrog-url/artifact.war
cp artifact.war /opt/tomcat/webapps/
7.2 Database Setup
mysql -h mydbinstance... -u admin -p
CREATE DATABASE mydatabase;
SOURCE /path/to/schema.sql;
8 Post-Deployment
8.1 Cron Job for Logs
0 0 * * * /usr/bin/aws s3 cp /path/to/tomcat/logs s3://mybucket/tomcat-logs/ --
recursive --region us-east-1 && rm -rf /path/to/tomcat/logs/*
4
Java Application Deployment AWS 3-Tier Architecture Deployment
8.2 CloudWatch Alarms
aws cloudwatch put-metric-alarm --alarm-name "DBConnectionsHigh" ...
9 Validation
• Verify EC2 access via Bastion Host.
• Confirm application availability via browser.
10 Overview of AWS 3-Tier Architecture