A simple static website deployed using an Apache http web server within an EC2 Linux instance for demoing AWS in the MLSA Stark Expo Event.
- Go to AWS and create a new account if you don't have one. This project will stay entirely within AWS Free Tier limits which is applicable to new accounts for upto 12 months since creation.
- Download the website files from this repository.
- After logging in, go to the Services tab in the AWS Console homepage.
- Search EC2 and click on
Launch Instance
.
-
Choose a name for your EC2 server.
-
Under
Application and OS Images (AMI)
choose Ubuntu. This sets what OS your Linux server will be running. -
Under
Instance Type
chooset2.micro
. This specifies what the technical specficiations of your server will be such as no. of CPUs, memory etc. -
Under
Key Pair (login)
select Create a new key pair. Select the.pem
key which can later be used to access your server manually and run commands and functions on it.- Provide a key name for example awslinux and leave the rest of the settings to default. Click Create key.
-
Under
Network Settings
chooseCreate security group
under Firewall. Enable allow HTTPS and HTTP traffic from the internet. This handles the security of the connections to and from the server. We have a public website so we want to allow any user(traffic) to access our site over the Internet. -
Under Summary, choose
Launch Instance
. -
Click on Instances on the left pane. The EC2 instance you just created will show up. It will take a while for it to get ready and show
Running
under the Instance State column which indicates your server is ready. -
Click on the Instance ID. This will open up a pane showing all the details of your server. Set aside the
Public IPv4 Address
andPublic IPv4 DNS
which will later be used to access our website on the server. -
Click on the
Connect
button at the top of this pane. Under the Connect to Instance page, selectEC2 Instance Connect
and clickConnect
.
-
Once the browser based EC2 CLI opens up run the following set of commands in order.
sudo su - apt-get update -y apt-get install -y httpd systemctl status httpd mkdir aws_site cd aws_site wget https://github.com/SourasishBasu/staticsite_starkexpo.git ls -lrt unzip main.zip ls -lrt cd staticsite_starkexpo-main mv * /var/www/html/ cd /var/www/html ls -lrt systemctl status httpd systemctl enable httpd systemctl start httpd systemctl status httpd
This installs an Apache HTTP server onto the Linux machine to have it behave as a web server and host the website files we transfer inside it from this git repository. The website files have been taken from Free CSS.
- Use the
Public IPv4 Address
from the EC2 instance to visit your website on the Internet.
CI/CD is the software practice that automates software development, testing, and deployment for faster and more reliable releases.
In our case we would like to make changes to our website files within the Github repository and have those changes automatically added, deployed and reflected in the website online immediately using Github Actions.
Fork this repository. Inside your forked repo follow these steps:
- Go to your Repository Settings > Secrets and Variables > Actions. Under Repository Secrets create the following 4 secret variables:
- EC2_SSH_KEY: This will be your .pem file which you will use to login to the instance
- HOST_DNS: Public DNS record of the instance, it will look something like this ec2-xx-xxx-xxx-xxx.us-west-2.compute.amazonaws.com from the EC2 server.
- USERNAME: Will be the username of the EC2 instance, usually `ubuntu`
- TARGET_DIR: Is where you want to deploy your code.
-
Create a
.github/workflows
directory in your repository on GitHub if this directory does not already exist. -
In the
.github/workflows
directory, create a file namedgithub-actions-ec2.yml
. -
Copy the below snippet into the .yml file.
name: Push-to-EC2 # Trigger deployment only on push to main branch on: push: branches: - main jobs: deploy: name: Deploy to EC2 on master branch push runs-on: ubuntu-latest steps: - name: Checkout the files uses: actions/checkout@v2 - name: Deploy to Server 1 uses: easingthemes/ssh-deploy@main env: SSH_PRIVATE_KEY: ${{ secrets.EC2_SSH_KEY }} REMOTE_HOST: ${{ secrets.HOST_DNS }} REMOTE_USER: ${{ secrets.USERNAME }} TARGET: ${{ secrets.TARGET_DIR }} - name: Executing remote ssh commands using ssh key uses: appleboy/ssh-action@master with: host: ${{ secrets.HOST_DNS }} username: ${{ secrets.USERNAME }} key: ${{ secrets.EC2_SSH_KEY }} script: | sudo apt-get -y update sudo apt-get install -y apache2 sudo systemctl start apache2 sudo systemctl enable apache2 cd home sudo mv * /var/www/html
In the above block we have defined our job with name Deploy to EC2 and enforced it to run on latest Ubuntu version. In order to deploy the code to our server and change the files on it, we need to access the EC2 instance using ssh
. This workflow ensures that these actions only run when we push our changes to main
branch.
Before Changes | After Changes |
---|---|
- Attach a custom domain from AWS Route 53
- Generate custom dashboards from logs generated by the instance for monitoring purposes via AWS Cloudwatch
- Create a billing alarm to alert in case of charges accumulate
- Host website on AWS S3 via Static Site Hosting