Getting started with AWS SageMaker - Part II

api design

In a previous post, I talked in details about using Amazon SageMaker as a Machine Learning service platform that eliminates the need for backend OS management, so users can focus on the machine learning part. An extension of SageMaker is that the model endpoint can be deployed as a public API through API Gateway with Lambda integration.

April 30th, 2020 - 5 minute read -
AWS, Sagemaker, API Gateway

The workflow

The Amazon API Gateway offers a collection of API resources and tools. For simplicity, I will only focus on the RESTful API and lambda integration. When users invoke the HTTPS endpoint, API Gateway will pass the request event as a payload onto the lambda function and return the model output. Below are the steps for creating a RESTful API.

  1. First you need to have a working SageMaker endpoint. You can launch the endpoint from a SM model in the console, but make sure the corresponding model artifact is saved in the S3 bucket (otherwise the endpoint creation will fail).
  2. Configure a lambda functions that "talks" to the SM endpoint.
  3. Set up a public RESTful API in API Gateway with the proper methods and query parameter name.

Lambda

Before getting started on writing the lambda function, create a IAM role with a policy that allows for publically invoking the endpoint object. The policy I used for the role "public_user" looks like this:


    {
        "Version": "2012-10-17",
        "Statement": [
            {
                "Sid": "VisualEditor0",
                "Effect": "Allow",
                "Action": "sagemaker:InvokeEndpoint",
                "Resource": "*"
            }
        ]
    }
 

After the role is set, head over to the AWS Lambda console, and on the left menu select Functions then Create function. For Execution role, choose Use an existing role and select that role you had just created (Fig.1).

lambda setup
Fig.1. Lauching lambda

Within the configuration tab, add a trigger for API Gateway. This is required to tell Lambda that the trigger event is coming from the API Gateway; trigger event is defined as the resource/request that invokes the lambda function to run. You can multiple triggers for one function, and each trigger will act independently. Under Environment variables, enter the SM endpoint name. The actual lambda function for my model is shown below.

At this point, it is a good idea to test your lambda function. In the browser frame where you entered the lambda code, go to Test then Configure Events. This feature gives the user the option to choose the event template to test against, and modify the code if necessary. Once testing has been passed, move on to the API Gateway.

API Gateway integration

In the Gateway console, choose REST API type to start developing an API. In the next page, it will ask for some basic setting. The only thing to fill out on that page is the API name. Next, on the Resources page, create a Resource from the Actions drop-down. Assign a name to the resource (I named mine test), then return to the same drop-down and select Create Method to creat a ANY method. Contrary to example tutorials [1], a POST method will not work.

Fig.2. below shows the setting parameters I applied to this resource method. Note that the Lambda Function refers to the name of the earlier lambda function.

method initial setup
Fig.2. Setting API method with lambda integration

Click on the method, and it will lead to a screen showing Method Execution and the accommpanied flow chart. Choose Method Request to specify the Query String Parameters, since the lambda function was writtent to read data from this field (Fig.3).

method any setup
Fig.3. Specifying the query parameter name.

Before deploying the API, there is an option to test it. Return to the previous screen for Method Execution, go to Test. The Method should be POST, and for the Query Strings, enter the parameter name followed by = (separated by comma), then run Test. On the right side of the page, you should see a status of 200 and a response body. This means the API successfully passed the test! If there was any error, carefully look through the logs to identify the cause.

http request url
Fig.4. Testing the resource method with trigger event.

Now it is time to deploy the API to a stage. Simply go to the Actions drop-down, and select Deploy API and name the Deployment stage. The tricky part is finding the right url for the API endpoint. You should go to Stages, and expand the stage object to see the invoke url. It should follow this pattern: https://{restapi_id}.execute-api.{region}.amazonaws.com/{stage_name}/{resource_name}. To make a http request, append the query string values with a ? mark. For example, my query string parameter is data, so this will be added to the url: ?data={val1,val2,val3, val4...} (Fig.5), where the {val1,val2,val3, val4...} represent the input feature values for model prediction.

http request url
Fig.5. Making a HTTP request with query parameter values.

That's all there is to it! For other type of API configuration such as HTTP proxy integration, check out the article here.

REFERENCES

[1]Olsen,R. Call an Amazon SageMaker model endpoint using Amazon API Gateway and AWS Lambda,https://aws.amazon.com/blogs/machine-learning/call-an-amazon-sagemaker-model-endpoint-using-amazon-api-gateway-and-aws-lambda/?ref=Welcome.AI

[2]Tutorial: Build an API with HTTP proxy integration , https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-create-api-as-simple-proxy-for-http.html