Creating an AWS EC2 Instance
This guide will walk through creating and provisioning an EC2 instance - useful for data collection, automated trading, or running various bots.
Part 1: Creating an Instance
1. Log into the AWS EC2 Console
You can visit the EC2 Instance Console at the following link, replacing the desired region with your own: https://console.aws.amazon.com/ec2/v2/home?region=us-east-2
Note: Friends don't let friends choose US-East-1
2. Click "Launch Instance" and select the Ubuntu 20.04 LTS AMI with x86 architecture
3. Choose an Instance Type, select t2.micro to utilize the free tier
4. Configure Instance
Select a specific subnet - this will be the same subnet used for all EC2 services, allowing multiple instances and other services to interact with each other. The actual subnet chosen does not matter (see image below) but it does need to be consistent.
5. Select Storage Options
I prefer to use GP3 type SSD storage, the size of the volume is optional between 8gb - 16tb, the cost is around ~$10 per 100GB.
6. Create Security Group
Type | Protocol | Port Range | Source |
---|---|---|---|
SSH | TCP | 22 | My IP |
HTTP | TCP | 80 | My IP |
All Traffic | All | All | AWS Default Security Group |
All Traffic | All | All | My IP |
PostgreSQL | TCP | 5432 | My IP |
7. Review Instance and Launch
Once reviewing that all settings are correct and you're ready to launch the instance, click the "Launch Instance" button.
Next a pop up screen will ask you to create a new Key Pair (or use an existing one). Your key pair is similar to a password, and is the ONLY way to access your new EC2 instance. To read more about key pairs, see here: https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-key-pairs.html.
From the screen above, choose RSA for the Key pair type, and type a name that is easy to remember/type. Save the .pem file in a secure and easy to access location on your computer.
8. Launch Instance
Part 2: Setting Up Your New Instance
1. Create an Elastic IP (Optional)
A.) Select Elastic IPs from the left hand menu in the EC2 Console. An Elastic IP address is a static IPv4 address designed for dynamic cloud computing.
B.) Click Allocate Instance
You will now see an allocated static IPv4 address in your Elastic IP console
C.) Click Actions -> Associate
Select your newly created EC2 instance and associate the Elastic IP
Click "Associate" and you'll see that the Elastic IP is now attached to your EC2 instance. This allows us to always use the same IP address, regardless if the instance is stopped & restarted (which would generally refresh the internal IPv4 address).
D.) Write down or remember your Elastic IP address, this is how we will connect to the instance
2. Accessing your Instance via SSH
A.) Open up a terminal / command prompt window and CD to the location where you saved your key-pair.pem file
Run the following command to SSH into your EC2 instance (replace the x's with your Elastic IP address)
If prompted, type "yes" to confirm that you would like to add the ECDSA key fingerprint to your list of known hosts. You will then see the Ubuntu Server "home" page.
3. General Provisioning
First we'll start by making sure everything is up to date by running the following command, restart to apply any changes and then login to your instance again (restarting takes approx. 30sec)
4. Copying local files to and from your instance
You can copy local folders / files from your personal computer to your new EC2 instance. This makes it easy to develop locally (with a GUI) and deploy your production code in the cloud.
To copy a file from your local machine to your EC2 instance, use the following code (replace ~/Desktop with the location of your key-pair):
If you would like to copy (PUSH) from your EC2 instance to your local computer, from the SSH window of your EC2 instance, use the following code:
Or to PULL from your EC2 instance to a local machine using your local terminal/command prompt window, use the following code (this will pull the requested file/folder to your current location in the terminal window):
Part 3: Optional Software / Settings
Creating an Ubuntu Service (FastAPI used as an example)
Installing PostgreSQL
Download and install the latest version of Postgres
Edit the config files to match the following screenshots
Restart Postgres
Add Postgres User
Create Database
Download the latest version of PgAdmin to access your database from a remote host (e.g. on your main Mac/Windows PC): https://www.pgadmin.org/download/
Installing Redis
Installing Caddy (web server / reverse proxy)
Caddy is used for when you need to allow inbound access to your EC2 instance, for example if you are running an API that takes requests, or serving a website.
Installing Caddy
Enable Caddy Reverse Proxy
Edit Caddy Config Files
Edit the config file to match the following:
Editing Security Group
You will also need to update your AWS EC2 security group preferences (see#6.-create-security-group) to allow inbound connections by adding the following:
Type | Protocol | Port Range | Source |
---|---|---|---|
All Traffic | TCP | 80 | 0.0.0.0/0 |
All Traffic | TCP | 443 | 0.0.0.0/0 |
Last updated