Repository: serverless/serverless (JavaScript)
Date: 2019-07-25 12:47:43+00:00
Task type: Bug Fix

Issue:
`sls package` is demanding a default profile since the latest version?
# This is a Bug Report

Since the last update `sls package` requires a default profile to be set in the aws config ( or `--aws-profile` to be given ).

Not sure why the decision was made to require the aws profile even during packaging, or if this is a bug. 

It is quite anoying to us as we have a number of linting and sanity checks on the generated cf templates for which we would then have to add some aws credentials in our CI/CD for.

## Description

- What went wrong?
Running `sls package` without the default provider set or adding `--aws-profile` breaks. 

- What did you expect should have happened?
It should package the app without the need for the default profile to be set like it did before.

- What was the config you used?
The default one 

```
service: test

provider:
  name: aws
  runtime: nodejs10.x
functions:
  hello:
    handler: handler.hello
```

- What stacktrace or error message from your provider did you see?

```
Unhandled rejection ServerlessError: AWS provider credentials not found. Learn how to set up AWS provider credentials in our docs here: <http://slss.io/aws-creds-setup>.
    at BbPromise.catch.then.identity (/Users/philipp/node/dev/dh/sfa-api/node_modules/serverless/lib/plugins/aws/lib/validate.js:46:19)
    at tryCatcher (/Users/philipp/node/dev/dh/sfa-api/node_modules/bluebird/js/release/util.js:16:23)
    at Promise._settlePromiseFromHandler (/Users/philipp/node/dev/dh/sfa-api/node_modules/bluebird/js/release/promise.js:517:31)
    at Promise._settlePromise (/Users/philipp/node/dev/dh/sfa-api/node_modules/bluebird/js/release/promise.js:574:18)
    at Promise._settlePromise0 (/Users/philipp/node/dev/dh/sfa-api/node_modules/bluebird/js/release/promise.js:619:10)
    at Promise._settlePromises (/Users/philipp/node/dev/dh/sfa-api/node_modules/bluebird/js/release/promise.js:699:18)
    at _drainQueueStep (/Users/philipp/node/dev/dh/sfa-api/node_modules/bluebird/js/release/async.js:138:12)
    at _drainQueue (/Users/philipp/node/dev/dh/sfa-api/node_modules/bluebird/js/release/async.js:131:9)
    at Async._drainQueues (/Users/philipp/node/dev/dh/sfa-api/node_modules/bluebird/js/release/async.js:147:5)
    at Immediate.Async.drainQueues [as _onImmediate] (/Users/philipp/node/dev/dh/sfa-api/node_modules/bluebird/js/release/async.js:17:14)
    at runCallback (timers.js:705:18)
    at tryOnImmediate (timers.js:676:5)
    at processImmediate (timers.js:658:5)
    at process.topLevelDomainCallback (domain.js:126:23)
```




Issue discussion:
I have similar issue, local invocation was working fine in 1.48.2 but breaks in 1.48.3 because of profile:
We install serverless globally
```
$ yarn global add serverless
yarn global v1.17.3
[1/4] Resolving packages...
warning serverless > json-refs > path-loader > superagent@3.8.3: Please note that v5.0.1+ of superagent removes User-Agent header by default, therefore you may need to add it yourself (e.g. GitHub blocks requests without a User-Agent header).  This notice will go away with v5.0.2+ once it is released.
[2/4] Fetching packages...
[3/4] Linking dependencies...
[4/4] Building fresh packages...
success Installed "serverless@1.48.3" with binaries:
      - serverless
      - slss
      - sls
```
And then call 
```
ENVIRONMENT=test serverless invoke local -f ingestion -d '{"Records":[{"eventSource":"health"}]}' --environment=environments/test.yml

  Error --------------------------------------------------
 
  Profile ci-user does not exist
 
     For debugging logs, run again after setting the "SLS_DEBUG=*" environment variable.
 
  Get Support --------------------------------------------
     Docs:          docs.serverless.com
     Bugs:          github.com/serverless/serverless/issues
     Issues:        forum.serverless.com
 
  Your Environment Information ---------------------------
     Operating System:          linux
     Node Version:              8.10.0
     Serverless Version:        1.48.3
     Enterprise Plugin Version: 1.3.2
     Platform SDK Version:      2.1.0
 
Script failed with status 1
```

