Install serverless framework to execute nodejs application in aws lambda

Viewed 853

While testing the lambda function in api gateway i get below error message. Let me give some some background here that what I'm doing.

I've created a stack which is creating apigateway, lambda and dynamo db. The project has serverless.yml consisting of below configurations

I'm new to this setup and what I understand is that I'll have to install serverless framework (may with "sudo npm -i -g serverless") on lambda. Could you help me understanding where and how can I get this done OR is it something which Developers has to take care of?

Please let me know if you any other details.

serverless.yml

service: ps-hbo-api
plugins: ${file(./serverless-${self:provider.stage}.yml):plugins}
custom: ${file(./serverless-${self:provider.stage}.yml):custom}
vfg: ${file(./serverless-${self:provider.stage}.yml):vfg}
logs: ${file(./serverless-${self:provider.stage}.yml):logs}

serverless-prod.yml

plugins:
  - serverless-prune-plugin
  - serverless-pseudo-parameters
#Testing otherwise has to be removed  
  - serverless-http 

index.js

const serverless = require('serverless-http');
const bodyParser = require('body-parser');
const express = require('express');
const app = express();

Error message

    {"errorType":"Runtime.ImportModuleError","errorMessage":"Error: Cannot find module 'serverless-http'\nRequire stack:\n- /var/task/index.js\n- /var/runtime/UserFunction.js\n- /var/runtime/index.js","trace":["Runtime.ImportModuleError: Error: Cannot find module 'serverless-http'","Require stack:","- /var/task/index.js","- /var/runtime/UserFunction.js","- /var/runtime/index.js","    at _loadUserApp (/var/runtime/UserFunction.js:100:13)","    at Object.module.exports.load (/var/runtime/UserFunction.js:140:17)","    at Object.<anonymous> (/var/runtime/index.js:43:30)","    at Module._compile (internal/modules/cjs/loader.js:999:30)","    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)","    at Module.load (internal/modules/cjs/loader.js:863:32)","    at Function.Module._load (internal/modules/cjs/loader.js:708:14)","    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:60:12)","    at internal/main/run_main_module.js [TRUNCATED]
Fri May 14 07:53:59 UTC 2021 : Lambda execution failed with status 200 due to customer function error: Error: Cannot find module 'serverless-http'
        Require stack:
        - /var/task/index.js
        - /var/runtime/UserFunction.js
        - /var/runtime/index.js. Lambda request id: 2fe67c72-8d94-4011-b274-c7c91f10cdcf
        Fri May 14 07:53:59 UTC 2021 : Method completed with status: 502    
1 Answers

It looks like after you are deploying your lambda functions, it was not able to find the serverless-http npm dependency. Hence, I would recommend adding the serverless-plugin-include-dependencies plugin in your serverless-prod.yml so that it downloads and install the necessary dependencies while deploying your project.

After adding the plugin, here's how it should look like.

plugins:
  - serverless-prune-plugin
  - serverless-pseudo-parameters
  - serverless-plugin-include-dependencies

You can check the details of the dependency plugin here from the serverless documentation.

Related