Skip to content

Commit

Permalink
Merge pull request #5662 from jetbridge/master
Browse files Browse the repository at this point in the history
Provide AWS_PROFILE from configuration for invoke local
  • Loading branch information
pmuens committed Jan 22, 2019
2 parents 7bc381e + da83822 commit 89d037c
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 1 deletion.
6 changes: 6 additions & 0 deletions lib/plugins/aws/invokeLocal/index.js
Expand Up @@ -111,6 +111,12 @@ class AwsInvokeLocal {
NODE_PATH: '/var/runtime:/var/task:/var/runtime/node_modules',
};

// profile override from config
const profileOverride = this.provider.getProfile();
if (profileOverride) {
lambdaDefaultEnvVars.AWS_PROFILE = profileOverride;
}

const providerEnvVars = this.serverless.service.provider.environment || {};
const functionEnvVars = this.options.functionObj.environment || {};

Expand Down
12 changes: 12 additions & 0 deletions lib/plugins/aws/invokeLocal/index.test.js
Expand Up @@ -24,6 +24,7 @@ describe('AwsInvokeLocal', () => {
let serverless;
let provider;
let awsInvokeLocal;

beforeEach(() => {
options = {
stage: 'dev',
Expand Down Expand Up @@ -266,12 +267,23 @@ describe('AwsInvokeLocal', () => {
};
});

afterEach(() => {
delete process.env.AWS_PROFILE;
});

it('it should load provider env vars', () => awsInvokeLocal
.loadEnvVars().then(() => {
expect(process.env.providerVar).to.be.equal('providerValue');
})
);

it('it should load provider profile env', () => {
serverless.service.provider.profile = 'jdoe';
return awsInvokeLocal.loadEnvVars().then(() => {
expect(process.env.AWS_PROFILE).to.be.equal('jdoe');
});
});

it('it should load function env vars', () => awsInvokeLocal
.loadEnvVars().then(() => {
expect(process.env.functionVar).to.be.equal('functionValue');
Expand Down
13 changes: 13 additions & 0 deletions lib/plugins/aws/provider/awsProvider.js
Expand Up @@ -408,6 +408,19 @@ class AwsProvider {
return regionSourceValue.value || defaultRegion;
}

getProfileSourceValue() {
const values = this.getValues(this, [
['options', 'profile'],
['serverless', 'config', 'profile'],
['serverless', 'service', 'provider', 'profile'],
]);
const firstVal = this.firstValue(values);
return firstVal ? firstVal.value : null;
}
getProfile() {
return this.getProfileSourceValue();
}

getServerlessDeploymentBucketName() {
if (this.serverless.service.provider.deploymentBucket) {
return BbPromise.resolve(this.serverless.service.provider.deploymentBucket);
Expand Down
41 changes: 40 additions & 1 deletion lib/plugins/aws/provider/awsProvider.test.js
Expand Up @@ -329,7 +329,6 @@ describe('AwsProvider', () => {
expect(awsProvider.getCredentials()).to.deep.eql({ region: options.region });
});
});

it('should retry if error code is 429', (done) => {
const error = {
statusCode: 429,
Expand Down Expand Up @@ -1027,6 +1026,46 @@ describe('AwsProvider', () => {
});
});

describe('#getProfile()', () => {
let newAwsProvider;

it('should prefer options over config or provider', () => {
const newOptions = {
profile: 'optionsProfile',
};
const config = {
profile: 'configProfile',
};
serverless = new Serverless(config);
serverless.service.provider.profile = 'providerProfile';
newAwsProvider = new AwsProvider(serverless, newOptions);

expect(newAwsProvider.getProfile()).to.equal(newOptions.profile);
});

it('should prefer config over provider in lieu of options', () => {
const newOptions = {};
const config = {
profile: 'configProfile',
};
serverless = new Serverless(config);
serverless.service.provider.profile = 'providerProfile';
newAwsProvider = new AwsProvider(serverless, newOptions);

expect(newAwsProvider.getProfile()).to.equal(config.profile);
});

it('should use provider in lieu of options and config', () => {
const newOptions = {};
const config = {};
serverless = new Serverless(config);
serverless.service.provider.profile = 'providerProfile';
newAwsProvider = new AwsProvider(serverless, newOptions);

expect(newAwsProvider.getProfile()).to.equal(serverless.service.provider.profile);
});
});

describe('#getServerlessDeploymentBucketName()', () => {
it('should return the name of the serverless deployment bucket', () => {
const describeStackResourcesStub = sinon
Expand Down

0 comments on commit 89d037c

Please sign in to comment.