title: AWS SAM Lambda Application Development + AWS SDK -- Can't get a working example
body: <p>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 <a href="https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-getting-started-hello-world.html" rel="nofollow noreferrer">AWS tutorial</a>, 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).</p>
  <pre><code>// 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();
  }
  </code></pre>
  <p>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:</p>
  <ol>
  <li>Is SAM the answer to being able to run and debug Node.js applications locally?</li>
  <li>And if so, how do I get a working SAM application that uses the ‘aws-sdk’?</li>
  </ol>
  <p>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.</p>
  <h2>Example of the example SAM application not building.</h2>
  <pre><code>$ 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
  </code></pre>
  <h3>The build errors</h3>
  <pre><code>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 {&quot;os&quot;:&quot;darwin&quot;,&quot;arch&quot;:&quot;any&quot;} (current: {
  &quot;os&quot;:&quot;linux&quot;,&quot;arch&quot;:&quot;x64&quot;})
  
  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
  </code></pre>
  <h2>Turning the working Lambda Function Into a SAM Application</h2>
  <p>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.</p>
  <h3>Running locally</h3>
  <pre><code>$ 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      {&quot;errorType&quot;:&quot;Runtime.HandlerNotFound&quot;,&quot;errorMessage&quot;:&quot;app.lambdaHandler is undefined or not exported&quot;,&quot;stack&quot;:[&quot;Runtime.HandlerNotFound: app.lambdaHandler is undefined or not exported&quot;,&quot;    at Object.module.exports.load (/var/runtime/UserFunction.js:144:11)&quot;,&quot;    at Object.&lt;anonymous&gt; (/var/runtime/index.js:43:30)&quot;,&quot;    at Module._compile (internal/modules/cjs/loader.js:1063:30)&quot;,&quot;    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)&quot;,&quot;    at Module.load (internal/modules/cjs/loader.js:928:32)&quot;,&quot;    at Function.Module._load (internal/modules/cjs/loader.js:769:14)&quot;,&quot;    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)&quot;,&quot;    at internal/main/run_main_module.js:17:47&quot;]}
  time=&quot;2021-04-25T14:28:27.252&quot; level=panic msg=&quot;ReplyStream not available&quot;
  2021/04/25 14:28:27 http: panic serving 127.0.0.1:44020: &amp;{0xc0000f4000 map[] 2021-04-25 14:28:27.252909676 +0000 UTC m=+0.322609308 panic &lt;nil&gt; ReplyStream not available &lt;nil&gt; &lt;nil&gt; }
  goroutine 30 [running]:
  &lt;Stuff Cut Out&gt;
  2021-04-25T14:28:27.266Z        undefined       ERROR   Uncaught Exception      {&quot;errorType&quot;:&quot;Error&quot;,&quot;errorMessage&quot;:&quot;socket hang up&quot;,&quot;code&quot;:&quot;ECONNRESET&quot;,&quot;stack&quot;:[&quot;Error: socket hang up&quot;,&quot;    at connResetException (internal/errors.js:607:14)&quot;,&quot;    at Socket.socketOnEnd (_http_client.js:493:23)&quot;,&quot;    at Socket.emit (events.js:327:22)&quot;,&quot;    at Socket.EventEmitter.emit (domain.js:467:12)&quot;,&quot;    at endReadableNT (internal/streams/readable.js:1327:12)&quot;,&quot;    at processTicksAndRejections (internal/process/task_queues.js:80:21)&quot;]}
  time=&quot;2021-04-25T14:28:27.288&quot; level=error msg=&quot;Init failed&quot; InvokeID= error=&quot;Runtime exited with error: exit status 129&quot;
  time=&quot;2021-04-25T14:28:27.288&quot; level=error msg=&quot;INIT DONE failed: Runtime.ExitError&quot;
  </code></pre>
  <h3>Using &quot;Invoke on AWS&quot; from vscode.</h3>
  <p>Loading response...</p>
  <pre><code>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  {&quot;errorType&quot;:&quot;Error&quot;,&quot;errorMessage&quot;:&quot;EACCES: permission denied, open '/var/task/app.js'&quot;,&quot;code&quot;:&quot;EACCES&quot;,&quot;errno&quot;:-13,&quot;syscall&quot;:&quot;open&quot;,&quot;path&quot;:&quot;/var/task/app.js&quot;,&quot;stack&quot;:[&quot;Error: EACCES: permission denied, open '/var/task/app.js'&quot;,&quot;    at Object.openSync (fs.js:476:3)&quot;,&quot;    at Object.readFileSync (fs.js:377:35)&quot;,&quot;    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1090:18)&quot;,&quot;    at Module.load (internal/modules/cjs/loader.js:928:32)&quot;,&quot;    at Function.Module._load (internal/modules/cjs/loader.js:769:14)&quot;,&quot;    at Module.require (internal/modules/cjs/loader.js:952:19)&quot;,&quot;    at require (internal/modules/cjs/helpers.js:88:18)&quot;,&quot;    at _tryRequire (/var/runtime/UserFunction.js:75:12)&quot;,&quot;    at _loadUserApp (/var/runtime/UserFunction.js:95:12)&quot;,&quot;    at Object.module.exports.load (/var/runtime/UserFunction.js:140:17)&quot;]}
  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:
  {&quot;errorType&quot;:&quot;Error&quot;,&quot;errorMessage&quot;:&quot;EACCES: permission denied, open '/var/task/app.js'&quot;,&quot;trace&quot;:[&quot;Error: EACCES: permission denied, open '/var/task/app.js'&quot;,&quot;    at Object.openSync (fs.js:476:3)&quot;,&quot;    at Object.readFileSync (fs.js:377:35)&quot;,&quot;    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1090:18)&quot;,&quot;    at Module.load (internal/modules/cjs/loader.js:928:32)&quot;,&quot;    at Function.Module._load (internal/modules/cjs/loader.js:769:14)&quot;,&quot;    at Module.require (internal/modules/cjs/loader.js:952:19)&quot;,&quot;    at require (internal/modules/cjs/helpers.js:88:18)&quot;,&quot;    at _tryRequire (/var/runtime/UserFunction.js:75:12)&quot;,&quot;    at _loadUserApp (/var/runtime/UserFunction.js:95:12)&quot;,&quot;    at Object.module.exports.load (/var/runtime/UserFunction.js:140:17)&quot;]}
  
  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  {&quot;errorType&quot;:&quot;Error&quot;,&quot;errorMessage&quot;:&quot;EACCES: permission denied, open '/var/task/app.js'&quot;,&quot;code&quot;:&quot;EACCES&quot;,&quot;errno&quot;:-13,&quot;syscall&quot;:&quot;open&quot;,&quot;path&quot;:&quot;/var/task/app.js&quot;,&quot;stack&quot;:[&quot;Error: EACCES: permission denied, open '/var/task/app.js'&quot;,&quot;    at Object.openSync (fs.js:476:3)&quot;,&quot;    at Object.readFileSync (fs.js:377:35)&quot;,&quot;    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1090:18)&quot;,&quot;    at Module.load (internal/modules/cjs/loader.js:928:32)&quot;,&quot;    at Function.Module._load (internal/modules/cjs/loader.js:769:14)&quot;,&quot;    at Module.require (internal/modules/cjs/loader.js:952:19)&quot;,&quot;    at require (internal/modules/cjs/helpers.js:88:18)&quot;,&quot;    at _tryRequire (/var/runtime/UserFunction.js:75:12)&quot;,&quot;    at _loadUserApp (/var/runtime/UserFunction.js:95:12)&quot;,&quot;    at Object.module.exports.load (/var/runtime/UserFunction.js:140:17)&quot;]}
  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:
  {&quot;errorType&quot;:&quot;Error&quot;,&quot;errorMessage&quot;:&quot;EACCES: permission denied, open '/var/task/app.js'&quot;,&quot;trace&quot;:[&quot;Error: EACCES: permission denied, open '/var/task/app.js'&quot;,&quot;    at Object.openSync (fs.js:476:3)&quot;,&quot;    at Object.readFileSync (fs.js:377:35)&quot;,&quot;    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1090:18)&quot;,&quot;    at Module.load (internal/modules/cjs/loader.js:928:32)&quot;,&quot;    at Function.Module._load (internal/modules/cjs/loader.js:769:14)&quot;,&quot;    at Module.require (internal/modules/cjs/loader.js:952:19)&quot;,&quot;    at require (internal/modules/cjs/helpers.js:88:18)&quot;,&quot;    at _tryRequire (/var/runtime/UserFunction.js:75:12)&quot;,&quot;    at _loadUserApp (/var/runtime/UserFunction.js:95:12)&quot;,&quot;    at Object.module.exports.load (/var/runtime/UserFunction.js:140:17)&quot;]}
  </code></pre>
  <h3>Running the Lambda Function within the AWS Console</h3>
  <p>Gives the same EACCES: permission denied</p>