Repository: serverless/serverless (JavaScript)
Date: 2019-12-05 08:45:58+00:00
Task type: Bug Fix

Issue:
Serverless invoke local plugin for aws provider seems to be broken after updating to 1.59
# Bug Report

## Description

<!-- Please use https://forum.serverless.com, StackOverflow or other forums for Q&A -->
<!-- Please answer ALL the question below. Otherwise we probably have to close the issue due to missing information -->

1. What did you do?
**Upgraded serverless from 1.58 to 1.59 using npm global flag**
2. What happened?
**When attempting to invoke any function locally, the following error is thrown**
`TypeError:` Cannot read property 'accessKeyId' of null`

3. What should've happened?
**Runs a function locally without issue. Downgrading serverless back to 1.58 fixes the problem.**
4. What's the content of your `serverless.yml` file?
```yaml
service:
  name: live
# app and org for use with dashboard.serverless.com
#app: your-app-name
#org: your-org-name

custom:
  webpack:
    webpackConfig: ./webpack.config.js
    includeModules: true

# Add the serverless-webpack plugin
plugins:
  - serverless-webpack

provider:
  name: aws
  runtime: nodejs10.x
  apiGateway:
    minimumCompressionSize: 1024 # Enable gzip compression for responses > 1 KB
  environment:
    AWS_NODEJS_CONNECTION_REUSE_ENABLED: 1

functions:
  subscribe:
    handler: src/handler.subscribe
    events:
      - http:
          method: post
          path: subscribe
  unsubscribe:
    handler: src/handler.unsubscribe
    events:
      - http:
          method: delete
          path: unsubscribe
```
5. What's the output you get when you use the `SLS_DEBUG=*` environment variable (e.g. `SLS_DEBUG=* serverless deploy`)
```
Serverless: Load command interactiveCli
Serverless: Load command config
Serverless: Load command config:credentials
Serverless: Load command config:tabcompletion
Serverless: Load command config:tabcompletion:install
Serverless: Load command config:tabcompletion:uninstall
Serverless: Load command create
Serverless: Load command install
Serverless: Load command package
Serverless: Load command deploy
Serverless: Load command deploy:function
Serverless: Load command deploy:list
Serverless: Load command deploy:list:functions
Serverless: Load command invoke
Serverless: Load command invoke:local
Serverless: Load command info
Serverless: Load command logs
Serverless: Load command metrics
Serverless: Load command print
Serverless: Load command remove
Serverless: Load command rollback
Serverless: Load command rollback:function
Serverless: Load command slstats
Serverless: Load command plugin
Serverless: Load command plugin
Serverless: Load command plugin:install
Serverless: Load command plugin
Serverless: Load command plugin:uninstall
Serverless: Load command plugin
Serverless: Load command plugin:list
Serverless: Load command plugin
Serverless: Load command plugin:search
Serverless: Load command config
Serverless: Load command config:credentials
Serverless: Load command rollback
Serverless: Load command rollback:function
Serverless: Load command webpack
Serverless: Load command login
Serverless: Load command logout
Serverless: Load command generate-event
Serverless: Load command test
Serverless: Load command dashboard
Serverless: Invoke invoke:local
 
  Type Error ---------------------------------------------
 
  TypeError: Cannot read property 'accessKeyId' of null
      at AwsInvokeLocal.loadEnvVars (/home/wk/.nvm/versions/node/v10.15.3/lib/node_modules/serverless/lib/plugins/aws/invokeLocal/index.js:133:27)
  From previous event:
      at Object.before:invoke:local:loadEnvVars [as hook] (/home/wk/.nvm/versions/node/v10.15.3/lib/node_modules/serverless/lib/plugins/aws/invokeLocal/index.js:33:12)
      at BbPromise.reduce (/home/wk/.nvm/versions/node/v10.15.3/lib/node_modules/serverless/lib/classes/PluginManager.js:489:55)
  From previous event:
      at PluginManager.invoke (/home/wk/.nvm/versions/node/v10.15.3/lib/node_modules/serverless/lib/classes/PluginManager.js:489:22)
      at getHooks.reduce.then (/home/wk/.nvm/versions/node/v10.15.3/lib/node_modules/serverless/lib/classes/PluginManager.js:524:24)
  From previous event:
      at PluginManager.run (/home/wk/.nvm/versions/node/v10.15.3/lib/node_modules/serverless/lib/classes/PluginManager.js:524:8)
      at variables.populateService.then (/home/wk/.nvm/versions/node/v10.15.3/lib/node_modules/serverless/lib/Serverless.js:115:33)
      at runCallback (timers.js:705:18)
      at tryOnImmediate (timers.js:676:5)
      at processImmediate (timers.js:658:5)
      at process.topLevelDomainCallback (domain.js:120:23)
  From previous event:
      at Serverless.run (/home/wk/.nvm/versions/node/v10.15.3/lib/node_modules/serverless/lib/Serverless.js:102:74)
      at serverless.init.then (/home/wk/.nvm/versions/node/v10.15.3/lib/node_modules/serverless/bin/serverless.js:72:30)
      at /home/wk/.nvm/versions/node/v10.15.3/lib/node_modules/serverless/node_modules/graceful-fs/graceful-fs.js:111:16
      at /home/wk/Github/live/node_modules/graceful-fs/graceful-fs.js:57:14
      at /home/wk/.nvm/versions/node/v10.15.3/lib/node_modules/serverless/node_modules/graceful-fs/graceful-fs.js:45:10
      at FSReqWrap.args [as oncomplete] (fs.js:140:20)
  From previous event:
      at initializeErrorReporter.then (/home/wk/.nvm/versions/node/v10.15.3/lib/node_modules/serverless/bin/serverless.js:72:8)
      at runCallback (timers.js:705:18)
      at tryOnImmediate (timers.js:676:5)
      at processImmediate (timers.js:658:5)
      at process.topLevelDomainCallback (domain.js:120:23)
  From previous event:
      at Object.<anonymous> (/home/wk/.nvm/versions/node/v10.15.3/lib/node_modules/serverless/bin/serverless.js:61:4)
      at Module._compile (internal/modules/cjs/loader.js:701:30)
      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)
      at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)
 
  Get Support --------------------------------------------
     Docs:          docs.serverless.com
     Bugs:          github.com/serverless/serverless/issues
     Issues:        forum.serverless.com
 
  Your Environment Information ---------------------------
     Operating System:          linux
     Node Version:              10.15.3
     Framework Version:         1.59.0
     Plugin Version:            3.2.5
     SDK Version:               2.2.1
     Components Core Version:   1.1.2
     Components CLI Version:    1.4.0
