Building a Machine Learning API: Integrating AWS Lambda with API Gateway

Analytics Vidhya · Beginner ·🔧 Backend Engineering ·3mo ago

Key Takeaways

This video demonstrates how to expose a machine learning model deployed on AWS Lambda as a REST API using Amazon API Gateway, covering the integration of Lambda with API Gateway and testing the API.

Full Transcript

Welcome back everybody. So what is our next goal? We want to expose deployed ML model features and get a prediction response. Our model is running in AWS Lambda container. So for a quick recap, I have taken the example request which we had passed while doing a test using Lambda function. So this was our request body and this was the example response which we are expecting. So in short we are going to expose our ML model via APIs. I have also mentioned the steps we are going to perform and then I will be taking you all towards performing these steps one by one. So we will be checking whether our lambda is working fine or not. Then we are going to open the API gateway console which is basically one of the very good service in AWS in order to expose the APIs. If you are not aware about API gateway you can get an understanding API gateway act as a front door for all the APIs request coming from the users and then to redirect it to the backend services. In short, it is acting as a middleware everybody or facade layer. You're going to create a new rest API. You will be creating a post method and integrate with lambda. Then you're going to deploy the API. You're going to test from the API gateway console and then you will be testing using curl or a Python script. Okay. So I will be using a Python notebook in order to do a round of testing. So let's go step by step. Our first step is to ensure that our lambda working fine. So I will be navigating to management console and the first landing place would be lambda. Okay, you all would be already aware that how to get into the lambda console. If not search for lambda in the search bar and you will be navigating to lambda dashboard. Okay, you can always click on this last modified in order to check what was the latest function you had created. In my case, I have this iris ML container. Let me open this and I know that I have basically deployed this particular function using container images. I will be going to test and let me click on test button. We are going to ensure that we are getting the expected response which we had taken as a sample response to and yes we have the exactly the same response we were expecting and mentioned as a sample response. Next thing is go to API gateway everybody. So in the search bar type API and it will start giving you suggestions. So I have got a suggestion for API gateway. I will be opening API gateway. So currently I'm in Mumbai region. You can make a choice of a region as per your requirement. Okay, let me show you something. If you are new to API gateway, you can click on this and read about it little bit. Amazon API gateway is a fully managed service that makes it easy for developers to create, publish, maintain, monitor and secure APIs. Either you can click on create an API from here. Okay? Or you can go to this particular API section. From here also you can click on create an API. Let's see the region in which no API is existing. Means I want to show you a layout difference. Currently in Mumbai region I am already having so many APIs created. Let me take any other region in which I don't have any single API created. And let's see if there is any layout difference. Okay. So here I don't have any API created. If I will be going to create an API, I'm going to get a similar kind of layout. Okay. So there is no layout difference even if you are having an API existing in your region or not. I'm going back everybody because I'm going to use Mumbai as my primary region for all my exercises. And even you have to ensure like if you are basically using Mumbai for creating lambda and other resources you had used in the past demo then API you have to create in the Mumbai region only. Okay. So let me click on create API. We have different types of APIs. I'm going to choose REST API because REST API provides more flexibility [laughter] and you can also gain complete control over the request and response along with the API management capabilities. Okay, it works with Lambda HTTP AWS services. I want to integrate this API with Lambda function. That is my need. So, click on build and you have to choose new API. If you're already having an existing API, you can select the second radio button and so okay. But as I am going to create a fresh new API. So let me provide the API name. Okay. So let's say I am giving the API name as Iris ML API. If you want you can give any name of your choice. In descriptions you can mention APIs for Iris ML model. Something like this. Okay, the API endpoint type I will be keeping regional. We have different type of API endpoint. Okay, for the time being I will be talking about regional APIs are deployed in the current AWS region. Just remember that much. Okay, if you have chosen Mumbai, it will be deployed in Mumbai region. Let's click on create API. So our API is created. Now we are going to click on create resource. What is resource? Everybody understand resource as the entry point. Whenever you are going to access the APIs, resources will be entry point or you can also call it as the logical grouping. It is useful logical grouping everybody. So let's say I'm creating a resource. I'm giving a resource name as predict and this would be my resource path. Okay, secret resource you can see a resource got created. You will be seeing a resource with slash predict under the slash. Now what is the next thing everybody? Once the resource got created, we have to create a method. Okay. So we will click on create method. We have different types of method. I am going to create a post method. And then the integration type would be lambda. I want to integrate the API with a lambda function. If you have any other requirement, let's say you want to integrate with existing HTTP endpoint, you want to generate a response based on API gateway mappings and transformations. You can choose other radio button. But in our case, we know that our ML model is deployed on Lambda. So we are going to integrate this API with lambda function. Okay. So method you have chosen post and now we are talking about lambda integration. So in the search type Iris ML container. You have selected this. You can adjust other settings if you want. But for the time being we are not changing any logic. We are not providing or maintaining any request header. I'm keeping it very simple. I just want to show you how can you expose it via APIs. Click on create method. So you have created a post method too.

