-
Set up a Front-End Machine with a web server to upload a picture, analyze it with Amazon Rekognition and predict your mood
-
Set up a Back-End Machine with a python server for the predictions (ideally in a private subnet, but not in this tutorial)
-
Create a VPC (CIDR: 192.168.0.0/16)
-
Create a Public Subnet (CIDR: 192.168.0.0/24)
-
Create an Internet Gateway and attach it to the VPC
-
Edit the Route Table
Public Subnet Route Table Destination Target 10.0.0.0/16 local 0.0.0.0/0 IGW -
Launch two EC2 instances (for example Ubuntu Server 18.04 LTS (HVM), SSD Volume Type - ami-04b9e92b5572fa0d1) with a basic Instance Type (e.g., t2.micro)
-
If a Public IP has not been assigned at launch, then allocate new Elastic IP addresses and associate them to each machine
-
Edit the Security Groups of the instances
- for the front-end machine, open ports 80 (HTTP) and 443 (HTTPS) for the connection with the web server
- for the back-end machine, open port 5000 used by
Flask
Python web framework
Front-End Machine Security Group Type Ports Protocol Source Inbound SSH 22 TCP 0.0.0.0/0 HTTP 80 TCP 0.0.0.0/0 HTTPS 443 TCP 0.0.0.0/0 Outbound all traffic all all 0.0.0.0/0 Back-End Machine Security Group Type Ports Protocol Source Inbound SSH 22 TCP 0.0.0.0/0 Custom TCP Rule 5000 TCP 0.0.0.0/0 Outbound all traffic all all 0.0.0.0/0
-
Connect to the machine via ssh
PS> ssh -i myKeyPair.pem ubuntu@<frontend_public_dns>
-
Update the package tool
$ sudo apt-get update
-
Install
apache
web server$ sudo apt-get install apache2
-
From local machine, export
Files/index.html
andFiles/static/*
to remote machinePS> scp -i myKeyPair.pem ./index.html ubuntu@<frontend_public_ip>:/home/ubuntu/ PS> scp -i myKeyPair.pem ./static/* ubuntu@<frontend_public_ip>:/home/ubuntu/static/
-
Move
index.html
andstatic/
to the default Ubuntu document root/var/www/html
$ sudo mv index.html /var/www/html $ sudo mv static /var/www/html
-
In the
static/app.js
file, change the target IP (public IP) of the back-end machine (variablebackend_ip
) -
We can now access the web application by typing in any browser
http:\\<frontend_public_ip>
-
Connect to the machine via ssh
PS> ssh -i myKeyPair.pem ubuntu@<backend_public_dns>
-
Update the package tool
$ sudo apt-get update
-
Install
Miniconda
(and restart the shell)$ wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh $ sh Miniconda3-latest-Linux-x86_64.sh $ exec $SHELL
If conda command is not found after installation:
$ export PATH=~/miniconda3/bin:$PATH
-
Create a new virtual environment with Python 3, install packages
Flask
andboto3
$ conda create -n myenv python=3.6 $ conda activate myenv $ conda install Flask boto3
-
Install Flask extension for handling Cross Origin Resource Sharing (CORS)
conda intall flask-cors
-
Configure AWS credentials:
- create a file
~/.aws/credentials
with the Access Key ID and the Secret Access Key, which looks like that:
[default] aws_access_key_id=XXXXXXXXXXXXXXXXXXXX aws_secret_access_key=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX aws_session_token=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
- a file
~/.aws/config
with the default region:
[default] region = us-east-1
- create a file
-
From local machine, export Python script
rekognition.py
to remote backend machinePS> scp -i myKeyPair.pem ./rekognition.py ubuntu@<backend_public_ip>:/home/ubuntu/
-
Create an S3 bucket with name
image-for-mood-reko
to store the picture to analyze (see inrekognition.py
)Nota: the name of the bucket must be unique across all existing bucket names in Amazon S3, so use your own and update
rekognition.py
script accordingly -
Make sure
myenv
virtual environment is active and then run Flask app$ python rekognition.py