Using Google Cloud’s AutoML in AWS Lambda with API Gateway

Rahul Raman
Oct 31 · 5 min read

The Cloud AutoML API is a suite of machine learning products that enables developers with limited machine learning expertise to train high-quality models specific to their business needs, by leveraging Google’s state-of-the-art transfer learning, and Neural Architecture Search technology.

In this tutorial, we’ll go through the step-by-step details on how to use AutoML API library with a simple lambda function using serverless framework. We will first create a lambda function which will act as a wrapper for AutoML and then use API gateway to trigger it. This function will make a simple request to a machine learning model hosted over AutoML(currently in beta) and return the classification result as a response.

Before continuing further, it is assumed that the following prerequisites are met:

  1. An AWS account with permission to access AWS Lambda console
  2. Serverless framework (Getting Started) installed.
  3. Python3.6+ runtime
  4. A sample model hosted in Google’s AutoML

You can check out AutoML from GCP’s left panel > Natural Language > AutoML text classification. To start using your custom model, you’ll have to first create a service account. If you don’t need a custom model solution, the Cloud Natural Language API provides content classification, entity and sentiment analysis, and more.

AWS Lambda with Serverless Framework

Let us start by defining our serverless.yml file:

serverless YAML configuration

Here we define our function ActionableAPI under functions with an HTTP POST event trigger and custom route /classify . This will setup an API Gateway for the endpoint and send the request to your function as an event when called. When you configure your function to be HTTP trigger based, the default timeout is automatically set to 30 seconds. If you’re expecting your requests to take more than 30 seconds to process, which is already a significant amount of time, try to break your functionality either across multiple lambda functions or look for other event mechanisms through which the request can be triggered.

The runtime is defined as python3.6 under provider and the default stage is dev . The environment variables are referenced according to the stage and you can define other stages like dev and prod and update myEnvironment accordingly. The important variables for our use case here are

  1. GOOGLE_APPLICATION_CREDENTIALS, which is the path to the file you’ll get while setting up your service account.
  2. project_id: id of your project under which the model is deployed
  3. compute_region : region in which your model is hosted
  4. model_id : id of your custom model

Since I’ll be using AutoML, I’ve added a layer for it’s python library and configured it in `ActionableAPI`. Layers are typically used for libraries that AWS runtime does not support out of the box. It has great re-usability when used across functions and it helps standardize your code as well. I won’t be going into the details of setting up a layer since it is a little out of scope for this tutorial. For more details on serverless.yml configuration and setting up a lambda layer, you can refer to this and this.

For a complete understanding of how API Gateway works in context of Lambdas, you can refer to this extensive guide.

Next, let’s define the function handleRequest under the file name ActionableAPI.py as configured in the YAML file:

Lambda function

Let’s start with a little explanation of what this function is trying to do. At the top, you can see that I’ve imported automl client library, which is basically what we configured as the layer for our function. In handleRequest , we check for sentence present in the request body. If sentence is present, we initialize the parameters required by our model with the environment variables as defined in the YAML file above and setup our AutoML client. This calls our model hosted in Google Cloud’s AutoML and returns true if there is a piece of text in the sentence (typically review) suggesting an improvement or recommendation to increase the overall quality of the product. If the classification score comes above a threshold of 0.5, we mark it as actionable and return the result along with the confidence score.

The interesting point to notice here is the way the result is returned from the function. This is to ensure API Gateway compatibility which expects the lambda to return the result in a certain manner. It typically goes like this:

{
"statusCode": 200,
"headers": {"Content-Type": "application/json"},
"body": "response body"
}

At this point, your code structure should look something like this:

(root)
|
|
|--- serverless.yml
|
|--- ActionableAPI.py
|
|--- config.json // if you have externalized your AWS keys
|
|--- node_modules/
|
|--- package.json
|
|--- package-lock.json
|
|--- automl_key.json
|

Now, to deploy the lambda function with stage dev as defined, the following command can be used:

serverless deploy -v --stage dev

To add small libraries, you don’t have to create a layer. It can be simply done by defining those packages in requirements.txt and using serverless-python-requirements . This takes care of downloading and installing the packages while deploying.

In the deployment logs, you can see the API endpoint for your lambda:

.....
.....
api keys:
......
endpoints:
POST - https://abc123XY.execute-api.us-east-2.amazonaws.com/dev/classify
functions:
.....
.....

If, by any chance, you missed this, you can head over to the Lambda Management Console in AWS and search for the function name, which in our case will be dev-ActionableAPI and click on API Gateway:

Lambda Management Console

You can then scroll to the bottom of the page and check out the API endpoint details. After you’ve tried out the API, the logs should appear in CloudWatch by default under the log group /aws/lambda/dev-ActionableAPI .

Now that we’re all setup, let’s see how we can publish this API in a marketplace.

LINK TO THE PART 2 OF THE TUTORIAL

Bewgle

Bewgle, Inc. Making sense of reviews.

Rahul Raman

Written by

ML Engineer at Bewgle

Bewgle

Bewgle

Bewgle, Inc. Making sense of reviews.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade