Easy way to Deploy Lambda and API Gateway with Serverless Framework
There are so many approaches to deploy Lambda function steamlessly into AWS Cloud such as SAM template, CodeDeploy, Cloudformation, Terraform, Serverless and so on. Among them, today I’m going to use to deploy Lambda and API Gateway with serverless framework. If you’re new to Serverless framework, please go following link https://serverless.com/framework/docs and read about its documentation.
The Serverless Framework helps you build serverless apps with radically less overhead and cost. It provides a powerful, unified experience to develop, deploy, test, secure and monitor your serverless applications.
As mentioned above, deploying Lambda function with Serverless is kinda easy to compare with SAM, CodeDeploy and so on. If you’re NodeJS developer, it’s quite easy to deploy your nodejs function to AWS Lambda with Serverless framework.
Please check below figure that how Serverless framework suppose to deploy your nodejs application to AWS Lambda.
Ok, now I’ll show how to deploy my nodejs application to AWS Lambda.
- Install serverless framework with NPM first.
npm i serverless
2. Create hello world nodejs function and file name will be index.js
const express = require('express')
const serverless = require('serverless-http')
const app = express()
app.get('/', async (req, res, next) => {
res.status(200).send('Hello World!')
})
module.exports.server = serverless(app)
3. Create AWS credentials
sls config credentials --provider aws --key ACCESS_KEY --secret SECRET_KEY
PS: for serverless, you can use whatever you prefer between serverless or sls.
4. Create serverless.yml file and under same folder with index.js file
4.1 I will create Lambda function and its configuration first.
functions:
app:
handler: index.handler
events:
- http:
method: any
path: /{proxy+}
cors: true
4.2 I will configure its Lambda function memory, runtime and IAM role as well. Ok, that Lambda function will do CRUD into DynamoDB table and access to S3 bucket as well.
provider:
name: aws
runtime: nodejs8.10
stage: dev
region: ap-southeast-1
memorySize: 128
iamRoleStatements:
- Effect: Allow
Action:
- dynamodb:*
Resource: "arn:aws:dynamodb:ap-southeast-1:*:*"
- Effect: Allow
Action:
- s3:*
Resource:
- arn:aws:s3:::mybucket/*
- arn:aws:s3:::mybucket
- Effect: Allow
Action:
- lambda:InvokeFunction
Resource: "*"
4.3 I’ll create AWS API Gateway to be integrated with that Lambda function and create custom main as well.
Please keep in mind that custom domain URL will be api.ppshein.com and we must have to create SSL certificate for that domain in AWS ACM first before deploying that nodejs function to AWS Lambda.
custom:
customDomain:
domainName: api.ppshein.com
basePath: ''
stage: ${self:provider.stage}
createRoute53Record: true
So far, my serverless.yml file is completed and now I’m about to deploy that nodejs application to AWS Lambda. As I’m creating custom domain name, I need to create that custom domain in AWS with serverless frame like that,
serverless create_domain
And it would take 25 or 30 mins as least. After that, I’m going to deploy my nodejs application to AWS Lambda as follow:
serverless deploy
Once deployed you will be able to test the API using cURL or Postman.