Once your API (app.py) is working we're ready to deploy it! For this, do the following:
SSH into your EC2 instance from milestone2. There are no issues if you want to spin another EC2 instance; if you plan to do so, make sure you terminate any other running instances.
Make a file app.py file in your instance and copy what you developed above in there.
2.1 You can use the linux editor using vi. More details on vi Editor here. I do recommend doing it this way and knowing some basics like :wq,:q!,dd will help.
2.2 Or else you can make a file in your laptop called app.py and copy it over to your EC2 instance using scp. Eg: scp -r -i "ggeorgeAD.pem" ~/Desktop/worker.py ubuntu@ec2-xxx.ca-central-1.compute.amazonaws.com:~/
Download your model from s3 to your EC2 instance.
Presumably you already have pip or conda installed on your instance from your previous milestone. You should use one of those package managers to install the dependencies of your API, like flask, joblib, sklearn, etc.
4.1. You have installed it in your TLJH using Installing pip packages. if you want to make it available to users outside of jupyterHub (which you want to in this case as we are logging into EC2 instance as user ubuntu by giving ssh -i privatekey ubuntu@) you can follow these instructions.
4.2. Alternatively you can install the required packages inside your terminal.
Install packages (there might be others):
conda install flask scikit-learn joblib
Now you're ready to start your service, go ahead and run flask run --host=0.0.0.0 --port=8080. This will make your service available at your EC2 instance's IP address on port 8080. Please make sure that you run this from where app.py and model.joblib resides.
You can now access your service by typing your EC2 instances public IPv4 address appened with :8080 into a browswer, so something like http://:8080.
You should use curl to send a post request to your service to make sure it's working as expected.
EG: curl -X POST http://your_EC2_ip:8080/predict -d '{"data":[1,2,3,4,53,11,22,37,41,53,11,24,31,44,53,11,22,35,42,53,12,23,31,42,53]}' -H "Content-Type: application/json"
Now, what happens if you exit your connection with the EC2 instance? Can you still reach your service?
There are several options we could use to help us persist our server even after we exit our shell session. We'll be using screen. screen will allow us to create a separate session within which we can run flask and which won't shut down when we exit the main shell session. Read this to learn more on screen.
Now, create a new screen session (think of this as a new, separate shell), using: screen -S myapi. If you want to list already created sessions do screen -list. If you want to get into an existing screen -x myapi.
Within that session, start up your flask app. You can then exit the session by pressing Ctrl + A then press D. Here you are detaching the session, once you log back into EC2 instance you can attach it using screen -x myapi.
Feel free to exit your connection with the EC2 instance now and try accessing your service again with curl. You should find that the service has now persisted!
Once your API (app.py) is working we're ready to deploy it! For this, do the following:
SSH into your EC2 instance from milestone2. There are no issues if you want to spin another EC2 instance; if you plan to do so, make sure you terminate any other running instances. Make a file app.py file in your instance and copy what you developed above in there.
2.1 You can use the linux editor using vi. More details on vi Editor here. I do recommend doing it this way and knowing some basics like :wq,:q!,dd will help.
2.2 Or else you can make a file in your laptop called app.py and copy it over to your EC2 instance using scp. Eg: scp -r -i "ggeorgeAD.pem" ~/Desktop/worker.py ubuntu@ec2-xxx.ca-central-1.compute.amazonaws.com:~/
Download your model from s3 to your EC2 instance.
Presumably you already have pip or conda installed on your instance from your previous milestone. You should use one of those package managers to install the dependencies of your API, like flask, joblib, sklearn, etc.
4.1. You have installed it in your TLJH using Installing pip packages. if you want to make it available to users outside of jupyterHub (which you want to in this case as we are logging into EC2 instance as user ubuntu by giving ssh -i privatekey ubuntu@) you can follow these instructions.
4.2. Alternatively you can install the required packages inside your terminal.
You can now access your service by typing your EC2 instances public IPv4 address appened with :8080 into a browswer, so something like http://:8080.
You should use curl to send a post request to your service to make sure it's working as expected.
EG: curl -X POST http://your_EC2_ip:8080/predict -d '{"data":[1,2,3,4,53,11,22,37,41,53,11,24,31,44,53,11,22,35,42,53,12,23,31,42,53]}' -H "Content-Type: application/json"
Now, what happens if you exit your connection with the EC2 instance? Can you still reach your service?
There are several options we could use to help us persist our server even after we exit our shell session. We'll be using screen. screen will allow us to create a separate session within which we can run flask and which won't shut down when we exit the main shell session. Read this to learn more on screen. Now, create a new screen session (think of this as a new, separate shell), using: screen -S myapi. If you want to list already created sessions do screen -list. If you want to get into an existing screen -x myapi. Within that session, start up your flask app. You can then exit the session by pressing Ctrl + A then press D. Here you are detaching the session, once you log back into EC2 instance you can attach it using screen -x myapi. Feel free to exit your connection with the EC2 instance now and try accessing your service again with curl. You should find that the service has now persisted!