Part 2: Spin up virtual machine on AWS and use it to host your flask server
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:
- Go to AWS dashboard
- Click on EC2
- 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)
2.2 Starting the machine:
- Select t2 micro machine
- Click review and launch
- Click Launch
- Create New Key Pair & Download pem file into home directory
- Launch Instance
2.31 Setup ssh
- Move .pem file into .ssh directory
- Navigate to .ssh directory
- 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
- Navigate to EC2 page and click on Running Instances
- Click on running instance and select security
- Click on Inbound
2.33 Open the necessary ports
- add SSH rule for port 22 with source ::/0
- add custom TCP Rule for port 3333 and source 0.0.0.0/0
- add custom TCP Rule for port 3333 and source ::/0
Before anything more, navigate back to the running instances, and find your Public DNS (IPv4) number:
2.4 Set up the Config file in .ssh folder on your local machine
- Navigate to .ssh folder
- Open config file (if you don’t have one, make one without extension)
- 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!
- On your local machine go to the directory above flask_website.
- 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