Open In App

Build an API Gateway REST API with Lambda Integration

Last Updated : 16 Apr, 2023
Like Article

Pre-requisite: AWS

Amazon Web Services is a leading cloud provider which provides us with plenty of Paas, and Iaas, and services that we can use to build and deploy our applications. we going to build and Deploy a REST API with API Gateway which is integrated with AWS Lambda and expose GET and POST methods, here we going to use an open-source framework Serverless for deploying our API.

API Gateway

Amazon API Gateway is a fully managed service that makes it easy for developers to create, publish, maintain, monitor, and secure APIs at any scale.

AWS Lambda

AWS Lambda is a serverless computing service provided by Amazon Web Services (AWS). we can create functions and self-contained applications.

With AWS Lambda functions we can perform any kind of computing task, from serving web pages to building backend APIs, and we can integrate lambda with other AWS services as well, here in this article, we are integrating lambda with API Gateway.


Serverless is a framework that can be used to build applications on AWS, this will encapsulate things and provide us with a simple structure to create something in AWS. We can Code less and Build more with Serverless.

Install Serverless

Install serverless by running the below command

npm i -g serverless

Check the version of serverless

serverless -v
Check serverless version


Create REST API and its resources

To create a REST API, we need to do the following:

  • Create REST API in API Gateway.
  • Create Resources in created REST API.
  • Configure Lambda Integration.

We can automate all the above things using the below script.

Serverless Config

Write the below content serverless.yml

service: api-gateway-lambda-rest-api

useDotenv: true

  name: aws
  stage: ${opt:stage, 'dev'}
  region: ap-south-1
  lambdaHashingVersion: 20201221
    restApi: true
    level: INFO
    blockPublicAccess: true
    name: ${self:custom.config.CODE_DEPLOYMENT_BUCKET}
    maxPreviousDeploymentArtifacts: 3

    S3_BUCKET: ${env:S3_BUCKET}

    name: ${self:service}-function-${self:provider.stage}
    handler: lambda_function.handler
    description: Sample application with API Gateway integration
    runtime: python3.9
    memorySize: 128
    timeout: 10
      - http:
          path: api/hello
          method: post
      - http:
          path: api/testApi
          method: get


In the provider, we configure cloud provider details like cloud provider name, region, logs config, etc.


In this section (not a default option, we can replace custom with any name) we gonna maintain any configuration variables required for this script.


Cloudformation script generated based on our serverless script and application code (if any) will be stored in this bucket and it will then be used for deployment.

Note: Here we’ll not be creating a new bucket through this script, create an s3 bucket for the deployment bucket, if it does not exist already.

Deploy Our REST API

To deploy our REST API, you can run the below command.

serverless deploy --stage alpha
Deploy serverless app


Deploy Serverless Application

API Gateway Resources

After running the above command successfully you can check API Gateway, you’ll find REST API with GET and PUT methods integrated with Lambda.

API Gateway Resources


Test our API

Now we can test deployed REST API, we can invoke the API through any browser client to test out the API, below are sample API calls.

GET Method

In the below image, I’ve invoked an API  with the GET method, which gave us a sample message.

Note: As we didn’t set any authorization, no need to pass any authentication headers or data to invoke our API.

GET Request on API Gateway-Lambda REST API


POST Method

In the below image, I’ve invoked an API  with the POST method, with the request body containing the name key, which returns a dynamic response from Lambda.

POST Request on API Gateway-Lambda REST API


Other Use Cases

Using API Gateway – Lambda integration, we can deploy many applications, like below:

  • Serverless applications: Lambda integration  is well-suited for serverless applications, we can deploy backend APIs using this, many big size companies use lambda to deploy their backend APIs (Django, NodeJS, etc)
  • Webhooks: API Gateway – Lambda integration can be used to build webhooks where the Lambda function is triggered by an external system like Stripe, Twilio, or GitHub.
  • Microservices: API Gateway – Lambda is well suited for building microservices-based architectures where multiple Lambda functions are used to build a single application. 


In this blog, we learned how to create a REST API with a Lambda function integration. In addition, the Serverless framework simplifies the deployment process and enables developers to focus on writing code. The API Gateway-Lambda integration can be used to build various types of applications, from serverless applications to microservices. By leveraging this powerful tool, developers can build scalable and efficient APIs on AWS.

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads