Overview
We build an object detection API (Flask + YOLOv5) using AWS Lambda. By building a machine learning inference model using AWS Lambda, we aim to reduce costs.
The following article was used as a reference.
https://zenn.dev/gokauz/articles/72e543796a6423
Updates to the repository contents and additions of how to use it from API Gateway have been made.
Registering Functions on Lambda
Clone the following GitHub repository.
Running Locally
Next, create a virtual environment using venv and install the modules.
Then, running the following outputs JSON data of the object detection results.

Deployment
As mentioned at the beginning, we use the work from the following article.
https://zenn.dev/gokauz/articles/72e543796a6423
Here, we use Jupyter Notebook.
After Jupyter Notebook starts, select the following build_and_test.ipynb and execute the steps as instructed.

A change made in this repository is that it loads a conf.json file first.

A conf.json.template is included in the cloned repository, so rename it to conf.json and provide your AWS CLI profile name in the profile field.
Docker needs to be running when executing the Notebook.
(Optional) Test Execution on AWS Lambda
Below are instructions for running tests on AWS Lambda. Note that since inference is executed within the Notebook above, this step is not required. (This is my personal memo.)
Access the Lambda function page and select the “Test” tab.
As shown below, select “apigateway-aws-proxy” as the template and provide a base64-encoded string of the image to use as a sample in the body field. For creating base64-encoded image strings, refer to the “Try Inference” section in the Notebook above.

You can verify the execution results as follows.

API Gateway Configuration
From API Gateway’s “Create API,” click “Build” for REST API.

Provide an API name and click the “Create API” button.

Creating a Resource
This time, we create a resource called “detect.” Select “Actions” > “Create Resource,” enter “detect” as the resource name, and click the “Create Resource” button.

Creating a Method
Next, create a “POST” method. From “Actions” > “Create Method,” enter “POST.”


On the method setup screen, check “Use Lambda Proxy integration,” enter the name of the Lambda function you created in “Lambda Function,” and save.

A dialog to add permissions appears as shown below. Press the “OK” button.

Next, select “Method Request.”

Add “Content-Type” to “HTTP Request Headers.”

Next, select “Settings” on the left side of the screen and set " / " for “Binary Media Types.” (Please change this setting according to your use case.)

Next, select “Resources” on the left side of the screen and select Test.

Enter the following value in the request body and click the “Test” button.

If you get results like the following, it is successful.

Deploying the API
After testing is complete, select “Deploy API.”

Provide a stage name (e.g., dev) and click “Deploy.”

As a result, a URL is issued as follows.

Using that URL, post an image as follows, and if you get results, it is successful.
You can also try it with the following Google Colab.
https://colab.research.google.com/drive/1f0hUKi6Z6t0-zXaAwDW-j8fqshi43K8J?usp=sharing
Custom Domain Configuration
API Gateway
Select “Custom Domain Names” and click the “Create” button as shown below.

Then enter the “Domain name” and configure the endpoint type and ACM certificate as shown below.

On the subsequent screen, select the “API mappings” tab and click “Configure API mappings” on the right side.

Then enter the API name and stage name you created earlier and save.

Route 53
In Route 53, enter the information based on the custom domain name specified earlier.

As a result, you can publish the API with a custom domain as follows.
(Reference) How to Update ECR and Lambda Functions
A newly added Notebook called update.ipynb is stored in the repository. It is used to update previously created ECR and Lambda functions.
To use it, enter the already created repository_name and function_name in conf.json before executing the Notebook.
Summary
In this article, we built an object detection API (Flask + YOLOv5) using AWS Lambda.
We hope this article serves as a useful reference when building machine learning inference models using AWS Lambda.