AWS - Creating a simple VPC and EC2 instance

Introduction

This article with step through the process of quickly creating a VPC and attching an EC2 instance

Creating a VPC

Rescource by region will automaticlly have a default vpc, subnets, route tables and more created for you. One default for every Region. We can use the default VPC and reconfigure it to meet our needs. However, we will make one from scratch because that is more fun

  1. Log into AWS and select the VPC service
  2. Choose your region
  3. Launch VPC wizard
  4. Step1: Select a VPC Configuration - VPC with a Single Public Subnet
  5. Step 2: VPC with a Single Public Subnet
    1. IPv4 CIDR block: 10.0.0.0/16
    2. IPv6 CIDER block: No IPv6 CIDR block
    3. VPC name: name of your VPC
    4. Public subnet's IPv4 CIDR: 10.0.0.0/24
    5. Availability Zone: Select first availability zone
    6. Subnet name: public-subnet-a
    7. The rest of the options can stay as they are
    8. Click create VPC

This will take you to a successfully create page.

Creating an EC2 Instance

Now it's time to create an EC2 Instance, we will choose to create an Amazon Linux instace

  1. Select EC2 from the AWS web console
  2. Click 'Launch Instance'
  3. Step 1: Select Amazon Linux 2
  4. Step 2: Select 't2.micro' instance type, click continue
  5. Step 3:
    • Num. of instances : 1
    • Network: your new vpc
    • Subnet: your new subnet
    • Auto-assign Public IP: enable
    • The rest of the settings can stay at default, click next
  • Step 4: The wizard automaticly configures EBS for us and we can move on, click next
  • Step 5: click 'add another tag' with key/value Name/demo-app. This will help to identify it amongst serveral instances.
  • Step 6:
    • Assign a security group: Create a new sercurity group
    • Security group name: demo-ec2-sg
    • Description: Security group for awesome demo instances

At this point we one rule for SSH already created, this is good but we will need to create another rule.

  1. Click 'Add Rule'
  2. Type: Custom TCP
  3. Port Range: 8000
  4. Source: Anywhere
  5. Click Review and Launch
  6. Click Launch

Very Important

This last step is critical. In the pop up window you will have the option to select an existing key pair or create a new key pair. Select 'Create a new key pair' give it a name demo-app-keys. Then click 'Download Key Pair' and save this somewhere you will not loose or delete it. Finally, click 'Launch Instance'

Connecting and Deploying to an EC2 Instance

To complete this section you will want Python 3 installed on your development machine

To test out the EC2 instance we are going to create a very simple HTTP server that will deliver a simplet HTML page

Creating HTML and Python scripts

Create the follow html page

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Super Awsome Web Page 5000</title>
</head>
<body>
    <h3>Hello World!</h3>
</body>
</html>

Create the following python script:

http_runner.py

import http.server
import socketserver

PORT = 8000
Handler = http.server.SimpleHTTPRequestHandler

with socketserver.TCPServer(("", PORT), Handler) as httpd:
    print("serving at port", PORT)
    httpd.serve_forever()

Start the server python3 http_runner.py

confirm by opening a browser and navigate to localhost:8000

Configure EC2

  1. Goto EC2 Dashboard, click 'running instances'
  2. Select the new instance
  3. In the 'Description' tab take note of:
    • Public IP/ Private IP
    • Key pair
    • Availability zone
  4. Modify key pair file chmod 400 ~/path/to/your.pem
  5. ssh -i <path to pem file> ec2-user@<instance ip address>
  6. type yes when promted about authenticity of host
  7. you are now logging into the new machine

Now that we are logged in to the new instance we should always update the system first

sudo yum update

Type Y to initiate the operation

Install Python

yum list installed | grep -i python3

sudo yum install python3 -y

mkdir simple_http_app

python3 -m venv simple_http_app/

source ~/simple_http_app/bin/activate

pip install pip --upgrade

File Transfer

Time to move our local files to the ec2 instance

  1. Exit the ec2 console by typing exit, returning you to your local system prompt
  2. scp -r -i <pem_file> <local_code> ec2-user@<ec2_ip>:/home/ec2-user/simple_http_app
  3. Log back into the instance
  4. Run python script and verify by using the browser to view the html page

Very Important: This server is not for production use. This is just a simple http service for development and testing