Invoke an Amazon SageMaker endpoint using AWS Lambda

Step-by-step guide for calling an Amazon SageMaker XGBoost regression model endpoint using API Gateway and AWS Lambda

Sriramya Kannepalli
Analytics Vidhya
4 min readMay 1, 2020

--

Photo by h heyerlein on Unsplash

Assuming you are here because you already have a deployed SageMaker ML model endpoint, trying to understand the process of hosting it using AWS Lambda functions. If yes, then let’s get started..

Note : If not, you can create a XGBoost SageMaker endpoint by reading my previous blog Random Forest and XGBoost on Amazon SageMaker and implement this jupyter notebook.

Jupyter Notebook for deploying XGBoost as SageMaker Endpoint

Where is my Endpoint ?

Now after training and deploying the model in SageMaker, you can copy the name of your deployed endpoint from SageMaker>Inference>endpoints from SageMaker Console. We will be using the endpoint name while defining the environment variables in AWS Lambda function.

Creating AWS Lambda Function

Search for ‘Lambda’ in AWS console and click on ‘create function’ under functions.

Screenshot — Create new Function in AWS Lambda

Select Runtime — Python 3.6 and add the below sample code in Function code:

and click on ‘Save’.

Sample lambda_function-Code in python

Click on edit Environment Variables > Add environment variable and add

Key : ‘ENDPOINT_NAME’

Value: ‘<Your-Endpoint-name>’ and click ‘Save’.

If you are implementing the XGBoost model endpoint used in the above sample tutorial then your value string may be in format :

Value: ‘xgboost-YEAR–MONTH–DATE–xx–xx–xx–xxx’

Defining IAM role

Then scroll up and click on ‘Permissions’ tab.

Click on Execution Role > Role name

You well be redirected to IAM Console.

  1. Click on your policy under Policy Name.
  2. Click on edit policy > JSON
  3. Add comma at the end of existing JSON string and include the following string at the end.

4. Don’t forget to click ‘Review Policy’ and ‘Save Changes’.

This will give your Lambda function permission to invoke a SageMaker model endpoint.

API Gateway

Now search for API Gateway in AWS Console

  • Click on ‘Import’ under REST API section.
  • Select ‘New API’ under ‘Create new API’.
  • Enter ‘API name’ in Settings and click on ‘Create API’. You will be redirected to below screen —
  • Click on ‘Create Resource’. Enter ‘Resource Name’ and click ‘Create Resource’. For e.g. I named my resource ‘housing-predictor’
  • Click on ‘Actions’ and ‘Create Method’
  • Select ‘POST’ method and click on ‘✔️ ’.
  • Enter Lambda Function name and click on ‘save’.
  • Now go to ‘Actions’ and hit ‘Deploy API’.
  • Select ‘New Stage’ and enter Stage Name. For e.g test or prod and click on ‘Deploy’.
  • Now click on POST under Stages > test and copy the Invoke URL ending with <your-function-name>

Note: Your URL should end with <your function-name> and not ‘test’.

Now that we have the Lambda function, an API Gateway, and the test data(copy single data point from your test data), let’s test it using Postman, which is an HTTP client for testing web services. You can download the latest version of Postman here.

Place the Invoke URL into Postman as shown in the following screenshot and choose POST as method. In the Body tab, place the test data as shown in the following screenshot. Choose the Send button and you will see the returned result as “1005792.625” for the case of the test data shown in XGBoost tutorial.

Hurrah!! 🎊🎉🎊 You have created a model endpoint deployed and hosted by Amazon SageMaker. Then you called the endpoint using serverless architecture(an API Gateway and a Lambda function) that invoke the endpoint.

Now you know how to call a machine learning model endpoint hosted by Amazon SageMaker using AWS Lamda serverless Functions.. Congratulations !!! 👏👏👏

Photo by Susan Quiles Photography on Unsplash

--

--

Sriramya Kannepalli
Analytics Vidhya

Startup Founder | Machine Learning Engineer | Tech Blogger