Skip to content

Commit

Permalink
Update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
SourasishBasu authored Oct 31, 2023
1 parent 5c99b93 commit 521dc4a
Showing 1 changed file with 67 additions and 6 deletions.
73 changes: 67 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,27 @@ A simple static website deployed using an Apache http web server within an EC2 L
# Prerequisites

- Go to [AWS](https://aws.amazon.com/) 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
- Download the website files from this repository.

# Instructions

1. After logging in, go to the Services tab in the AWS Console homepage.
2. Search EC2 and scroll to `Launch Instance`
2. Search EC2 and click on `Launch Instance`.

<details>
<summary>EC2 Instructions</summary>
<summary><h2>EC2 Instructions</h2></summary>

### Creating a Linux EC2 instance

1. Choose a name for your EC2 server.
2. Under `Application and OS Images (AMI)` choose Ubuntu. This sets what OS your Linux server will be running.
3. Under `Instance Type` choose `t2.micro`. This specifies what the technical specficiations of your server will be such as no. of CPUs, memory etc.
4. Under `Key Pair (login)` select Create a new key pair. This gives you a key which will later be used to access your server manually and run commands and functions on it.
4. 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.
5. Under `Network Settings` choose `Create 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.
6. Under Summary, choose `Launch Instance`.
7. 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.
8. Click on the Instance ID. This will open up a pane showing all the details of your server. Set aside the `Public IPv4 Address` which will later be used to access our website on the server.
8. Click on the Instance ID. This will open up a pane showing all the details of your server. Set aside the `Public IPv4 Address` and `Public IPv4 DNS` which will later be used to access our website on the server.
9. Click on the `Connect` button at the top of this pane. Under the Connect to Instance page, select `EC2 Instance Connect` and click `Connect`.

### Setting up the Website
Expand Down Expand Up @@ -60,5 +61,65 @@ A simple static website deployed using an Apache http web server within an EC2 L

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.
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:

<details>
<summary><h2>Github Actions Steps</h2></summary>

### Creating Github Actions workflow
1. 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.

2. Create a `.github/workflows` directory in your repository on GitHub if this directory does not already exist.
3. In the `.github/workflows` directory, create a file named `github-actions-ec2.yml`.
4. Copy the below snippet into the .yml file.

```bash
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.
</details>

0 comments on commit 521dc4a

Please # to comment.