EC2 Linux Hands-On Lab

EC2 Overview

Amazon Elastic Compute Cloud (Amazon EC2) is a web service that provides resizable compute capacity in the cloud. Amazon EC2’s simple web service interface allows you to obtain and configure capacity with minimal friction. Amazon EC2 reduces the time required to obtain and boot new server instances to minutes, allowing you to quickly scale capacity, both up and down, as your computing requirements change. Amazon EC2 changes the economics of computing by allowing you to pay only for capacity that you actually use.

This lab will walk you through launching, configuring, and customizing a web server on Amazon EC2 using the AWS Management Console.

Let’s get started!

Create a new Key Pair

In this lab, you will need to create an EC2 instance using an SSH keypair. The following steps outline creating a unique SSH keypair for you to use in this lab.

  1. Sign into the AWS Management Console and open the Amazon EC2 console.

  2. In the upper-right corner of the AWS Management Console, confirm you are in the desired AWS region (e.g., Sydney).

  3. Click on Key Pairs in the Network & Security section near the bottom of the leftmost menu. This will display a page to manage your SSH key pairs. ec2-create-keyair1

  4. To create a new SSH key pair, click the Create Key Pair button at the top of the browser window. ec2-create-keypair2

  5. In the resulting pop up window, type [Your Name]-ImmersionDay into the Key Pair Name: text box and click Create.

For Windows users, please select ppk.

ec2-create-keypair3

  1. The page will download the file “[Your Name]-ImmersionDay.pem” to the local drive. Follow the browser instructions to save the file to the default download location.
  2. Remember the full path to the file .pem file you just downloaded.

You will use the key pair you just created to manage your EC2 instances for the rest of the lab.

Launch a Web Server Instance

In this example, we will launch an Amazon Linux 2 instance, bootstrap Apache/PHP, and install a basic web page that will display information about our instance.

Sign into your AWS Management Console and choose EC2 from the Services menu. Choose Instances in the menu on the left.

  1. Click on Launch instances create-instance

  2. In the Quick Start section, click Free tier only and then select the first Amazon Linux 2 AMI for 64-bit (x86) architecture and click Select. Note that the ami-xxxxxxxxx label and specific versions of the installed package may be different than in the image below. amazon-linux

  3. In the Step 2. Choose an Instance Type, select the t2.micro instance size and click Next: Configure Instance Details. ec2-instance-type

If it isn’t labeled Free Tier Eligible, you may incur a charge!

  1. On Step 3. Configure Instance Details page, expand the Advanced Details section located at the bottom of the page, then, copy/paste the script below into the User Data field. This shell script will install Apache & PHP, start the web service, and deploy a simple web page. Click Next: Add Storage.

User data is a method for bootstrapping your instance - Any code placed here will be executed the first time an instance is launched.

#include
https://s3.amazonaws.com/immersionday-labs/bootstrap.sh
  1. On this page you have the ability to modify or add storage and disk drives to the instance. For this lab, we will simply accept the storage defaults and click Next: Add Tags.

  2. Here, you can choose a “friendly name” for your instance by clicking ‘Add Tag’, and entering “Name” for the Key part and [Your Name] - Web Server for the Value part. This Name key, more correctly known as a tag, will appear in the console once the instance launches. It makes it easy to keep track of running machines in a complex environment. Click Next: Configure Security Group. ec2-name-tag

  3. You will be prompted to create a new security group, which will be your firewall rules. On the assumption that we are building out a Web server, name your new security group [Your Name] - Web Tier, and confirm an existing SSH rule exists which allows TCP port 22 from Anywhere. Click Add Rule.

  4. Add now another rule with HTTP from the ‘Type’ dropdown menu, and confirm TCP port 80 is allowed from Anywhere (you’ll notice, that “Anywhere is the same as ‘0.0.0.0/0’). security-group

  5. Click the Review and Launch button after configuring the security group.

  6. Review your cofiguration and choices, and then click Launch.

  7. Select the key pair that you created in the beginning of this lab from the drop-down and check the I acknowledge checkbox. Then click the Launch Instances button. Your instance will now be starting, which may take a moment. launch-instance

  8. Click the View Instances button in the lower right hand portion of the screen to view the list of EC2 instances. Once your instance has launched, you will see your Web Server as well as the Availability Zone the instance is in, and the publicly routable DNS name.

  9. Click the checkbox next to your web server to view details about this EC2 instance. instance

Browse the Web Server

  1. Wait for the instance to pass the Status Checks to finish loading. browse-web-server

If it says Initializing, let it finish.

running-instance

  1. Open a new browser tab and browse the Web Server by entering the EC2 instance’s Public DNS name into the browser. The EC2 instance’s Public DNS name can be found in the console by reviewing the “Public IPv4 DNS” name line highlighted above.

If you click on open address the default behaviour will open a new browser tab directing to https on the instance dns name. Please use http.

You should see a website that looks like the following: web-server

If you don’t see the web page (and you’ve waited a sufficient time for the instance to boot), try rebooting the instance via the console. Can you find it??

Great Job! You have deployed a server and launched a web site in a matter of minutes!!

Connect to your linux instance

To connect to the Linux instance we will use an SSH client. For Windows users, go to the Connecting To Your Linux Instance using PuTTy section below. For Mac users, use the terminal.

  1. Find the instructions to connect to your instance by first selecting your instance and finding the Connect link within the Actions menu. Select SSH client. ec2-connect

  2. Navigate to where your private key is located and enter the following command. Replace the first name and last name with the name you specified prior in step 5 when you created the key.

chmod 400 [FirstName][LastName]-ImmersionDay.pem
  1. Make a note of the Public IP of this EC2 instance ec2-publicIP

  2. Enter the following command in your SSH client to connect to your Linux instance.

ssh -i "[FirstName][LastName]-ImmersionDay.pem ec2-user@<EC_PUBLIC_IP>

Connecting To Your Linux Instance using PuTTy

  1. Start PuTTy

  2. In the Category pane, choose Session.

  3. In the Host Name box enter your public IP.

  4. Set the Port value to 22.

  5. Under Connection type, select SSH. ec2-putty1

  6. In the Category pane, expand Connection, expand SSH, and then choose Auth. Complete the following:

    • Choose Browse.
    • Select the .ppk file that you generated for your key pair and choose Open.
    • Choose Open.
  7. If this is the first time you have connected to this instance, PuTTY displays a security alert dialog box that asks whether you trust the host to which you are connecting.

  8. Choose Yes. A window opens and login as “ec2-user” and you are connected to your instance.

Cleaning up Your Resources

To delete the EC2 instance you created, Select the instance. From the Instance state menu, select Terminate instance.
ec2-delete

You will see a pop up window, asking if you are sure you wish to terminate this instance. Click “Terminate”.

Up for a Challenge? (Optional)