Part 2: Spin up virtual machine on AWS and use it to host your flask server

Elliott Saslow
Future Vision
Published in
4 min readJul 9, 2018

This is a part of a 5 part series in building web apps with flask and deploying them with data science and crypto currency applications. For this tutorial, we will be putting the server that we created in tutorial 1 onto an EC2 hosted by amazon and creating a static url that can then be used to access the site later. If you are just interested in how to set up a machine using Amazon Web Services, this tutorial does not require completing the part 1 and will be beneficial to you as well!

For this, you will need a development account through Amazon Web Services (AWS), and need to know how to get to your AWS dashboard. Everything else will be covered in this tutorial.

2.1 Select the EC2:

  1. Go to AWS dashboard
  2. Click on EC2
  3. On the next page, navigate to launch instance

Select AMI ID Deep Learning AMI (Ubuntu) Version 11.0 (ami-6356761c) (if not available switch to location in top right to N. Virginia)

Dashboard on the Left, Launch instance center, **Incorrect machine on right, use one quoted above

2.2 Starting the machine:

  1. Select t2 micro machine
  2. Click review and launch
  3. Click Launch
  4. Create New Key Pair & Download pem file into home directory
  5. Launch Instance
Steps for launching, from left to right

2.31 Setup ssh

  1. Move .pem file into .ssh directory
  2. Navigate to .ssh directory
  3. Change permissions on the .pem file
mv VirtualMachine.pem .ssh/
cd .ssh/
chmod 400 VirtualMachine.pem

2.32 Change Security options for your EC2 on AWS

  1. Navigate to EC2 page and click on Running Instances
  2. Click on running instance and select security
  3. Click on Inbound

2.33 Open the necessary ports

  1. add SSH rule for port 22 with source ::/0
  2. add custom TCP Rule for port 3333 and source 0.0.0.0/0
  3. add custom TCP Rule for port 3333 and source ::/0
After you have added the rules, your security should look like this

Before anything more, navigate back to the running instances, and find your Public DNS (IPv4) number:

You might also want to copy down your IPv4 Public IP

2.4 Set up the Config file in .ssh folder on your local machine

  1. Navigate to .ssh folder
  2. Open config file (if you don’t have one, make one without extension)
  3. Put the following text into the config file
Host virtual
HostName Your_Public_DNS_goes_here
User ubuntu
IdentityFile ~/.ssh/VirtualMachine.pem <- path_to_pem_file

Save your config folder and go to terminal

By typing the following command, you should be able to access your virtual machine:

ssh virtual

2.5 Adding your flask server to the new machine

So now flashback to part 1 of the tutorial. We made a Flask Server! Lets get this on the new machine and run our server remotely!

  1. On your local machine go to the directory above flask_website.
  2. Type commands:
# Copy the files over
scp -r flask_website/ virtual:
#ssh into the virtual machine
ssh virtual
#change directories into the website folder
cd flask_website
#start the server
python server.py

To now access the website, get your ip, and add :3333 to it, so in my case it would be: http://54.209.214.30:3333/

Or alternatively, you can use the following commands so that you can access the site at any time!

#ssh into the machine
ssh virtual
#Go into the website directory
cd flask_website
#start a tmux session
tmux
#Run the server
python server.py

and before closing the terminal session press control +b then d, and to access it later, tmux a -t 0 to re-attach to the session

--

--