environments/test.yml: 
```
stage: test
profile: ci-user
```
The same for me. Works only on `1.48.2`.
@pmuens Could you please change label from `question` to `bug`? Looks like it's a breaking change or bug to be honest

Solution patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index bea7daeeed6..36968bb06da 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,3 +1,13 @@
+# 1.48.4 (2019-07-25)
+
+- [Add note for supported version of existing bucket feature](https://github.com/serverless/serverless/pull/6435)
+- [Support in interactive flow for SFE provided AWS creds](https://github.com/serverless/serverless/pull/6440)
+- [Fix sls package regression caused by cred fail fast](https://github.com/serverless/serverless/pull/6447)
+
+## Meta
+
+- [Comparison since last release](https://github.com/serverless/serverless/compare/v1.48.3...v1.48.4)
+
 # 1.48.3 (2019-07-23)
 
 - [Issue 6364 request path](https://github.com/serverless/serverless/pull/6422)
diff --git a/lib/plugins/aws/lib/validate.js b/lib/plugins/aws/lib/validate.js
index a4be8a5f069..c279e339855 100644
--- a/lib/plugins/aws/lib/validate.js
+++ b/lib/plugins/aws/lib/validate.js
@@ -22,7 +22,11 @@ module.exports = {
       Object.keys(creds).length === 0 &&
       !process.env.ECS_CONTAINER_METADATA_FILE &&
       !process.env.AWS_CONTAINER_CREDENTIALS_RELATIVE_URI &&
-      !process.env.AWS_CONTAINER_CREDENTIALS_FULL_URI
+      !process.env.AWS_CONTAINER_CREDENTIALS_FULL_URI &&
+      ['logs', 'info', 'deploy', 'remove', 'rollback', 'metrics', 'invoke'].includes(
+        this.serverless.processedInput.commands[0]
+      ) &&
+      this.serverless.processedInput.commands[1] !== 'local'
     ) {
       // first check if the EC2 Metadata Service has creds before throwing error
       const metadataService = new this.provider.sdk.MetadataService({
diff --git a/package.json b/package.json
index e901c6663df..49e7efc94d3 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
 {
   "name": "serverless",
-  "version": "1.48.3",
+  "version": "1.48.4",
   "engines": {
     "node": ">=6.0"
   },


Test patch:
diff --git a/lib/plugins/aws/lib/validate.test.js b/lib/plugins/aws/lib/validate.test.js
index 64dee4c92db..0a0a5e2aeea 100644
--- a/lib/plugins/aws/lib/validate.test.js
+++ b/lib/plugins/aws/lib/validate.test.js
@@ -33,6 +33,7 @@ describe('#validate', () => {
     awsPlugin.serverless.setProvider('aws', provider);
 
     awsPlugin.serverless.config.servicePath = true;
+    serverless.processedInput = { commands: ['deploy'] };
 
     Object.assign(awsPlugin, validate);
   });
@@ -95,5 +96,18 @@ describe('#validate', () => {
         expect(awsPlugin.options.region).to.equal('some-region');
       });
     });
+
+    it('should not check the metadata service if not using a command that needs creds', () => {
+      awsPlugin.options.region = false;
+      awsPlugin.serverless.service.provider = {
+        region: 'some-region',
+      };
+      provider.cachedCredentials = {};
+      serverless.processedInput = { commands: ['print'] };
+
+      return expect(awsPlugin.validate()).to.be.fulfilled.then(() => {
+        expect(awsPlugin.options.region).to.equal('some-region');
+      });
+    });
   });
 });