Unable to invoke aws lambda locally using sls

Viewed 1431

Im using sls framework to launch my code and it was working fine until today. serverless.yml's configuration wasnt modified and ive tried reinstalling sls but nothing seems to work.

The following message occurs for all my repos after throwing the following command: sls invoke local -f function_name --accountId $accountId

Error message:

 
  TypeError: Cannot read property 'Fn::ImportValue' of undefined
      at /usr/local/lib/node_modules/serverless/lib/plugins/aws/invokeLocal/index.js:188:22
      at Array.map (<anonymous>)
      at /usr/local/lib/node_modules/serverless/lib/plugins/aws/invokeLocal/index.js:187:53
      at tryCatcher (/usr/local/lib/node_modules/serverless/node_modules/bluebird/js/release/util.js:16:23)
      at Function.Promise.attempt.Promise.try (/usr/local/lib/node_modules/serverless/node_modules/bluebird/js/release/method.js:39:29)
      at AwsInvokeLocal.loadEnvVars (/usr/local/lib/node_modules/serverless/lib/plugins/aws/invokeLocal/index.js:154:25)
      at AwsInvokeLocal.tryCatcher (/usr/local/lib/node_modules/serverless/node_modules/bluebird/js/release/util.js:16:23)
      at Promise._settlePromiseFromHandler (/usr/local/lib/node_modules/serverless/node_modules/bluebird/js/release/promise.js:547:31)
      at Promise._settlePromise (/usr/local/lib/node_modules/serverless/node_modules/bluebird/js/release/promise.js:604:18)
      at Promise._settlePromise0 (/usr/local/lib/node_modules/serverless/node_modules/bluebird/js/release/promise.js:649:10)
      at Promise._settlePromises (/usr/local/lib/node_modules/serverless/node_modules/bluebird/js/release/promise.js:729:18)
      at _drainQueueStep (/usr/local/lib/node_modules/serverless/node_modules/bluebird/js/release/async.js:93:12)
      at _drainQueue (/usr/local/lib/node_modules/serverless/node_modules/bluebird/js/release/async.js:86:9)
      at Async._drainQueues (/usr/local/lib/node_modules/serverless/node_modules/bluebird/js/release/async.js:102:5)
      at Immediate.Async.drainQueues [as _onImmediate] (/usr/local/lib/node_modules/serverless/node_modules/bluebird/js/release/async.js:15:14)
      at processImmediate (internal/timers.js:456:21)
      at process.topLevelDomainCallback (domain.js:137:15)```

5 Answers

Fixed by specifying the stage

I encountered the same issue and resolved it by specifying the stage with -s <STAGE> when executing sls invoke local.

Why this worked for me

I'm using serverless-dotenv-plugin, which lets me split my .env files by stage like so:

  • .env.dev
  • .env.test
  • .env.prod

Adding the stage helps specify which .env file to use with your local invocation. Without it, Serverless was trying to import .env with no stage appended, which doesn't exist on my project.

I was experiencing the same issue as well. It turns out that the latest version of Serverless seems to have bug. Downgrading to a previous version of sls fixed mine. I downgraded to verion 1.80, and it works fine now.

I figured out what's wrong. There was a warning while the invocation was running:

 Serverless Warning --------------------------------------
  A valid environment variable to satisfy the declaration 'env:MY_API_KEY' could not be found.

Since it was a warning I just ignored it and it previously worked but apparently the framework doesnt like it now. Dealing with the warning either by removing the variable or defining it resolved my issue.

For me all the environment variables had to be initialized for resolving error.

 Serverless Warning --------------------------------------
 
  A valid environment variable to satisfy the declaration 'env:<variable>' could not be found.

So I did this -

export <variable>=""

I got stuck with this issue as well. The reason is the environment variables defined in serverless.yml was not in the local .env file.

    name: ...
    handler: ...
    environment:
      API_KEY: ${env:API_KEY}
      STAGE: ${self:provider.stage}

Here, API_KEY and STAGE should exist in .env.

Related