Repository: serverless/serverless (JavaScript)
Date: 2018-11-22 16:32:51+00:00
Task type: Bug Fix

Issue:
CLI option aws-profile overriden by profile defined in serverless.yml
# This is a Bug Report

## Description

When a profile is defined in serverless, the `--aws-profile` option doesn't override it.
Let says I've that config:

```yml
provider:
    name: aws
    runtime: nodejs8.10
    profile: default
    region: ${opt:region, 'eu-west-1'}
```

Here is my `~/.aws/credentials`:

```ini
[dev]
aws_access_key_id = xx
aws_secret_access_key = xx
region = eu-west-1
```

When I want to deploy with the **dev** profile using `serverless deploy --aws-profile dev` I got that error:

```
  Error --------------------------------------------------
 
  Profile default does not exist
```
It's because when the profile is defined in serverless.yml it tries to load it before going to check for the option `aws-profile`.

It was originally define in the good way on the first implementation (#3701), ie: the `--aws-profile` option comes before the defined profile.
But that behavior was changed in #3979.

When the profile is defined AND the `--aws-profile` is not defined, we shouldn't load the profile from `serverless.yml` otherwise it should be loaded from the option `aws-profile`.

## Additional Data

* ***Serverless Framework Version you're using***: 1.33.2
* ***Operating System***: Ubuntu
* ***Stack Trace***: n/a
* ***Provider Error messages***: n/a



Issue discussion:

Solution patch:
diff --git a/lib/plugins/aws/provider/awsProvider.js b/lib/plugins/aws/provider/awsProvider.js
index 536c8541206..9f1a919c1cb 100644
--- a/lib/plugins/aws/provider/awsProvider.js
+++ b/lib/plugins/aws/provider/awsProvider.js
@@ -315,7 +315,7 @@ class AwsProvider {
 
     // add specified credentials, overriding with more specific declarations
     impl.addCredentials(result, this.serverless.service.provider.credentials); // config creds
-    if (this.serverless.service.provider.profile) {
+    if (this.serverless.service.provider.profile && !this.options['aws-profile']) {
       // config profile
       impl.addProfileCredentials(result, this.serverless.service.provider.profile);
     }


Test patch:
diff --git a/lib/plugins/aws/provider/awsProvider.test.js b/lib/plugins/aws/provider/awsProvider.test.js
index cc31e3be66c..988b0a06a2c 100644
--- a/lib/plugins/aws/provider/awsProvider.test.js
+++ b/lib/plugins/aws/provider/awsProvider.test.js
@@ -743,6 +743,25 @@ describe('AwsProvider', () => {
       expect(credentials.credentials.profile).to.equal('notDefault');
     });
 
+    it('should get credentials when profile is provied via --aws-profile option even if profile is defined in serverless.yml', () => { // eslint-disable-line max-len
+      process.env.AWS_PROFILE = 'notDefaultTemporary';
+      newAwsProvider.options['aws-profile'] = 'notDefault';
+
+      serverless.service.provider.profile = 'notDefaultTemporary2';
+
+      const credentials = newAwsProvider.getCredentials();
+      expect(credentials.credentials.profile).to.equal('notDefault');
+    });
+
+    it('should get credentials when profile is provied via process.env.AWS_PROFILE even if profile is defined in serverless.yml', () => { // eslint-disable-line max-len
+      process.env.AWS_PROFILE = 'notDefault';
+
+      serverless.service.provider.profile = 'notDefaultTemporary';
+
+      const credentials = newAwsProvider.getCredentials();
+      expect(credentials.credentials.profile).to.equal('notDefault');
+    });
+
     it('should set the signatureVersion to v4 if the serverSideEncryption is aws:kms', () => {
       newAwsProvider.serverless.service.provider.deploymentBucketObject = {
         serverSideEncryption: 'aws:kms',