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

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

custom:
  config:
    CODE_DEPLOYMENT_BUCKET: ${env:CODE_DEPLOYMENT_BUCKET}
    S3_BUCKET: ${env:S3_BUCKET}

functions:
  myfunction:
    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
    events:
      - http:
          path: api/hello
          method: post
      - http:
          path: api/testApi
          method: get

Provider

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

Custom

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

CODE_DEPLOYMENT_BUCKET 

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.

Build an API Gateway REST API with Lambda Integration

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.

Similar Reads

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....

Install Serverless

Install serverless by running the below command...

Create REST API and its resources

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

Deploy Our REST API

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

Deploy Serverless Application

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....

Other Use Cases

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

Conclusion

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....