Launching an EC2 Instance with an Employee Directory Application
For this scenario, you will create an employee directory application by configuring user data when setting up the EC2 instance. In this exercise, you will log into the AWS Management Console as the IAM Admin user, launch an EC2 instance using a pre-created IAM role, and then stop and terminate the instance to prevent additional costs.
Task 1: Launching an EC2 Instance Using a Role
In this task, you will launch an EC2 instance to host the employee directory application.
Log In to AWS Management Console:
- If necessary, log in as your IAM Admin user.
Open the EC2 Service:
- In the Services search bar, search for
EC2
and open the service.
- In the Services search bar, search for
Navigate to Instances:
- In the navigation pane, under
Instances
, chooseInstances
.
- In the navigation pane, under
Launch a New Instance:
- Choose
Launch instances
.
- Choose
Name Your Instance:
- For
Name
, enteremployee-directory-app
.
- For
Select Amazon Machine Image (AMI):
- Under
Application and OS Images (Amazon Machine Image)
, choose the defaultAmazon Linux 2023
.
- Under
Choose Instance Type:
- Under
Instance type
, selectt2.micro
.
- Under
Create a Key Pair for Login:
Under
Key pair (login)
, chooseCreate a new key pair
.For
Key pair name
, enterapp-key-pair
and chooseCreate key pair
. The required.pem
file should automatically download.
Configure Network Settings:
Under
Network settings
andEdit
:Keep the default VPC selection.
For
Subnet
, choose the first subnet in the dropdown list.Enable
Auto-assign Public IP
.
Set Up Security Group:
Under
Firewall (security groups)
, chooseCreate security group
.For
Security group name
andDescription
, enterapp-sg
.
Configure Inbound Security Group Rules:
Remove the
ssh
rule by choosingRemove
.Add a new rule by choosing
Add security group rule
:For
Type
, chooseHTTP
.For
Source type
, chooseAnywhere
.
Configure IAM Instance Profile:
Expand
Advanced details
.Under
IAM instance profile
, chooseS3DynamoDBFullAccessRole
.
Add User Data Script:
In the
User data
box, paste the following code:bashCopy code#!/bin/bash -ex wget https://aws-tc-largeobjects.s3-us-west-2.amazonaws.com/DEV-AWS-MO-GCNv2/FlaskApp.zip unzip FlaskApp.zip cd FlaskApp/ yum -y install python3-pip pip install -r requirements.txt yum -y install stress export PHOTOS_BUCKET=${SUB_PHOTOS_BUCKET} export AWS_DEFAULT_REGION=<INSERT REGION HERE> export DYNAMO_MODE=on FLASK_APP=application.py /usr/local/bin/flask run --host=0.0.0.0 --port=80 export AWS_DEFAULT_REGION=us-west-2
Replace Region Placeholder:
- Change
<INSERT REGION HERE>
to your current region. For example,us-west-2
.
- Change
Launch the Instance:
- Choose
Launch instance
.
- Choose
View All Instances:
- Choose
View all instances
. Your instance should be listed underInstances
.
- Choose
Wait for Instance Initialization:
- Wait for the
Instance state
to change toRunning
and theStatus check
to change to2/2 checks passed
.
- Wait for the
Task 2: Viewing the Application
In this task, you will view the application running on the instance in a web browser.
Select the Instance:
- Select the instance by checking its box. Instance information will load in the pane.
Copy Public IPv4 Address:
- On the
Details
tab, copy thePublic IPv4 address
.
- On the
Open Application in Browser:
In a new browser window, paste the IP address you copied. Ensure the URL starts with
http://
(nothttps://
).You should see an
Employee Directory
placeholder. Note that the application isn’t connected to a database yet.
Congratulations! You have successfully created an EC2 instance that hosts the employee directory application. After exploring the instance, you will stop and terminate it to avoid future costs.
Task 3: Cleaning Up
In this task, you will terminate the EC2 instance you launched.
Stop the Instance:
Go back to the AWS Management Console.
Ensure the
employee-directory-app
instance is selected.At the top of the console pane, choose
Instance state
, thenStop instance
, and confirm by choosingStop
.Wait until the
Instance state
changes toStopped
.
Terminate the Instance:
Make sure the instance is selected.
Choose
Instance state
, thenTerminate instance
, and confirm by choosingTerminate
.
You have successfully cleaned up by terminating the EC2 instance, ensuring no additional costs incur. Please use FrerTier and delete all the services.