> First and foremost, you need setup serverless Node.js application. Based on the link to follow and configure as below:
# Welcome to Serverless!
#
# This file is the main config file for your service.
# It's very minimal at this point and uses default values.
# You can always add more config options for more control.
# We've included some commented out config examples here.
# Just uncomment any of them to get that config option.
#
# For full config options, check the docs:
# docs.serverless.com
#
# Happy Coding!
service: MongoService
# app and org for use with dashboard.serverless.com
#app: your-app-name
#org: your-org-name
# You can pin your service to only deploy with a specific Serverless version
# Check out our docs for more details
frameworkVersion: '3'
provider:
name: aws
apiName: MongoService
runtime: nodejs14.x
region: ap-southeast-1
functions:
hello:
handler: handler.index
name: getConnection
description: Get PowerBI Connection from Amazon RDS
events:
- http:
path: connection
method: post
Note: The key apiName allow us to change name of API gateway in serverless application.
> Run serverless deploy to deploy API Gateway and serverless to AWS server.
serverless deploy
Deploying MongoService to stage dev (ap-southeast-1)
✔ Service deployed to stack MongoService-dev (53s)
endpoint: POST - https://qgw9zym1o5.execute-api.ap-southeast-1.amazonaws.com/dev/connection
functions:
hello: getConnection (5.9 MB)
Need a better logging experience than CloudWatch? Try our Dev Mode in console: run "serverless --console"
> Look at API Gateway dashboard
Note: The endpoint will target lambda function named getConnection(). If you open getConnection in Lambda and see the similarity.
> Here is postman for testing endpoint: https://qgw9zym1o5.execute-api.ap-southeast-1.amazonaws.com/dev/connection
> To detect what happens in lambda function, we can open CloudWatch for tracking it. So let move to monitor > View logs in CloudWatch.
For instance, here is your issue as below:
{
"errorType": "Runtime.HandlerNotFound",
"errorMessage": "handler.index is undefined or not exported",
"stack": [
"Runtime.HandlerNotFound: handler.index is undefined or not exported",
" at Object.module.exports.load (/var/runtime/UserFunction.js:304:11)",
" at Object.<anonymous> (/var/runtime/index.js:43:34)",
" at Module._compile (internal/modules/cjs/loader.js:1085:14)",
" at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)",
" at Module.load (internal/modules/cjs/loader.js:950:32)",
" at Function.Module._load (internal/modules/cjs/loader.js:790:12)",
" at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:75:12)",
" at internal/main/run_main_module.js:17:47"
]
}
> Here, the handler.js should change into index.js and need to exclude some files or folder that is unnecessary upon deployment.
# Welcome to Serverless!
#
# This file is the main config file for your service.
# It's very minimal at this point and uses default values.
# You can always add more config options for more control.
# We've included some commented out config examples here.
# Just uncomment any of them to get that config option.
#
# For full config options, check the docs:
# docs.serverless.com
#
# Happy Coding!
service: MongoService
# app and org for use with dashboard.serverless.com
#app: your-app-name
#org: your-org-name
# You can pin your service to only deploy with a specific Serverless version
# Check out our docs for more details
frameworkVersion: '3'
provider:
name: aws
apiName: MongoService
runtime: nodejs14.x
region: ap-southeast-1
layers:
database:
package:
artifact: mongodb.zip
name: database
description: contain libs for connecting mongo db
compatibleRuntimes:
- nodejs14.x
retain: true
allowedAccounts:
- '*' # ALL accounts!
functions:
hello:
handler: index.handler
name: getConnection
description: Get PowerBI Connection from Amazon RDS
layers:
- arn:aws:lambda:ap-southeast-1:181156772356:layer:mongodb:3
events:
- http:
path: connection
method: post
package:
exclude:
- node_modules/**
- package.json
- package-lock.json
- database.zip
- run.ts
include:
- index.js
Note: You need to run layers first and copy arn:aws:lambda:ap-southeast-1:181156772356:layer:database:3 after generating. To set up for function, we need create layers and assign value again.
> Go to Postman and run again.
> You can download source code from link here. If you got problem then you need to go to CloudFormation > Stacks. You can delete all stacks to ensure serverless deploy work properly.