EC2 Exercise 1.2: Host a Static Webpage with Content from Github

Kerry Sheldon
3 min readMay 6, 2018

--

(This post is part of the AWS for the Bootcamp Grad series. The series consists of exercises to build familiarity with various aspects of the AWS ecosystem. Again, all of these posts are “exercises” for introductory exposure to AWS — they are NOT represented as best practices.)

Background

Many of the steps in this exercise will be the same as the ones covered in Exercise 1.1. However, in this exercise you will clone a Github repo containing and index.html file into your EC2 instance, rather than manually creating the index.html file on the instance itself.

This exercise is designed to reinforce the skills covered in the last exercise, and further emphasize that interacting with your EC2 instance is essentially the same as interacting with your physical machine (albeit a Linux one).

In this exercise, you are going to do the following:

  • Save an index.html file to a repository on Github
  • Launch an EC2 instance through the AWS console
  • SSH into to the EC2 instance and install a web server
  • Install Git on the EC2 instance
  • Clone a Repo from Github onto the EC2 instance

Save an index.html file to a repository on Github

On your local machine, initialize a git directory and create an index.html file. Add valid html to the file and push the repo to Github.

Launch an EC2 Instance

Follow the steps in the ‘Launch an EC2 Instance’ section of Exercise 1.1. However, in the final step, you will NOT need to create a new key-pair. You can select and utilize the key-pair file from your previous exercise.

SSH into the EC2 Instance and Install a Web Server

Follow the steps in the ‘SSH into the EC2 Instance and Install a Web Server’ section of Exercise 1.1.

Install Git on the EC2 Instance

At this point you should still be in your EC2 instance. Install git with the following command

yum install git -y

Clone a Repo from Github onto the EC2 instance

As you may recall from Exercise 1.1, the apache web server will display the index.html file found in /var/www/html directory in the root path of your website.

You want to clone the repo you created earlier into that directory.

  1. Navigate to the directory
cd /var/www/html

2. Attempt to clone the contents of the repository into this directory using SSH.

(note the . at the end of the command to put the contents of the repo into the current directory)

git clone git@github.com:<your_git_user_name>/<repo_name>.git .

This will not work because Github will not find an appropriate SSH key on your EC2 instance to authenticate your access. Just like your physical computer, you need to generate public and private SSH keys on the instance and add the public key to Github.

3. Generate your SSH keys on the EC2 instance

Run the following commands to generate your SSH keys on the EC2 instance (If you want to learn more about SSH key generation, check out this page):

ssh-keygen -t rsa -C "your-email@gmail.com"
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_rsa
cat ~/.ssh/id_rsa.pub

Copy the key that is displayed from the last command to your clipboard.

4. Add the Public SSH key to your Github Account

Follow the steps described here.

5. Clone the repository into you EC2 Instance

Now you should be able to clone the contents of the repository using the following command.

git clone git@github.com:<your_git_user_name>/<repo_name>.git .

Make sure the index.html file is there and contains content:

cat index.html

Navigate back to the EC2 dashboard in the AWS console and and copy the Public DNS(IPV4) of your instance into your clipboard. Paste the address into your browser. If all went well, you will see the html that you pushed to Github at the beginning of this exercise!

Clean Up

Navigate to the EC2 dashboard, select your instance, and click on Actions. Select Instance State → Terminate. Confirm that you want to terminate and you’re done. This will automatically kick you out of the SSH session in your terminal.

Next

EC2 Exercise 1.3: Host a Static Webpage with Content from S3

--

--

Kerry Sheldon

Software Developer. Graduate of Turing School of Software and Design.