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
- Log into AWS and select the VPC service
- Choose your region
- Launch VPC wizard
- Step1: Select a VPC Configuration - VPC with a Single Public Subnet
- Step 2: VPC with a Single Public Subnet
- IPv4 CIDR block: 10.0.0.0/16
- IPv6 CIDER block: No IPv6 CIDR block
- VPC name: name of your VPC
- Public subnet's IPv4 CIDR: 10.0.0.0/24
- Availability Zone: Select first availability zone
- Subnet name: public-subnet-a
- The rest of the options can stay as they are
- 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
- Select EC2 from the AWS web console
- Click 'Launch Instance'
- Step 1: Select Amazon Linux 2
- Step 2: Select 't2.micro' instance type, click continue
- 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.
- Click 'Add Rule'
- Type: Custom TCP
- Port Range: 8000
- Source: Anywhere
- Click Review and Launch
- 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
- Goto EC2 Dashboard, click 'running instances'
- Select the new instance
- In the 'Description' tab take note of:
- Public IP/ Private IP
- Key pair
- Availability zone
- Modify key pair file
chmod 400 ~/path/to/your.pem
ssh -i <path to pem file> ec2-user@<instance ip address>
- type
yes
when promted about authenticity of host - 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
- Exit the ec2 console by typing
exit
, returning you to your local system prompt - scp -r -i <pem_file> <local_code> ec2-user@<ec2_ip>:/home/ec2-user/simple_http_app
- Log back into the instance
- 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