Skip to content

ForkahEH/Deploy-HTML-based-static-web-application-on-AWS-EC2

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 

Repository files navigation

Deploy-HTML-based-static-web-application-on-AWS-EC2# Application Setup

This project was adapted from: https://devopsrealtime.com/deploy-html-based-static-web-application-on-aws-ec2/

image

Pre-Deployment

Create the Golden AMI with application dependencies

Step 1: Launch EC2 instance for Golden AMI

  1. In the AWS Console Home, search for and select "EC2" resource.

  2. In the EC2 Console, select "Launch Instances".

  3. In the "Launch Instance" page, enter the name of the webserver: HTML Server.

    Instance type: t2.micro

    Key pair: CICD

    Select Edit in Network Settings

    select create security group

    Security group name: HTML SG

    Rules: SSH, port 22 and HTTP, port 80

    SSH source: My IP, Custm TCP source: Everywhere IPv4

    NB: The source, "My IP" is selected for added security.

  4. Click "Launch Instance"

  5. Click "View Instances"

Screenshot 2023-10-05 191914

  1. Refresh until status check for the instance shows "2/2 checks passed".

  2. In this project, we'll connect to the instance using SSH.

Screenshot 2023-10-05 191940

Step 2: Install Application dependecies

  1. Install AWSCLI using the installation instructions from the AWS Documentation: https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html

Screenshot 2023-10-05 192256

  1. Install and start Apache Web Server using the following commands:

    sudo apt update

    sudo apt install apache2

    sudo systemctl start apache2

    sudo systemctl status apache2

    Screenshot 2023-10-05 192720

Step 3: Configure Apache to start automatically after the instance reboot

  1. Enable Apache to start on reboot: sudo systemctl enable apache2

Screenshot 2023-10-05 192525

Step 4: Verify Installation of Git

Check if git is already installed: git --version

Screenshot 2023-10-05 192602

Step 5: Create Golden AMI

  1. Stop and select the HTML server, Click on the actions tab, select "Images and template" from the drop down menu, select "create image" from the drop down menu.

Screenshot 2023-10-06 093120

  1. In the Create image page, enter the name of the image and enable reboot and click "Create image".

  2. In the EC2 Console, select AMIs under Images

    Screenshot 2023-10-05 192955

  3. Refresh until the AMI is available

    Screenshot 2023-10-06 093909

Deployment

Infrastructure Setup

Step 1: Create Security Group allowing port 22 from custom IP source and port 80 from public

The HTML SG created earlier is used

Screenshot 2023-10-06 094302

Step 2: Create Key-Pair and download the private key

The CICD key pair is used

Step 3: Create t2.micro type EC2 instance using Golden AMI

  1. In the AWS Console Home, search for and select "EC2" resource.

  2. In the EC2 Console, select "Launch Instances".

  3. In the "Launch Instance" page, enter the name of the webserver: Static Server.

    Application and OS Images (Amazon Machine Image): Select My AMIs and select the HTML AMI.

Screenshot 2023-10-06 100036

Instance type: t2.micro

Key pair: CICD

In the Network settings section, select the existing HTML SG security group.

  1. Click "Launch Instance"

  2. Click "View Instances"

Screenshot 2023-10-06 095831

Step 4: Create Elastic IP and associate the IP to EC2 instance

  1. In the EC2 Console, select "Elastic IPs".

  2. Click "Allocate Elastic IP"

Screenshot 2023-10-06 100813

  1. Under Allocate Elastic IP address, leave the defaults and click Allocate.

Screenshot 2023-10-06 101437

  1. Click on the actions tab, select "Associate Elastic IP Address" from the drop down menu, select "create image" from the drop down menu.

Screenshot 2023-10-06 101542

  1. In the "Associate Elastic IP address" page, under Resource type, select Instance, enter the id of the instance and click Associate.

Screenshot 2023-10-06 110330

Step 5: Create GP2 type EBS volume named /dev/xvdb of size 5GB in same AZ where EC2 was created

  1. In the EC2 Console, select "Volumes" under Elastic Block Store and click "Create volume".

image

  1. Choose the AZ of the EC2 instance, select Encryption and click Create volume.

  2. Select the created volume and enter the name of the volume.

Screenshot 2023-10-06 111109

Step 6: Attach EBS volume to EC2 instance

  1. Click on the actions tab, select "Attach volume".

  2. Select the ec2 instance from the instance text box.

  3. Choose "Attach".

Step 7: Create Route53 hosted zone with your domain name and configure A record pointing to the EC2 EIP

  1. In AWS Management Console, navigate to Route 53.

  2. In the Route 53 console, click on "Register a Domain".

Screenshot 2023-10-06 013315

  1. Under Register domains, enter a name and check its availability.

  2. Select the domain name you want to register and click "Proceed to register".

Screenshot 2023-10-06 014252

  1. Fill in your contact infomation, click "Next" and submit.

  2. Refresh till the registration is successful.

Screenshot 2023-10-06 020834

  1. Click Hosted zone and select your domain name.

