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.

  1. Log In to AWS Management Console:

    • If necessary, log in as your IAM Admin user.
  2. Open the EC2 Service:

    • In the Services search bar, search for EC2 and open the service.
  3. Navigate to Instances:

    • In the navigation pane, under Instances, choose Instances.
  4. Launch a New Instance:

    • Choose Launch instances.
  5. Name Your Instance:

    • For Name, enter employee-directory-app.
  6. Select Amazon Machine Image (AMI):

    • Under Application and OS Images (Amazon Machine Image), choose the default Amazon Linux 2023.
  7. Choose Instance Type:

    • Under Instance type, select t2.micro.
  8. Create a Key Pair for Login:

    • Under Key pair (login), choose Create a new key pair.

    • For Key pair name, enter app-key-pair and choose Create key pair. The required .pem file should automatically download.

  9. Configure Network Settings:

    • Under Network settings and Edit:

      • Keep the default VPC selection.

      • For Subnet, choose the first subnet in the dropdown list.

      • Enable Auto-assign Public IP.

  10. Set Up Security Group:

    • Under Firewall (security groups), choose Create security group.

    • For Security group name and Description, enter app-sg.

  11. Configure Inbound Security Group Rules:

    • Remove the ssh rule by choosing Remove.

    • Add a new rule by choosing Add security group rule:

      • For Type, choose HTTP.

      • For Source type, choose Anywhere.

  12. Configure IAM Instance Profile:

    • Expand Advanced details.

    • Under IAM instance profile, choose S3DynamoDBFullAccessRole.

  13. 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
      
  14. Replace Region Placeholder:

    • Change <INSERT REGION HERE> to your current region. For example, us-west-2.
  15. Launch the Instance:

    • Choose Launch instance.
  16. View All Instances:

    • Choose View all instances. Your instance should be listed under Instances.
  17. Wait for Instance Initialization:

    • Wait for the Instance state to change to Running and the Status check to change to 2/2 checks passed.

Task 2: Viewing the Application

In this task, you will view the application running on the instance in a web browser.

  1. Select the Instance:

    • Select the instance by checking its box. Instance information will load in the pane.
  2. Copy Public IPv4 Address:

    • On the Details tab, copy the Public IPv4 address.
  3. Open Application in Browser:

    • In a new browser window, paste the IP address you copied. Ensure the URL starts with http:// (not https://).

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

  1. 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, then Stop instance, and confirm by choosing Stop.

    • Wait until the Instance state changes to Stopped.

  2. Terminate the Instance:

    • Make sure the instance is selected.

    • Choose Instance state, then Terminate instance, and confirm by choosing Terminate.

You have successfully cleaned up by terminating the EC2 instance, ensuring no additional costs incur. Please use FrerTier and delete all the services.