This project was adapted from: https://devopsrealtime.com/deploy-html-based-static-web-application-on-aws-ec2/
-
In the AWS Console Home, search for and select "EC2" resource.
-
In the EC2 Console, select "Launch Instances".
-
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.
-
Click "Launch Instance"
-
Click "View Instances"
-
Refresh until status check for the instance shows "2/2 checks passed".
-
In this project, we'll connect to the instance using SSH.
- Install AWSCLI using the installation instructions from the AWS Documentation: https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html
-
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
- Enable Apache to start on reboot: sudo systemctl enable apache2
Check if git is already installed: git --version
- 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.
-
In the Create image page, enter the name of the image and enable reboot and click "Create image".
-
In the EC2 Console, select AMIs under Images
-
Refresh until the AMI is available
The HTML SG created earlier is used
The CICD key pair is used
-
In the AWS Console Home, search for and select "EC2" resource.
-
In the EC2 Console, select "Launch Instances".
-
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.
Instance type: t2.micro
Key pair: CICD
In the Network settings section, select the existing HTML SG security group.
-
Click "Launch Instance"
-
Click "View Instances"
-
In the EC2 Console, select "Elastic IPs".
-
Click "Allocate Elastic IP"
- Under Allocate Elastic IP address, leave the defaults and click Allocate.
- Click on the actions tab, select "Associate Elastic IP Address" from the drop down menu, select "create image" from the drop down menu.
- In the "Associate Elastic IP address" page, under Resource type, select Instance, enter the id of the instance and click Associate.
- In the EC2 Console, select "Volumes" under Elastic Block Store and click "Create volume".
-
Choose the AZ of the EC2 instance, select Encryption and click Create volume.
-
Select the created volume and enter the name of the volume.
-
Click on the actions tab, select "Attach volume".
-
Select the ec2 instance from the instance text box.
-
Choose "Attach".
Step 7: Create Route53 hosted zone with your domain name and configure A record pointing to the EC2 EIP
-
In AWS Management Console, navigate to Route 53.
-
In the Route 53 console, click on "Register a Domain".
-
Under Register domains, enter a name and check its availability.
-
Select the domain name you want to register and click "Proceed to register".
-
Fill in your contact infomation, click "Next" and submit.
-
Refresh till the registration is successful.
- Click Hosted zone and select your domain name.
- Click Create record. In the Quick create record page, select Record type A and click "Create Records".
https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ebs-using-volumes.html
-
Run the following command to view your available disk devices to help you determine the correct device name to use: lsblk
-
Run the following command to get information about the EBS volume: sudo file -s /dev/xvdf
-
Run the following command to create a file system on the volume: sudo mkfs -t xfs /dev/xvdf
-
Run the following command to mount the EBS Volume: sudo mount /dev/xvdf /var/www/html
-
Verify that the EBS volume is mounted properly using the following command: df -h
Step 2: Use Git commands and clone the source code from Bit Bucket repository provided in the pre-requisites
- Git clone the source code: git clone https://bitbucket.org/dptrealtime/html-web-app.git
- Change directory into the cloned directory
- 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
- Enter the public IP of the EC2 instance in the browser to verify deployment
-
In the AWS Management Console, navigate to the IAM console.
-
Select Roles and then Create role.
- Select AWS service under Trusted entity type. For Use case, choose EC2 under Common use cases and click Next.
-
In the list of policies, select the CloudWatchAgentServerPolicy and click Next.
-
Enter a name for the role and click Create role.
- First create access keys and run: aws configure
- Download the CloudWatch agent: wget https://s3.amazonaws.com/amazoncloudwatch-agent/ubuntu/amd64/latest/amazon-cloudwatch-agent.deb
-
Change to the directory containing the package and run the following:
sudo dpkg -i -E ./amazon-cloudwatch-agent.deb
sudo apt-get install -f
- Start the CloudWatch Agent: sudo systemctl start amazon-cloudwatch-agent
- Start the CloudWatch Agent Configuration Wizard: sudo /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-config-wizard
- Follow the prompts to create the CloudWatch agent configuration file with the wizard
-
Restart and enable Cloudwatch:
sudo systemctl start amazon-cloudwatch-agent
sudo systemctl enable amazon-cloudwatch-agent
-
In AWS Management Console, navigate to CloudWatch.
-
In the CloudWatch console, click on "Dashboards" in the left sidebar.
- Click on the "Create dashboard" button.
- Enter Dashboard name and press "Enter".
- In the "Add Widgets" page, select "Line", "Metrics" and click "Next".
- Enter "CPU" in the search bar, choose "Per-Instance Metrics", select "Static Server - Cpu Utilization" and click "Create Widget".
- Select "static_html" and click "Create Dashboard".
-
Click the plus sign to add another widget.
-
In the "Add Widgets" page, select "Line", "Metrics" and click "Next".
-
Click "CWAgent", "InstanceId" and select "Static Server" and click "Create Widget".
- The graphs for CPU and Memory are displayed on the page. Click "Save".
-
In AWS Management Console, navigate to SNS.
-
In the SNS console, click "Topic", "Create Topic".
- Select "Standard", enter the name of the topic and click "Create Topic".
- In the topic details page, click on "Create subscription".
- Under Protocol, select Email, under Endpoint, enter the email address the notification should be sent to and click "Create Subscription".
- Confirm the subscription by clicking a confirmation link sent to the email you entered.
- Voila!!! You have successfully subscribed.
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.
-
In AWS Management Console, navigate to CloudWatch.
-
In the CloudWatch console, click on "Alarms", then "In Alarm" in the left sidebar and click "Create Alarm".
3.Select "Select metric".
- In the "Select Metric" page, enter "CPU" in the search bar, choose "Per-Instance Metrics", select "Static Server - Cpu Utilization" and click "Select metric".
-
Under Metric, enter "5 minutes". Under Conditions, enter the following and click "Next".
Threshold type: Static
Whenever CPUUtilization is... : Greater than - 80
- Under Notification, select the SNS topic you created earlier and click "Next".
- 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.
-
In AWS Management Console, navigate to CloudWatch.
-
In the CloudWatch console, click on "Alarms", then "In Alarm" in the left sidebar and click "Create Alarm".
3.Select "Select metric".
- In the "Select Metric" page, click "CWAgent", "InstanceId" and select "Static Server" and click "Select metric".
-
Under Metric, enter "5 minutes". Under Conditions, enter the following and click "Next".
Threshold type: Static
Whenever mem_used_percent is... : Greater than - 60
-
Under Notification, select the SNS topic you created earlier and click "Next".
-
Under Name and Description, enter a name for your alarm, click "Next" and "Create Alarm".
-
In the CloudWatch console, click on "Alarms", then "All Alarms". Your alarms are displayed.
-
Open the main Apache configuration file: sudo vi /etc/apache2/apache2.conf
-
Add the following lines to configure the access log: CustomLog /var/log/apache2/access.log combined
- Add the following line to configure the error log: ErrorLog /var/log/apache2/error.log
-
Restart the Apache web server to apply the changes: sudo service apache2 restart
-
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
-
In AWS Management Console, navigate to S3.
-
In the S3 console, select Buckets > htmlstaticbucket > logs. The files would be displayed.
-
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
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
- In the htmlstaticbucket page, click on "Management" and click "Create lifecycle rule".
-
Enter a name for your rule, select "Apply to all objects in the bucket".
-
Under Lifecycle rule actions, select "Move noncurrent versions of objects between storage classes" and "Permanently delete noncurrent versions of objects".
- Under Transition current versions of objects between storage classes, select "Glacier Flexible Retrieval" and enter 30 days.
- Under Permanently delete noncurrent versions of objects, select 90 days.
- Click "Create".
Evidence of cronjob automation
Verify if you are able to access the web application from internet browser.






































