```

Similar or dependent issues: **N/A**


Issue discussion:

Solution patch:
diff --git a/.travis.yml b/.travis.yml
index e7c6ad3d821..e4864702fdd 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -22,6 +22,7 @@ install:
 branches:
   only:
     - master # Do not build PR branches
+    - release-fast-track # Do not build PR branches
     - /^v\d+\.\d+\.\d+$/ # Ensure to build release tags
 
 env:
@@ -70,7 +71,7 @@ jobs:
         - npm run lint-updated
         - npm test -- -b
 
-    # master branch and version tags
+    # release target branch and version tags
     - name: 'Lint, Unit Tests - Linux - Node.js v12'
       if: type != pull_request
       node_js: 12
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 66e7506739c..7d5c6dad1ba 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,3 +1,13 @@
+# Changelog
+
+All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
+
+### [1.59.1](https://github.com/serverless/serverless/compare/v1.59.0...v1.59.1) (2019-12-05)
+
+### Bug Fixes
+
+- Fix mishandling of cachedCredentials in invokeLocal ([699e78d](https://github.com/serverless/serverless/commit/699e78d251b7cbb3e6553c6d8554c2bf568be1fb)), closes [#7050](https://github.com/serverless/serverless/issues/7050), regression introduced with [#7044](https://github.com/serverless/serverless/issues/7044)
+
 # 1.59.0 (2019-12-04)
 
 - [Fix spelling and typos in docs, code variables and code comments](https://github.com/serverless/serverless/pull/6986)
diff --git a/lib/plugins/aws/invokeLocal/index.js b/lib/plugins/aws/invokeLocal/index.js
index d68e027e386..15f828ddfc2 100644
--- a/lib/plugins/aws/invokeLocal/index.js
+++ b/lib/plugins/aws/invokeLocal/index.js
@@ -128,16 +128,18 @@ class AwsInvokeLocal {
       NODE_PATH: '/var/runtime:/var/task:/var/runtime/node_modules',
     };
 
-    const { cachedCredentials } = this.provider;
     const credentialEnvVars = {};
-    if (cachedCredentials.accessKeyId) {
-      credentialEnvVars.AWS_ACCESS_KEY_ID = cachedCredentials.accessKeyId;
-    }
-    if (cachedCredentials.secretAccessKey) {
-      credentialEnvVars.AWS_SECRET_ACCESS_KEY = cachedCredentials.secretAccessKey;
-    }
-    if (cachedCredentials.sessionToken) {
-      credentialEnvVars.AWS_SESSION_TOKEN = cachedCredentials.sessionToken;
+    const { cachedCredentials } = this.provider;
+    if (cachedCredentials) {
+      if (cachedCredentials.accessKeyId) {
+        credentialEnvVars.AWS_ACCESS_KEY_ID = cachedCredentials.accessKeyId;
+      }
+      if (cachedCredentials.secretAccessKey) {
+        credentialEnvVars.AWS_SECRET_ACCESS_KEY = cachedCredentials.secretAccessKey;
+      }
+      if (cachedCredentials.sessionToken) {
+        credentialEnvVars.AWS_SESSION_TOKEN = cachedCredentials.sessionToken;
+      }
     }
 
     // profile override from config
diff --git a/package.json b/package.json
index 8ca9ad9d2a7..657640f3c40 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
 {
   "name": "serverless",
-  "version": "1.59.0",
+  "version": "1.59.1",
   "engines": {
     "node": ">=6.0"
   },


Test patch:
diff --git a/lib/plugins/aws/invokeLocal/index.test.js b/lib/plugins/aws/invokeLocal/index.test.js
index ff42324977a..9128e03be40 100644
--- a/lib/plugins/aws/invokeLocal/index.test.js
+++ b/lib/plugins/aws/invokeLocal/index.test.js
@@ -352,6 +352,18 @@ describe('AwsInvokeLocal', () => {
         });
     });
 
+    it('it should work without cached credentials set', () => {
+      provider.cachedCredentials = null;
+      return awsInvokeLocal
+        .loadEnvVars()
+
+        .then(() => {
+          expect('AWS_SESSION_TOKEN' in process.env).to.equal(false);
+          expect('AWS_ACCESS_KEY_ID' in process.env).to.equal(false);
+          expect('AWS_SECRET_ACCESS_KEY' in process.env).to.equal(false);
+        });
+    });
+
     it('should fallback to service provider configuration when options are not available', () => {
       awsInvokeLocal.provider.options.region = null;
       awsInvokeLocal.serverless.service.provider.region = 'us-west-1';