In a previous blog post, we covered how you can train and deploy model phases on SageMaker using the MindsDB container. At the end of that post, we created an endpoint and used SageMaker’s Runtime client to send data and get back inferences. In this post, we’ll expose the endpoint as a REST API and use the HTTP client as Postman for testing the service.
The general flow of how we do this is displayed in the flowchart below. Client application, in our case Postman, calls the API Gateway and passes the parameter values. The API Gateway passes the parameters to the Lambda function. The Lambda function parses the values and invokes SageMaker’s endpoint. The MindsDB model makes the predictions and returns the predicted value.
Create a Lambda function
Let’s login to the AWS console and select the Lambda service. Create a new Lambda function and choose Python3.7 as a runtime language.
Note that for the setup to work you will need to use an IAM role that has an AWS Lambda execution, SageMaker invoke endpoint and CloudWatch policies.
For the sake of presentation, we will keep the Lambda function code as simple to cover the basic flow. The basic flow will be to get the json data, invoke the endpoint and display the result.
Copy the code above in the function code editor and save the Lambda function.
Create an API Gateway
Now, let’s create the API. Select the API Gateway from Services and follow the below steps.
1. Create an API and select REST API
2. Choose the REST protocol and add an API Name. Leave the `Endpoint Type` the default one (Regional)
3. From the Actions dropdown menu, select Create a Resource and add a name.
4. Next, create a method and choose POST. Select the Lambda Function as Integration type and choose the Lambda function that we have previously created.
5. The last thing we need to do is to deploy the API. From the Actions dropdown menu, choose Deploy API.
6. The new popup will be displayed so we need to add the stage where the API will be deployed. Let’s add a test as a Deployment stage and click deploy.
7. Now, we have successfully deployed the Rest API. Copy the Invoke URL and add the resource path at the end, e.g., https://1212321.execute-api.us-east-1.amazonaws.com/test + /prediction.
Test with HTTP Client (Postman)
We will use Postman as an HTTP client, but you can use Curlx, Postwoman, Paw3 or follow along with the HTTP client that you are familiar with. Open the Postman (or another HTTP client) and paste the Invoke URL in the Request URL input. Choose POST as an HTTP method and select Body. Postman allows you to add file data in a POST request body so select binary and select the test dataset.
Click on Send and the returned result shall be displayed.
Hurray, you have successfully connected the hosted model endpoint on Sagemaker, to the serverless components (AWS Lambda and API Gateway) and invoked that through an HTTP client.