AWS Technical Essentials – Lab 2: Launch a Web Application on Amazon EC2
© 2022 Amazon Web Services, Inc. or its affiliates. All rights reserved. This work may not be reproduced or redistributed, in whole or in part, without prior written permission from Amazon Web Services, Inc. Commercial copying, lending, or selling is prohibited. All trademarks are the property of their owners.
Note: Do not include any personal, identifying, or confidential information into the lab environment. Information entered may be visible to others.
Corrections, feedback, or other questions? Contact us at AWS Training and Certification.
Objectives
After completing this lab, you will be able to:
- Navigate to the Amazon Elastic Compute Cloud (Amazon EC2) dashboard
- Explore the provided used by an EC2 instance on startup
- Launch an EC2 instance and configure (Secure Shell) access
- Connect to an EC2 instance using EC2 Instance Connect
- Stop and terminate an EC2 instance
Prerequisites
This lab requires:
- Notebook computer with Wi-Fi and Microsoft Windows, macOS, or Linux (Ubuntu, SuSE, or Red Hat)
- Administrator access (Microsoft Windows users)
- Internet browser, such as Chrome, Firefox, or Internet Explorer 9 or later
Note: Tablet devices cannot access the lab environment, although they can display student guides.
Duration
This lab requires 40 minutes to complete.
Scenario
In this lab, you will launch an employee directory web application. To start, you will provide bootstrap instructions to the EC2 instance using user data. The user data defines what the host must do on startup, such as set environment variables, install the application's dependencies, and so on. Next, you will configure SSH so you can connect to the EC2 instance. Then, you will connect to the instance using the built-in terminal provided by EC2 Instance Connect. Finally, you will terminate the EC2 instance.
Start lab
- To launch the lab, at the top of the page, choos Start Lab.
This starts the process of provisioning the lab resources. An estimated amount of time to provision the lab resources is displayed. You must wait for the resources to be provisioned before continuing.
If you are prompted for a token, use the one distributed to you (or credits you have purchased).
- To open the lab, choose Open Console.
The AWS Management Console sign-in page opens in a new web browser tab.
- On the Sign in as IAM user page:
- For IAM user name, enter .
- For Password, copy and paste the Password value listed to the left of these instructions.
- Choose Sign in.
Do not change the Region unless instructed.
Common sign-in errors
Error: You must first sign out
If you see the message, You must first log out before logging into a different AWS account:
- Choose the click here link.
- Close your Amazon Web Services Sign In web browser tab and return to your initial lab page.
- Choose Open Console again.
Error: Choosing Start Lab has no effect
In some cases, certain pop-up or script blocker web browser extensions might prevent the Start Lab button from working as intended. If you experience an issue starting the lab:
- Add the lab domain name to your pop-up or script blocker's allow list or turn it off.
- Refresh the page and try again.
Task 1: Launch Your Amazon EC2 Instance
In this task, you will start your EC2 instance and provide bootstrap information for the web application to launch. You will also enable SSH access to the instance.
Note: You must start the EC2 instance in the Region that matches the value of Region to the left of these instructions. You must use the same Region throughout the lab.
Step 1: Choose an Amazon Machine Image (AMI)
In the AWS Management Console, choose the Services menu, and then choose EC2. Alternatively, you can type the service name in the Search box to access the service directly.
In the left navigation pane, in the Instances section, choose Instances.
Choose Launch instance .
At the top right of the screen, choose Opt-out to the old experience button to use the old launch instances UI.
At the top of the list, find Amazon Linux 2 AMI (HVM), SSD Volume Type, make sure the 64-bit(x86) option is selected, and choose Select.
Step 2: Choose an Instance Type
- Select (make sure it is ). By default, this should be selected. This instance type has one virtual CPU and 1 GiB of memory.
Note: If is not available, choose or .
- Choose Next: Configure Instance Details.
Step 3: Configure Instance Details
This page contains settings that help you configure instance requirements, including networking and monitoring settings.
The Network setting specifies the virtual private cloud (VPC) in which an instance will be launched. You can have multiple networks, such as networks for development, testing, and production.
- For Network, select .
For Auto-assign Public IP, the option should be selected. If not, select . This setting assigns a public IP to the instance, so you can access the application in your browser.
Scroll down to the Advanced Details section, and locate the User data field.
When you launch an instance, you can pass to the instance. The data can be used to run common automated configuration tasks and scripts.
Your instance runs Amazon Linux, so you will provide a shell script that will run when the instance starts. This script installs the application's required dependencies. It also launches the application, so you can access it in your browser.
- Copy the following script, and paste it into the User data field. To copy the script, you can use the Clipboard button, located in the top-right corner of the code box.
#!/bin/bash -ex
# Update yum
yum -y update
# Add node's source repo
curl -sL https://rpm.nodesource.com/setup_15.x | bash -
#Install nodejs
yum -y install nodejs
# Create a dedicated directory for the application
mkdir -p /var/app
# Get the app from S3
wget https://aws-tc-largeobjects.s3-us-west-2.amazonaws.com/ILT-TF-100-TECESS-5/app/app.zip
# Unzip it into a specific folder
unzip app.zip -d /var/app/
cd /var/app/
# Install dependencies
npm install
# Start your app
npm start
This script performs the following tasks:
- Installs system updates
- Installs a source repository so the Node.js installer can be downloaded
- Installs Node.js
- Downloads the application code
- Creates a dedicated directory for the web application
- Downloads and deploys (extracts) the application into the specified directory
- Installs the application dependencies
- Sets the port that the application listens to
- Starts the web application
- Choose Next: Add Storage.
Step 4: Add Storage
Amazon EC2 stores data on a network-attached virtual disk called Amazon Elastic Block Store (Amazon EBS).
You will launch the Amazon EC2 instance using a default 8-GiB disk volume. This will be your root volume, also known as a boot volume.
- Choose Next: Add Tags.
Step 5: Add Tags
Tags can be used to categorize AWS resources in various ways, for example, by purpose, owner, or environment. This is useful when you have many resources of the same type — you can identify a specific resource based on its tags. For each tag, you define a key and a value.
- Choose Add Tag, and then add the following key and value text:
- Key:
- Value:
- Choose Next: Configure Security Group.
Step 6: Configure a Security Group
A security group acts as a virtual firewall that controls the traffic for one or more instances. When you launch an instance, you associate one or more security groups with the instance. You add rules to each security group that allow traffic to or from its associated instances. You can modify security group rules at any time; new rules automatically apply to all instances that are associated with the security group.
- In this step, configure the following fields as shown:
- Assign a security group:
- Security group name:
- Description:
By default, you should see an rule for the security group. This allows you to connect to the instance using SSH in the next task. Additionally, you must add a rule for HTTP access, so you can access the application in your browser.
To add an HTTP rule, do the following:
Choose Add Rule.
Using the Type dropdown, select HTTP. This allows inbound traffic on port . By default, this option allows HTTP traffic from everywhere (,).
Choose Review and Launch.
Step 7: Review Instance Launch
The Review page displays the configuration for the instance you are about to launch.
- Choose Launch.
A Select an existing key pair or create a new key pair window will appear.
Amazon EC2 uses public key cryptography to encrypt and decrypt login information. To log in to your instance, you must create a key pair, specify the name of the key pair when you launch the instance, and provide the private key when you connect to the instance.
In this lab, you will log in to your instance using EC2 Instance Connect, so you do not need a key pair.
In the Choose an existing key pair dropdown, select Proceed without a key pair.
Select the following option:
I acknowledge that without a key pair, I can connect to this instance only by using EC2 Instance Connect or if I know the password built into the AMI.
- Choose Launch Instances.
Your instance will be launched.
- Choose View Instances.
The instance should appear in a Pending state, which means it is being launched. When its state changes to Running, the instance has started booting. After a short time, you can access the instance. The instance receives a public DNS name that you can use to contact the instance from the internet.
- To see the details, choose your Web Application instance.
To show more information in the Details tab, drag the window divider up.
Review the instance's details, including the instance type, security settings, network settings, and so forth.
Wait for your instance to display the following:
- Instance state: Running
- Status check: 2/2 checks passed
Note: You might need to choose the Refresh button to see the status changes.
In the Details tab, locate the Public IPv4 address section and copy the DNS address.
In a new browser tab's address bar, type , and then paste the DNS address you copied in the previous step. You should see the following application screen.
Note: If you choose the open address link, your browser might try to browse the application using , and that won't work. The application can only be accessed using on port .
Task 2: Connect to Your Instance Using EC2 Instance Connect
Sometimes, you might need to access your EC2 instance operating system to perform certain administrative tasks, troubleshoot potential problems, check the system logs, and so forth. In this task, you will connect to your EC2 instance using EC2 Instance Connect.
To return to the AWS Management Console, choose the Services menu, and then choose EC2. Alternatively, you can type the service name in the Search box to access the service directly.
In the left navigation pane, in the Instances section, choose Instances.
At the top of the Instances screen, confirm that the Web Application instance is selected. If it's not selected, select it.
Choose Connect.
In the Connect to instance screen, in the EC2 Instance Connect tab, you can see the of the instance you are connecting to, as well as its .
In the User name field, use . Technically, you could connect using a different user that might be available on the instance you launched. For this lab, log in using the default .
Choose Connect.
You should see your instance's Linux shell, with a command prompt for .
Task 3: Stop and Start the Web Application (Optional)
In this task, you will stop and start the Web Application.
- To view the processes running on the host and the ports they are running on, place your cursor in the Linux shell, and type the following command:
sudo netstat -tulnp
- Press .
Your screen should look similar to the following image (in the image shown, the process running on port is underlined in red for reference).
In this example, the PID number is . Record your process's PID number. You will need it to stop the process.
Note: If the shell in your browser is unresponsive, repeat the steps in the previous task to reconnect to the host. Then, try the command again.
- To stop the process, enter the following command, using your process ID running on port in place of the placeholder text.
sudo kill <PID>
- In your browser, go to the tab where you accessed the , and refresh the page.
Note: If you closed the browser tab, open a new one, and type in the address bar followed by the instance's public IP address, as shown at the bottom of the screen in the browser where you entered the commands.
Since you just stopped the application, it won't display on your browser. You should get a connection timed out error or connection refused error in your browser.
- In the Linux shell, enter the following commands to restart the Web Application.
cd /var/app
sudo npm start
Refresh your browser tab where you accessed the . The Web Application should display.
To disconnect from the instance, close the Linux shell tab you used to connect with EC2 Instance Connect using SSH.
Task 4: Terminate Your EC2 Instance
In the Connect to instance screen, choose the Cancel link to the left the Connect button. You should see the list of instances in the EC2 instance dashboard.
In the instances list, choose Web Application.
Choose Instance state .
Choose Terminate instance.
Choose Terminate.
You will see the instance state should transition from Running, to Stopping, to Shutting down and ultimately, to Terminated.
Note: You might need to hit the refresh button at the top to see the status changes.
Lab Complete
Congratulations! You completed the lab.
End lab
Follow these steps to close the console, end your lab, and evaluate your lab experience.
Return to the AWS Management Console.
At the upper-right corner of the page, choose awsstudent@<AccountNumber>, and then choose Sign out.
Choose End Lab.
Choose OK.
(Optional):
- Select the applicable number of stars to rate your lab experience.
- 1 star = Very dissatisfied
- 2 stars = Dissatisfied
- 3 stars = Neutral
- 4 stars = Satisfied
- 5 stars = Very satisfied
- Enter a comment.
- Choose Submit.
You can close the window if you don't want to provide feedback.
Additional Resources
- For more information about Amazon EC2, see Amazon Elastic Compute Cloud.
- For more information about AWS Training and Certification, see http://aws.amazon.com/training/.