Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide AWS_PROFILE from configuration for invoke local #5662

Merged
merged 3 commits into from
Jan 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions lib/plugins/aws/invokeLocal/index.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -111,6 +111,12 @@ class AwsInvokeLocal {
NODE_PATH: '/var/runtime:/var/task:/var/runtime/node_modules', 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 providerEnvVars = this.serverless.service.provider.environment || {};
const functionEnvVars = this.options.functionObj.environment || {}; const functionEnvVars = this.options.functionObj.environment || {};


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

beforeEach(() => { beforeEach(() => {
options = { options = {
stage: 'dev', 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 it('it should load provider env vars', () => awsInvokeLocal
.loadEnvVars().then(() => { .loadEnvVars().then(() => {
expect(process.env.providerVar).to.be.equal('providerValue'); 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 it('it should load function env vars', () => awsInvokeLocal
.loadEnvVars().then(() => { .loadEnvVars().then(() => {
expect(process.env.functionVar).to.be.equal('functionValue'); expect(process.env.functionVar).to.be.equal('functionValue');
Expand Down
13 changes: 13 additions & 0 deletions lib/plugins/aws/provider/awsProvider.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -419,6 +419,19 @@ class AwsProvider {
return regionSourceValue.value || defaultRegion; 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() { getServerlessDeploymentBucketName() {
if (this.serverless.service.provider.deploymentBucket) { if (this.serverless.service.provider.deploymentBucket) {
return BbPromise.resolve(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
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -337,7 +337,6 @@ describe('AwsProvider', () => {
expect(awsProvider.getCredentials()).to.deep.eql({ region: options.region }); expect(awsProvider.getCredentials()).to.deep.eql({ region: options.region });
}); });
}); });

it('should retry if error code is 429', (done) => { it('should retry if error code is 429', (done) => {
const error = { const error = {
statusCode: 429, statusCode: 429,
Expand Down Expand Up @@ -991,6 +990,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()', () => { describe('#getServerlessDeploymentBucketName()', () => {
it('should return the name of the serverless deployment bucket', () => { it('should return the name of the serverless deployment bucket', () => {
const describeStackResourcesStub = sinon const describeStackResourcesStub = sinon
Expand Down