Original Description

Now that your machine learning model is deployed on AWS Lambda, how do you make it accessible to the world? In this tutorial, we walk through the process of exposing your model as a REST API using Amazon API Gateway. Think of API Gateway as the "front door" for your applications. It acts as a middleware layer that receives requests from users or apps and routes them to your backend Lambda function. What you will learn in this video: - API Gateway Essentials: Understanding how API Gateway acts as a facade layer for backend services. - Creating a REST API: A step-by-step guide to setting up a new API in the AWS Management Console. - Resources & Methods: How to create a /predict resource and configure a POST method to handle incoming data. - Lambda Integration: Seamlessly connecting your API Gateway to a container-based Lambda function. - Regional Deployment: Why choosing the right AWS region (like Mumbai) is critical for low latency and service compatibility. By the end of this video, you will have a functional API structure ready to receive JSON payloads and return real-time model predictions.
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Analytics Vidhya · Analytics Vidhya · 0 of 60

← Previous Next →
1 The DataHour: Data Science in Retail
The DataHour: Data Science in Retail
Analytics Vidhya
2 The DataHour: Anomaly detection using NLP and Predictive Modeling
The DataHour: Anomaly detection using NLP and Predictive Modeling
Analytics Vidhya
3 The DataHour: Energy Data Science Project from Scratch
The DataHour: Energy Data Science Project from Scratch
Analytics Vidhya
4 The DataHour: Explainable AI Need and Implementation
The DataHour: Explainable AI Need and Implementation
Analytics Vidhya
5 The DataHour: Google Cloud AI/ML
The DataHour: Google Cloud AI/ML
Analytics Vidhya
6 Prediction to Production in Machine Learning #machinelearning #prediction
Prediction to Production in Machine Learning #machinelearning #prediction
Analytics Vidhya
7 Practical Applications of Data science in Ecommerce
Practical Applications of Data science in Ecommerce
Analytics Vidhya
8 How to tackle Overfitting?#machinelearning #overfitting
How to tackle Overfitting?#machinelearning #overfitting
Analytics Vidhya
9 Building Data Pipelines on GCP #googlecloud #datapipelines #data
Building Data Pipelines on GCP #googlecloud #datapipelines #data
Analytics Vidhya
10 Hands-on with A/B Testing #abtesting #datascience
Hands-on with A/B Testing #abtesting #datascience
Analytics Vidhya
11 Efficient Implementations of Transformers #transformers #cnn  #machinelearning
Efficient Implementations of Transformers #transformers #cnn #machinelearning
Analytics Vidhya
12 Modern Deep Learning Architecture #deeplearning  #architecture #deeplearningtutorial
Modern Deep Learning Architecture #deeplearning #architecture #deeplearningtutorial
Analytics Vidhya
13 Key steps for Designing Artificial Neural Network (ANN) for Image classification #machinelearning
Key steps for Designing Artificial Neural Network (ANN) for Image classification #machinelearning
Analytics Vidhya
14 5 things you should know about Azure SQL #azure #sql #datahour #datascience
5 things you should know about Azure SQL #azure #sql #datahour #datascience
Analytics Vidhya
15 AI & ML in the Automotive Industry #machinelearning #ai
AI & ML in the Automotive Industry #machinelearning #ai
Analytics Vidhya
16 Building Machine Learning Models in BigQuery
Building Machine Learning Models in BigQuery
Analytics Vidhya
17 NLP aspects in Telecommunication Industry
NLP aspects in Telecommunication Industry
Analytics Vidhya
18 Practical Time Series Analysis
Practical Time Series Analysis
Analytics Vidhya
19 Fundamentals of Quantum Computing
Fundamentals of Quantum Computing
Analytics Vidhya
20 A DAY IN THE LIFE of a Data Scientist (From waking up to working on algorithms)
A DAY IN THE LIFE of a Data Scientist (From waking up to working on algorithms)
Analytics Vidhya
21 Classification Machine Learning Model from Scratch
Classification Machine Learning Model from Scratch
Analytics Vidhya
22 Knowledge Graph Solutions using Neo4j
Knowledge Graph Solutions using Neo4j
Analytics Vidhya
23 Model Guesstimation (MLOps)
Model Guesstimation (MLOps)
Analytics Vidhya
24 ETL Pipelines in Google Cloud Platform
ETL Pipelines in Google Cloud Platform
Analytics Vidhya
25 Key steps for Designing Convolutional Neural Network(CNN) for Image Classification
Key steps for Designing Convolutional Neural Network(CNN) for Image Classification
Analytics Vidhya
26 Getting Started with AWS EC2 #amazon #aws
Getting Started with AWS EC2 #amazon #aws
Analytics Vidhya
27 How to Use Azure NLP and Graph Databases for Intelligent Knowledge Mining
How to Use Azure NLP and Graph Databases for Intelligent Knowledge Mining
Analytics Vidhya
28 Certified AI & ML BlackBelt Plus Program #shorts
Certified AI & ML BlackBelt Plus Program #shorts
Analytics Vidhya
29 Visualizing Data using Python #machinelearning #visualization #python
Visualizing Data using Python #machinelearning #visualization #python
Analytics Vidhya
30 DCNN for Machine RUL Prediction using Time-series Data #timeseries #machinelearning #datascience
DCNN for Machine RUL Prediction using Time-series Data #timeseries #machinelearning #datascience
Analytics Vidhya
31 M in ML stands for Math & Magic
M in ML stands for Math & Magic
Analytics Vidhya
32 An Unsupervised ML approach using Clustering
An Unsupervised ML approach using Clustering
Analytics Vidhya
33 Customizing Large Language Models GPT3 for Real-life Use Cases #gpt3 #datascience
Customizing Large Language Models GPT3 for Real-life Use Cases #gpt3 #datascience
Analytics Vidhya
34 Model Parameters vs Hyperparameters - Techniques in ML Engineering #machinelearning
Model Parameters vs Hyperparameters - Techniques in ML Engineering #machinelearning
Analytics Vidhya
35 Practical MLOps #mlops #datascience
Practical MLOps #mlops #datascience
Analytics Vidhya
36 Data Engineering with Databricks #dataengineering #databricks
Data Engineering with Databricks #dataengineering #databricks
Analytics Vidhya
37 Multi-Objective Optimisation
Multi-Objective Optimisation
Analytics Vidhya
38 When Airflow Meets Kubernetes
When Airflow Meets Kubernetes
Analytics Vidhya
39 AI in Banking
AI in Banking
Analytics Vidhya
40 Learn Convolutional Neural Network for Image Recognition
Learn Convolutional Neural Network for Image Recognition
Analytics Vidhya
41 Extracting Value from Data
Extracting Value from Data
Analytics Vidhya
42 How to measure Marketing Channel Effectiveness
How to measure Marketing Channel Effectiveness
Analytics Vidhya
43 Transforming Lives | Data Science Immersive Bootcamp
Transforming Lives | Data Science Immersive Bootcamp
Analytics Vidhya
44 Stock Market Analysis - AI driven approach
Stock Market Analysis - AI driven approach
Analytics Vidhya
45 Become a Data Engineering Professional in 2022 | Future Trends + Skills Required
Become a Data Engineering Professional in 2022 | Future Trends + Skills Required
Analytics Vidhya
46 Ensemble Techniques in Machine Learning #machinelearning #ensemble #datascience
Ensemble Techniques in Machine Learning #machinelearning #ensemble #datascience
Analytics Vidhya
47 The Power of Visualization | Tableau Full Course | Analytics Vidhya
The Power of Visualization | Tableau Full Course | Analytics Vidhya
Analytics Vidhya
48 Demand for Data Engineers is on the Rise | Data Engineer | Analytics Vidhya
Demand for Data Engineers is on the Rise | Data Engineer | Analytics Vidhya
Analytics Vidhya
49 Data Visualization in Data Science | DataHour | Analytics Vidhya
Data Visualization in Data Science | DataHour | Analytics Vidhya
Analytics Vidhya
50 Role of Optimization in Machine Learning & Deep Learning | DataHour | Analytics Vidhya
Role of Optimization in Machine Learning & Deep Learning | DataHour | Analytics Vidhya
Analytics Vidhya
51 Solving any Machine Learning Problem | Approach and Steps Involved
Solving any Machine Learning Problem | Approach and Steps Involved
Analytics Vidhya
52 Topic Modeling Explained with Implementation | Using LDA in Python | DataHour by Arpendu Ganguly
Topic Modeling Explained with Implementation | Using LDA in Python | DataHour by Arpendu Ganguly
Analytics Vidhya
53 Data Engineering in E-Commerce | The Best Case Study
Data Engineering in E-Commerce | The Best Case Study
Analytics Vidhya
54 Introduction to Classification using Azure Machine Learning | DataHour | Analytics Vidhya
Introduction to Classification using Azure Machine Learning | DataHour | Analytics Vidhya
Analytics Vidhya
55 Introduction to Federated Learning | DataHour | Analytics Vidhya
Introduction to Federated Learning | DataHour | Analytics Vidhya
Analytics Vidhya
56 Diffusion Models for Generative Arts | DataHour | Analytics Vidhya
Diffusion Models for Generative Arts | DataHour | Analytics Vidhya
Analytics Vidhya
57 Master Google Analytics in 1 Hour | DataHour | Analytics Vidhya
Master Google Analytics in 1 Hour | DataHour | Analytics Vidhya
Analytics Vidhya
58 Learn Hypothesis Testing | DataHour | Analytics Vidhya
Learn Hypothesis Testing | DataHour | Analytics Vidhya
Analytics Vidhya
59 A Practical Approach to Kaggle Competition | DataHour | Analytics Vidhya
A Practical Approach to Kaggle Competition | DataHour | Analytics Vidhya
Analytics Vidhya
60 Making AI work for Business | DataHour | Analytics Vidhya
Making AI work for Business | DataHour | Analytics Vidhya
Analytics Vidhya

This video teaches how to expose a machine learning model deployed on AWS Lambda as a REST API using Amazon API Gateway, allowing users to access the model's predictions via API calls. The tutorial covers the integration of Lambda with API Gateway and testing the API using various tools.

Key Takeaways
  1. Ensure the Lambda function is working correctly
  2. Create a new REST API in API Gateway
  3. Create a resource and a post method in the API
  4. Integrate the API with the Lambda function
  5. Deploy the API
  6. Test the API using API Gateway console, Curl, or a Python script
💡 API Gateway acts as a middleware or facade layer, allowing users to access the machine learning model's predictions via API calls, while also providing features like security, monitoring, and maintenance.

Related AI Lessons

Up next
This Cop Was Held Accountable For His Brutality! #police #lawyer
Hampton Law
Watch →