0

I’m fairly new to AWS Lambda and I’m trying to set-up my environment to be able to develop a Lambda function, using vscode and to be able to use a debugger. The way to do this appears to be to develop a SAM (serverless application model) application, containing the lambda function. So I’ve set everything up as per the “hello world” example in the AWS tutorial, and that all seems to work OK. I have also created a standalone Lambda Function that successfully accesses the AWS-SDK to display some EC2 instance information (see below).

// Standalone Lambda Function that Works OK
const AWS = require('aws-sdk')
AWS.config.update({region: 'eu-west-2'});
const ec2 = new AWS.EC2();
exports.handler = async function(event) {
  return ec2.describeInstances().promise();
}

The problem I’m having is combining the two: that is accessing the aws-sdk from a SAM application. What isn’t helping is that when I try and use one of the pre-provided SAM templates that do access the aws-sdk (e.g. the S3 one), they won’t even build. So I guess I have two basic questions:

  1. Is SAM the answer to being able to run and debug Node.js applications locally?
  2. And if so, how do I get a working SAM application that uses the ‘aws-sdk’?

I’m on the latest version of everything. Node.js, SAM CLI, etc. The development machine is an ubuntu VM running on an ubuntu host. But the non aws-sdk example seems to work fine.

Example of the example SAM application not building.

$ sam init
Which template source would you like to use?
       1 - AWS Quick Start Templates
       2 - Custom Template Location
Choice: 1
What package type would you like to use?
       1 - Zip (artifact is a zip uploaded to S3)
       2 - Image (artifact is an image uploaded to an ECR image repository)
Package type: 1
Which runtime would you like to use?
       1 - nodejs14.x
       2 - python3.8
       3 - ruby2.7
       4 - go1.x
       5 - java11
       6 - dotnetcore3.1
       7 - nodejs12.x
       8 - nodejs10.x
       9 - python3.7
       10 - python3.6
       11 - python2.7
       12 - ruby2.5
       13 - java8.al2
       14 - java8
       15 - dotnetcore2.1
Runtime: 1
Project name [sam-app]: sam-app-sdk-test
Cloning app templates from https://github.com/aws/aws-sam-cli-app-templates
AWS quick start application templates:
       1 - Hello World Example
       2 - Step Functions Sample App (Stock Trader)
       3 - Quick Start: From Scratch
       4 - Quick Start: Scheduled Events
       5 - Quick Start: S3
       6 - Quick Start: SNS
       7 - Quick Start: SQS
       8 - Quick Start: Web Backend
Template selection: 5
   -----------------------
   Generating application:
   -----------------------
   Name: sam-app-sdk-test
   Runtime: nodejs14.x
   Dependency Manager: npm
   Application Template: quick-start-s3
   Output Directory: .
   
   Next steps can be found in the README file at ./sam-app-sdk-test/README.md

The build errors

