Echo Service

Let’s construct a basic echo application that returns the passed in message parameter.

A simple example

We’ll start with a simple example before we set up a complicated environment for hosting a predictive model. The first service we’ll set up is an echo application that returns the passed in message parameter as part of the response payload. To implement this functionality, we’ll use Flask to build a web service hosted on an EC2 instance. This service can be called on the open web using the public IP of the EC2 instance.

You can also run this service on your local machine, but it won’t be accessible over the web. In order to access the function, you’ll need to enable access on port 5000, which is covered in section Jupyter on EC2.

Setting up an application

The complete code for the echo web service is shown below:

Press + to interact
import flask
app = flask.Flask(__name__)
@app.route("/", methods=["GET","POST"])
def predict():
data = {"success": False}
# check for passed in parameters
params = flask.request.json
if params is None:
params = flask.request.args
# if parameters are found, echo the msg parameter
if "msg" in params.keys():
data["response"] = params.get("msg")
data["success"] = True
return flask.jsonify(data)
if __name__ == '__main__':
app.run(host='0.0.0.0')

The first step is loading the Flask library and creating a Flask object using the name special variable. Next, we define a predict function with a Flask annotation that specifies that the function should be hosted at “/” and accessible by HTTP ...