Screenshot 2023-10-06 021016

  1. Click Create record. In the Quick create record page, select Record type A and click "Create Records".

Screenshot 2023-10-06 021543

Application Setup

Step 1: Create File System on xvdb volume and mount it on /var/www/html directory

https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ebs-using-volumes.html

  1. Run the following command to view your available disk devices to help you determine the correct device name to use: lsblk

  2. Run the following command to get information about the EBS volume: sudo file -s /dev/xvdf

Screenshot 2023-10-06 015319

  1. Run the following command to create a file system on the volume: sudo mkfs -t xfs /dev/xvdf

  2. Run the following command to mount the EBS Volume: sudo mount /dev/xvdf /var/www/html

  3. Verify that the EBS volume is mounted properly using the following command: df -h

Screenshot 2023-10-06 015648

Step 2: Use Git commands and clone the source code from Bit Bucket repository provided in the pre-requisites

  1. Git clone the source code: git clone https://bitbucket.org/dptrealtime/html-web-app.git

Screenshot 2023-10-06 020015

Step 3: Deploy the source code into web server document root folder – /var/www/html

  1. Change directory into the cloned directory

Screenshot 2023-10-06 020213

  1. Copy the required files and restart the service

sudo cp -r css/ images/ index.html js error.htm ok.htm header.html /var/www/html

sudo systemctl restart apache2

Screenshot 2023-10-06 020228

  1. Enter the public IP of the EC2 instance in the browser to verify deployment

Screenshot 2023-10-06 022357

Post-Deployment

Configure Cloudwatch agent to monitor Memory utilization of EC2 instance

Step 1: Create IAM Role

https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/create-iam-roles-for-cloudwatch-agent-commandline.html

  1. In the AWS Management Console, navigate to the IAM console.

  2. Select Roles and then Create role.

Screenshot 2023-10-06 113457

  1. Select AWS service under Trusted entity type. For Use case, choose EC2 under Common use cases and click Next.

Screenshot 2023-10-06 113600

  1. In the list of policies, select the CloudWatchAgentServerPolicy and click Next.

  2. Enter a name for the role and click Create role.

Step 2: Install CloudWatch Agent

https://www.cloudthat.com/resources/blog/steps-to-monitor-memory-utilization-of-ec2-instance-with-cloudwatch

https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/install-CloudWatch-Agent-on-EC2-Instance.html

  1. First create access keys and run: aws configure

Screenshot 2023-10-06 023029

  1. Download the CloudWatch agent: wget https://s3.amazonaws.com/amazoncloudwatch-agent/ubuntu/amd64/latest/amazon-cloudwatch-agent.deb

Screenshot 2023-10-06 023346

  1. Change to the directory containing the package and run the following:

    sudo dpkg -i -E ./amazon-cloudwatch-agent.deb

    sudo apt-get install -f

Screenshot 2023-10-06 023731

  1. Start the CloudWatch Agent: sudo systemctl start amazon-cloudwatch-agent

Step 3: Create the CloudWatch agent configuration file with the wizard

  1. Start the CloudWatch Agent Configuration Wizard: sudo /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-config-wizard

Screenshot 2023-10-06 024444

  1. Follow the prompts to create the CloudWatch agent configuration file with the wizard

Screenshot 2023-10-06 024715

  1. Restart and enable Cloudwatch:

    sudo systemctl start amazon-cloudwatch-agent

    sudo systemctl enable amazon-cloudwatch-agent

Create Cloudwatch Dashboard to monitor CPU & Memory metrics of the EC2 instance

  1. In AWS Management Console, navigate to CloudWatch.

  2. In the CloudWatch console, click on "Dashboards" in the left sidebar.

Screenshot 2023-10-05 222212

  1. Click on the "Create dashboard" button.

Screenshot 2023-10-05 222443

  1. Enter Dashboard name and press "Enter".

Screenshot 2023-10-05 222710

  1. In the "Add Widgets" page, select "Line", "Metrics" and click "Next".

Screenshot 2023-10-05 222831

  1. Enter "CPU" in the search bar, choose "Per-Instance Metrics", select "Static Server - Cpu Utilization" and click "Create Widget".

Screenshot 2023-10-05 223554

  1. Select "static_html" and click "Create Dashboard".

Screenshot 2023-10-05 223916

  1. Click the plus sign to add another widget.

    Screenshot 2023-10-05 224430

  2. In the "Add Widgets" page, select "Line", "Metrics" and click "Next".

  3. Click "CWAgent", "InstanceId" and select "Static Server" and click "Create Widget".

Screenshot 2023-10-05 224945

  1. The graphs for CPU and Memory are displayed on the page. Click "Save".

Screenshot 2023-10-05 225137

Configure SNS topic and subscribe e-mail to the Topic

  1. In AWS Management Console, navigate to SNS.

  2. In the SNS console, click "Topic", "Create Topic".

Screenshot 2023-10-05 225553

  1. Select "Standard", enter the name of the topic and click "Create Topic".