sam build
Building codeuri: /media/sf_Virtual-machine-shr/DEV-Projects/OaC/sam-app-sdk-test runtime: nodejs14.x metadata: {} functions: ['S3Json
LoggerFunction']
Running NodejsNpmBuilder:NpmPack
Running NodejsNpmBuilder:CopyNpmrc
Running NodejsNpmBuilder:CopySource
Running NodejsNpmBuilder:NpmInstall

Build Failed                                                                                                                           
Error: NodejsNpmBuilder:NpmInstall - NPM Failed: npm WARN deprecated request@2.88.2: request has been deprecated, see https://github.c
om/request/request/issues/3142
npm WARN deprecated request-promise-native@1.0.9: request-promise-native has been deprecated because it extends the now deprecated req
uest package, see https://github.com/request/request/issues/3142
npm WARN deprecated har-validator@5.1.5: this library is no longer supported
npm WARN deprecated resolve-url@0.2.1: https://github.com/lydell/resolve-url#deprecated
npm WARN deprecated urix@0.1.0: Please see https://github.com/lydell/urix#deprecated
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@^2.1.2 (node_modules/jest-haste-map/node_modules/fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@2.3.2: wanted {"os":"darwin","arch":"any"} (current: {
"os":"linux","arch":"x64"})

npm ERR! Maximum call stack size exceeded

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/peter/.npm/_logs/2021-04-25T14_35_13_711Z-debug.log

Turning the working Lambda Function Into a SAM Application

If I take my working Lambda Function and try to deploy it as a SAM application that builds and deploys OK, but won’t run.

Running locally

$ sam local invoke
Invoking app.lambdaHandler (nodejs14.x)
Skip pulling image and use local one: amazon/aws-sam-cli-emulation-image-nodejs14.x:rapid-1.22.0.
Mounting /media/sf_Virtual-machine-shr/DEV-Projects/OaC/backups/nc-sam-awsbackup-snapshots/.aws-sam/build/ShareSnapshotsFunction as /var/task:ro,delegated inside runtime container
START RequestId: bf15ac81-7d54-48ad-bbb7-df5e196c09f0 Version: $LATEST
2021-04-25T14:28:27.244Z        undefined       ERROR   Uncaught Exception      {"errorType":"Runtime.HandlerNotFound","errorMessage":"app.lambdaHandler is undefined or not exported","stack":["Runtime.HandlerNotFound: app.lambdaHandler is undefined or not exported","    at Object.module.exports.load (/var/runtime/UserFunction.js:144:11)","    at Object.<anonymous> (/var/runtime/index.js:43:30)","    at Module._compile (internal/modules/cjs/loader.js:1063:30)","    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)","    at Module.load (internal/modules/cjs/loader.js:928:32)","    at Function.Module._load (internal/modules/cjs/loader.js:769:14)","    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)","    at internal/main/run_main_module.js:17:47"]}
time="2021-04-25T14:28:27.252" level=panic msg="ReplyStream not available"
2021/04/25 14:28:27 http: panic serving 127.0.0.1:44020: &{0xc0000f4000 map[] 2021-04-25 14:28:27.252909676 +0000 UTC m=+0.322609308 panic <nil> ReplyStream not available <nil> <nil> }
goroutine 30 [running]:
<Stuff Cut Out>
2021-04-25T14:28:27.266Z        undefined       ERROR   Uncaught Exception      {"errorType":"Error","errorMessage":"socket hang up","code":"ECONNRESET","stack":["Error: socket hang up","    at connResetException (internal/errors.js:607:14)","    at Socket.socketOnEnd (_http_client.js:493:23)","    at Socket.emit (events.js:327:22)","    at Socket.EventEmitter.emit (domain.js:467:12)","    at endReadableNT (internal/streams/readable.js:1327:12)","    at processTicksAndRejections (internal/process/task_queues.js:80:21)"]}
time="2021-04-25T14:28:27.288" level=error msg="Init failed" InvokeID= error="Runtime exited with error: exit status 129"
time="2021-04-25T14:28:27.288" level=error msg="INIT DONE failed: Runtime.ExitError"

Using "Invoke on AWS" from vscode.

Loading response...

Invocation result for arn:aws:lambda:eu-west-2:428829672862:function:nc-sam-awsbackup-snapshots-ShareSnapshotsFunction-Z9A1RR2GCQ9F
Logs:
START RequestId: f7a7d779-8ebd-4dde-987e-775f6116c2d4 Version: $LATEST
2021-04-25T14:21:24.159Z    undefined   ERROR   Uncaught Exception  {"errorType":"Error","errorMessage":"EACCES: permission denied, open '/var/task/app.js'","code":"EACCES","errno":-13,"syscall":"open","path":"/var/task/app.js","stack":["Error: EACCES: permission denied, open '/var/task/app.js'","    at Object.openSync (fs.js:476:3)","    at Object.readFileSync (fs.js:377:35)","    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1090:18)","    at Module.load (internal/modules/cjs/loader.js:928:32)","    at Function.Module._load (internal/modules/cjs/loader.js:769:14)","    at Module.require (internal/modules/cjs/loader.js:952:19)","    at require (internal/modules/cjs/helpers.js:88:18)","    at _tryRequire (/var/runtime/UserFunction.js:75:12)","    at _loadUserApp (/var/runtime/UserFunction.js:95:12)","    at Object.module.exports.load (/var/runtime/UserFunction.js:140:17)"]}
END RequestId: f7a7d779-8ebd-4dde-987e-775f6116c2d4
REPORT RequestId: f7a7d779-8ebd-4dde-987e-775f6116c2d4  Duration: 1439.52 ms    Billed Duration: 1440 ms    Memory Size: 128 MB Max Memory Used: 14 MB  
Unknown application error occurred
Error
Payload:
{"errorType":"Error","errorMessage":"EACCES: permission denied, open '/var/task/app.js'","trace":["Error: EACCES: permission denied, open '/var/task/app.js'","    at Object.openSync (fs.js:476:3)","    at Object.readFileSync (fs.js:377:35)","    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1090:18)","    at Module.load (internal/modules/cjs/loader.js:928:32)","    at Function.Module._load (internal/modules/cjs/loader.js:769:14)","    at Module.require (internal/modules/cjs/loader.js:952:19)","    at require (internal/modules/cjs/helpers.js:88:18)","    at _tryRequire (/var/runtime/UserFunction.js:75:12)","    at _loadUserApp (/var/runtime/UserFunction.js:95:12)","    at Object.module.exports.load (/var/runtime/UserFunction.js:140:17)"]}

Loading response...
Invocation result for arn:aws:lambda:eu-west-2:428829672862:function:nc-sam-awsbackup-snapshots-ShareSnapshotsFunction-Z9A1RR2GCQ9F
Logs:
START RequestId: 779c6351-0775-4f04-8a5e-987791b027bf Version: $LATEST
2021-04-25T14:31:26.397Z    undefined   ERROR   Uncaught Exception  {"errorType":"Error","errorMessage":"EACCES: permission denied, open '/var/task/app.js'","code":"EACCES","errno":-13,"syscall":"open","path":"/var/task/app.js","stack":["Error: EACCES: permission denied, open '/var/task/app.js'","    at Object.openSync (fs.js:476:3)","    at Object.readFileSync (fs.js:377:35)","    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1090:18)","    at Module.load (internal/modules/cjs/loader.js:928:32)","    at Function.Module._load (internal/modules/cjs/loader.js:769:14)","    at Module.require (internal/modules/cjs/loader.js:952:19)","    at require (internal/modules/cjs/helpers.js:88:18)","    at _tryRequire (/var/runtime/UserFunction.js:75:12)","    at _loadUserApp (/var/runtime/UserFunction.js:95:12)","    at Object.module.exports.load (/var/runtime/UserFunction.js:140:17)"]}
END RequestId: 779c6351-0775-4f04-8a5e-987791b027bf
REPORT RequestId: 779c6351-0775-4f04-8a5e-987791b027bf  Duration: 1449.59 ms    Billed Duration: 1450 ms    Memory Size: 128 MB Max Memory Used: 14 MB  
Unknown application error occurred
Error
Payload:
{"errorType":"Error","errorMessage":"EACCES: permission denied, open '/var/task/app.js'","trace":["Error: EACCES: permission denied, open '/var/task/app.js'","    at Object.openSync (fs.js:476:3)","    at Object.readFileSync (fs.js:377:35)","    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1090:18)","    at Module.load (internal/modules/cjs/loader.js:928:32)","    at Function.Module._load (internal/modules/cjs/loader.js:769:14)","    at Module.require (internal/modules/cjs/loader.js:952:19)","    at require (internal/modules/cjs/helpers.js:88:18)","    at _tryRequire (/var/runtime/UserFunction.js:75:12)","    at _loadUserApp (/var/runtime/UserFunction.js:95:12)","    at Object.module.exports.load (/var/runtime/UserFunction.js:140:17)"]}

Running the Lambda Function within the AWS Console

Gives the same EACCES: permission denied

P Burke
  • 1,630
  • 2
  • 17
  • 31
  • see `./sam-app-sdk-test/README.md` – petey Apr 26 '21 at 01:32
  • Does this answer your question? [Maximum call stack size exceeded error](https://stackoverflow.com/questions/6095530/maximum-call-stack-size-exceeded-error) – petey Apr 26 '21 at 01:33
  • @petey I've had a look at the README.md and that doesn't really help; in fact the README.md is not for the S3 example, its for the SQS! The link to the other SO question doesn't really help either - the code that is failing appears to be the code behind `sam build`, not the app I'm trying to write. – P Burke Apr 26 '21 at 11:02
  • SQS!!!! THATS CRAZY!!!!! I'll see about filing a bug for that. You can invoke a function that wont build. Can you remove all the info about invoking (its not your issue) and add your lambda handler code to it? Perhaps will can trace down the build issue? – petey Apr 26 '21 at 21:20
  • @petey. I'm afraid I don't understand what you mean. I can happily run a SAM app (local or on AWS), as long as it doesn't use the aws-sdk. And I can run a standalone Lambda function that does use the aws-sdk. What I can not do is use aws-sdk from a SAM app, even with the standard examples (which don't even build). So I'm not sure what you are asking me to add my code to? – P Burke Apr 27 '21 at 14:47
  • something is goofy with your app code. Lets have a look at your lamda function and package.json – petey Apr 27 '21 at 20:09

0 Answers0