aws-sam-cli: NODE_PATH is not set in the Container when in debug mode
Description
I was using sam 0.13 and I could debug my function using VS Code with no problems, after upgrading my lambdas do nodejs10.x (coming from 8.10) I had to update my sam version due compatbility, and now I no longer can debug my code, either in Ubuntu or macOS environment. I’m getting following error:
{
"errorType": "Runtime.ImportModuleError",
"errorMessage": "Error: Cannot find module 'aws-sdk'"
}
Does this package shouldn’t be inside container?
Additional point:
- It runs normally without debbuging mode (without -d 5858).
- Steps provided used to work using SAM 0.13 with node8.10 Runtime.
EDIT: I’ve just tried using SAM 0.17.0 with node8.10 Runtime and it worked. But ,using node10.x it doesn’t.
Steps to reproduce
project structure
node_modules/
template.yml
webpack.config.js
src/
-- lambdas/
---- records/
------ index.js
template.yml
AWSTemplateFormatVersion: '2010-09-09'
Transform: ["AWS::Serverless-2016-10-31", "CorsFixer"]
Description: >
auditing-back-app
Globals:
Function:
Runtime: nodejs10.x
Timeout: 60
MemorySize: 128
Api:
Cors:
AllowOrigin: "'*'"
AllowHeaders: "'Content-Type,Authorization'"
Parameters:
Environment:
Type: String
RecordsEndpointsFunction:
Type: AWS::Serverless::Function
Properties:
Description: !Sub ${Environment}
CodeUri: dist/records/records.zip
Handler: index.handler
MemorySize: 1536
Timeout: 500
Policies:
- AWSLambdaBasicExecutionRole
- AmazonDynamoDBFullAccess
- AmazonS3FullAccess
- AmazonSESFullAccess
- AmazonSQSFullAccess
Events:
UserEndpoints:
Type: Api
Properties:
Path: /users/{userId}/parkinglots/{parkingLotId}/records
Method: any
RestApiId: !Ref HttpApiGateway
NewFileUploaded:
Type: SQS
Properties:
Queue: !Sub arn:aws:sqs:${AWS::Region}:{hidden}:auditing-new-file-to-process-${Environment}
BatchSize: 1
RealTimeUpdater:
Type: Schedule
Properties:
Schedule: rate(5 minutes)
webpack.config.js
const glob = require('glob');
const path = require('path');
const ZipPlugin = require('zip-webpack-plugin');
module.exports = (env, argv) => {
console.log(`Buiding in ${argv.mode} mode`);
const functionsToBuild = glob
.sync('./src/lambdas/*/index.*')
.map(x => x.match(/(?<=src\/lambdas\/).*/)[0]);
return functionsToBuild.map(fxn => {
const lambdaName = fxn.split('/')[0];
const triggerName = fxn.split('/')[1];
return {
entry: path.join(__dirname, 'src', 'lambdas', lambdaName, triggerName),
devtool: 'cheap-module-eval-source-map',
output: {
filename: 'index.js',
path: path.join(__dirname, 'dist', lambdaName),
libraryTarget: 'commonjs2'
},
module: {
rules: [
{
test: /\.ts(x?)$/,
use: [
'ts-loader'
]
},
]
},
resolve: {
extensions: ['.ts', '.js']
},
optimization: {
minimize: false,
namedModules: true
},
plugins: [
new ZipPlugin({
path: path.join(__dirname, 'dist', lambdaName),
pathPrefix: '',
filename: `${lambdaName}.zip`
})
],
externals: {
'aws-sdk': 'aws-sdk',
'awslambda': 'awslambda',
'dynamodb-doc': 'dynamodb-doc',
'imagemagick': 'imagemagick'
},
target: 'node',
node: {
__filename: false,
__dirname: false
},
stats: 'errors-only',
bail: true,
}
});
}
package.json
{
"name": "serverless-auditing-back",
"version": "1.0.0",
"description": "This is a sample template for sam-app - Below is a brief explanation of what we have generated for you:",
"main": "index.js",
"scripts": {
"install": "find ./node_modules/* -mtime +10950 -exec touch {} \\;",
"test": "echo \"Error: no test specified\" && exit 1",
"build": "node_modules/.bin/webpack --config webpack.config.js --mode=development",
"build-prod": "node_modules/.bin/webpack --config webpack.config.js --mode=production"
},
"keywords": [],
"private": true,
"author": "",
"license": "ISC",
"devDependencies": {
"glob": "^7.1.4",
"terser-webpack-plugin": "^1.3.0",
"webpack": "^4.35.0",
"webpack-cli": "^3.3.5",
"zip-webpack-plugin": "^3.0.0"
},
"dependencies": {
"@types/node": "^12.0.10",
"axios": "^0.19.0",
"crypto": "^1.0.1",
"jsonwebtoken": "^8.5.1",
"moment": "^2.24.0",
"moment-timezone": "^0.5.25",
"sinesp-api": "^1.4.0",
"ts-loader": "^6.0.4",
"typescript": "^3.5.2",
"uuid": "^3.3.2"
}
}
launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "Attach to SAM CLI",
"type": "node",
"request": "attach",
"address": "localhost",
"port": 5858,
// From the sam init example, it would be "${workspaceRoot}/hello_world"
"localRoot": "${workspaceRoot}/",
"remoteRoot": "/var/task",
"protocol": "inspector",
"stopOnEntry": false
}
]
}
commands:
npm run build
sam local invoke -d 5858 RecordsEndpointsFunction
Observed result
Fetching lambci/lambda:nodejs10.x Docker container image… 2019-06-28 11:13:01 Mounting /tmp/tmpoVDOPI as /var/task:ro,delegated inside runtime container 2019-06-28 11:13:01 http://localhost:None “POST /v1.35/containers/create HTTP/1.1” 201 201 2019-06-28 11:13:01 http://localhost:None “GET /v1.35/containers/0c56b87b7a0f7ee5597371a544ffc652a9836bed4eb31d8b6a34c469aeef885d/json HTTP/1.1” 200 None 2019-06-28 11:13:01 http://localhost:None “GET /v1.35/containers/0c56b87b7a0f7ee5597371a544ffc652a9836bed4eb31d8b6a34c469aeef885d/json HTTP/1.1” 200 None 2019-06-28 11:13:02 http://localhost:None “POST /v1.35/containers/0c56b87b7a0f7ee5597371a544ffc652a9836bed4eb31d8b6a34c469aeef885d/start HTTP/1.1” 204 0 2019-06-28 11:13:02 Setting up SIGTERM interrupt handler 2019-06-28 11:13:02 http://localhost:None “GET /v1.35/containers/0c56b87b7a0f7ee5597371a544ffc652a9836bed4eb31d8b6a34c469aeef885d/json HTTP/1.1” 200 None 2019-06-28 11:13:02 http://localhost:None “POST /containers/0c56b87b7a0f7ee5597371a544ffc652a9836bed4eb31d8b6a34c469aeef885d/attach?stream=1&stdin=0&logs=1&stderr=1&stdout=1 HTTP/1.1” 101 0 Debugger listening on ws://0.0.0.0:5858/6f13348c-d0c6-479b-92d8-7076ac53cd7c For help, see: https://nodejs.org/en/docs/inspector Debugger attached. 2019-06-28T14:13:13.320Z undefined ERROR Uncaught Exception {“errorType”:“Runtime.ImportModuleError”,“errorMessage”:“Error: Cannot find module ‘aws-sdk’”,“stack”:[“Runtime.ImportModuleError: Error: Cannot find module ‘aws-sdk’”," at _loadUserApp (/var/runtime/UserFunction.js💯13)“,” at Object.module.exports.load (/var/runtime/UserFunction.js:140:17)“,” at Object.<anonymous> (/var/runtime/index.js:36:30)“,” at Module._compile (internal/modules/cjs/loader.js:698:14)“,” at Object.Module._extensions…js (internal/modules/cjs/loader.js:712:10)“,” at Module.load (internal/modules/cjs/loader.js:600:32)“,” at tryModuleLoad (internal/modules/cjs/loader.js:539:12)“,” at Function.Module._load (internal/modules/cjs/loader.js:531:3)“,” at Function.Module.runMain (internal/modules/cjs/loader.js:754:12)“,” at startup (internal/bootstrap/node.js:283:19)"]} START RequestId: 52fdfc07-2182-154f-163f-5f0f9a621d72 Version: $LATEST END RequestId: 52fdfc07-2182-154f-163f-5f0f9a621d72 REPORT RequestId: 52fdfc07-2182-154f-163f-5f0f9a621d72 Init Duration: 10554.03 ms Duration: 0.00 ms Billed Duration: 100 ms Memory Size: 1536 MB Max Memory Used: 86 MB { “errorType”: “Runtime.ImportModuleError”, “errorMessage”: “Error: Cannot find module ‘aws-sdk’” } 2019-06-28 11:13:13 http://localhost:None “GET /v1.35/containers/0c56b87b7a0f7ee5597371a544ffc652a9836bed4eb31d8b6a34c469aeef885d/json HTTP/1.1” 200 None 2019-06-28 11:13:13 http://localhost:None “DELETE /v1.35/containers/0c56b87b7a0f7ee5597371a544ffc652a9836bed4eb31d8b6a34c469aeef885d?force=True&link=False&v=False HTTP/1.1” 204 0
Expected result
Run function normally
Additional environment details (Ex: Windows, Mac, Amazon Linux etc)
- OS: Tried in Ubuntu 18.04.2 LTS and macOS Mojave 10.14.5
sam --version
: SAM CLI, version 0.17.0
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Reactions: 4
- Comments: 19 (11 by maintainers)
Commits related to this issue
- fix(debug): Add NODE_PATH env var when debugging Node10/12 To support debugging for Nodejs10.x and Nodejs12.x, SAM CLI overrides the entrypoint to the container. In newer runtimes (that are based on ... — committed to jfuss/aws-sam-cli by jfuss 5 years ago
- fix(debug): Add NODE_PATH env var when debugging Node10/12 (#1646) To support debugging for Nodejs10.x and Nodejs12.x, SAM CLI overrides the entrypoint to the container. In newer runtimes (that are ... — committed to aws/aws-sam-cli by jfuss 5 years ago
TBH the best solution would be to get the Lambda team to support extra args passed in to the bootstrap – that way you’re not gonna get out of sync with any changes they make!