ML-E9: Deploy an ML algorithm onto AWS EC2 (virtual machine)

Paul Pallaghy, PhD
6 min readJun 25, 2023

Here’s a simple way to get your nifty and brainy machine learning (ML) algorithm onto AWS EC2. As a live API.

I’m including all the steps you need to turn any piece of code into an unauthenticated API, and how to build and package in an ML predict function and host it on AWS.

ML series menu: E1 E2 E3 E4 E5 E6 E7 E8 E9

Hello world API

Our first objective is to get a basic Python ‘Hello World’ script functioning as a Flask API and test it in Postman. As a rudimentary test, let’s consider a Python application that transforms text into uppercase.

The following is an example of such a Python application:

Transforming this into a RESTful API is relatively simple, thanks to Flask. Here’s how you can accomplish that:

You can call this API from the command line as follows:

Or from within a Python script:

Lastly, to test the API using Postman, use the POST method to send a request to http://localhost:5000/upper with a JSON body, e.g., {"text":"hello"}.

Deplying to AWS

The next step is to deploy this API onto a new EC2 compute instance and test it again using Postman.

--

--

Paul Pallaghy, PhD

PhD Physicist / AI engineer / Biophysicist / Futurist into global good, AI, startups, EVs, green tech, space, biomed | Founder Pretzel Technologies Melbourne AU