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.