Screenshot 2023-10-05 230815

  1. In the topic details page, click on "Create subscription".

Screenshot 2023-10-05 231022

  1. Under Protocol, select Email, under Endpoint, enter the email address the notification should be sent to and click "Create Subscription".

Screenshot 2023-10-05 231412

  1. Confirm the subscription by clicking a confirmation link sent to the email you entered.

Screenshot 2023-10-05 231751

  1. Voila!!! You have successfully subscribed.

Screenshot 2023-10-05 232153

Screenshot 2023-10-05 232737

Configure Cloudwatch alarm with 1 data point and 5 minutes interval rate to notify to SNS topic when average CPU utilization is greater than 80% threshold.

  1. In AWS Management Console, navigate to CloudWatch.

  2. In the CloudWatch console, click on "Alarms", then "In Alarm" in the left sidebar and click "Create Alarm".

Screenshot 2023-10-05 233124

3.Select "Select metric".

  1. In the "Select Metric" page, enter "CPU" in the search bar, choose "Per-Instance Metrics", select "Static Server - Cpu Utilization" and click "Select metric".

Screenshot 2023-10-05 222831

  1. Under Metric, enter "5 minutes". Under Conditions, enter the following and click "Next".

    Threshold type: Static

    Whenever CPUUtilization is... : Greater than - 80

Screenshot 2023-10-05 234316

  1. Under Notification, select the SNS topic you created earlier and click "Next".

Screenshot 2023-10-05 234640

  1. Under Name and Description, enter a name for your alarm, click "Next" and "Create Alarm".

Configure Cloudwatch alarm with 1 data point and 5 minutes interval rate to notify to SNS topic when average Memory utilization is greater than 60% threshold.

  1. In AWS Management Console, navigate to CloudWatch.

  2. In the CloudWatch console, click on "Alarms", then "In Alarm" in the left sidebar and click "Create Alarm".

Screenshot 2023-10-05 233124

3.Select "Select metric".

  1. In the "Select Metric" page, click "CWAgent", "InstanceId" and select "Static Server" and click "Select metric".

Screenshot 2023-10-05 235451

  1. Under Metric, enter "5 minutes". Under Conditions, enter the following and click "Next".

    Threshold type: Static

    Whenever mem_used_percent is... : Greater than - 60

Screenshot 2023-10-05 235753

  1. Under Notification, select the SNS topic you created earlier and click "Next".

  2. Under Name and Description, enter a name for your alarm, click "Next" and "Create Alarm".

  3. In the CloudWatch console, click on "Alarms", then "All Alarms". Your alarms are displayed.

Screenshot 2023-10-06 000243

Use AWS CLI commands to push webserver logs to S3 bucket.

  1. Open the main Apache configuration file: sudo vi /etc/apache2/apache2.conf

  2. Add the following lines to configure the access log: CustomLog /var/log/apache2/access.log combined

Screenshot 2023-10-06 001221

  1. Add the following line to configure the error log: ErrorLog /var/log/apache2/error.log

Screenshot 2023-10-06 001812

  1. Restart the Apache web server to apply the changes: sudo service apache2 restart

  2. Run the following commands:

aws s3 cp /var/log/apache2/access.log s3://htmlstaticbucket/logs/logfile.log

aws s3 cp /var/log/apache2/error.log s3://htmlstaticbucket/logs/errorfile.log

Screenshot 2023-10-06 004112

  1. In AWS Management Console, navigate to S3.

  2. In the S3 console, select Buckets > htmlstaticbucket > logs. The files would be displayed.

Screenshot 2023-10-06 004240

  1. To automate log uploading, a cron job is used.

    Run this command: crontab -e

    Enter the following lines in the crontab file:

    0 * * * * aws s3 cp /var/log/apache2/access.log s3://htmlstaticbucket/logs/logfile-$(date +%Y%m%d%H).log

    0 * * * * aws s3 cp /var/log/apache2/error.log s3://htmlstaticbucket/logs/errorfile-$(date +%Y%m%d%H).log

    Screenshot 2023-10-06 005205

Configure S3 life cycle rules to transit previous version objects to Glacier after 30 days and delete the objects after 90 days of object creation date

  1. In the htmlstaticbucket page, click on "Management" and click "Create lifecycle rule".

Screenshot 2023-10-06 010026

  1. Enter a name for your rule, select "Apply to all objects in the bucket".

  2. Under Lifecycle rule actions, select "Move noncurrent versions of objects between storage classes" and "Permanently delete noncurrent versions of objects".

Screenshot 2023-10-06 011512

  1. Under Transition current versions of objects between storage classes, select "Glacier Flexible Retrieval" and enter 30 days.

Screenshot 2023-10-06 011631

  1. Under Permanently delete noncurrent versions of objects, select 90 days.

Screenshot 2023-10-06 011702

  1. Click "Create".

Screenshot 2023-10-06 012003

Evidence of cronjob automation

Screenshot 2023-10-06 012156

Validation

Verify if you are able to access the web application from internet browser.

Screenshot 2023-10-06 022208

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published