Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 1 addition & 20 deletions lib/plugins/aws/deploy/compile/functions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,6 @@ class AwsCompileFunctions {

const functionLogicalId = this.provider.naming
.getLambdaLogicalId(functionName);
const functionOutputLogicalId = this.provider.naming
.getLambdaOutputLogicalId(functionName);
const newFunctionObject = {
[functionLogicalId]: newFunction,
};
Expand Down Expand Up @@ -191,17 +189,7 @@ class AwsCompileFunctions {
newVersionObject);
}

// Add function to Outputs section
const newOutput = this.cfOutputDescriptionTemplate();
newOutput.Value = { 'Fn::GetAtt': [functionLogicalId, 'Arn'] };

const newOutputObject = {
[functionOutputLogicalId]: newOutput,
};

_.merge(this.serverless.service.provider.compiledCloudFormationTemplate.Outputs,
newOutputObject);

// Add function versions to Outputs section
const functionVersionOutputLogicalId = this.provider.naming

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am assuming this is only added if users opt-in function versioning. Do you think there's a way to compute function version arns on the fly as well?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that's right. The conditional for adding it to the template (or not) is here

.getLambdaVersionOutputLogicalId(functionName);
const newVersionOutput = this.cfOutputLatestVersionTemplate();
Expand Down Expand Up @@ -256,13 +244,6 @@ class AwsCompileFunctions {
};
}

cfOutputDescriptionTemplate() {
return {
Description: 'Lambda function info',
Value: 'Value',
};
}

cfOutputLatestVersionTemplate() {
return {
Description: 'Current Lambda function version',
Expand Down
21 changes: 2 additions & 19 deletions lib/plugins/aws/deploy/compile/functions/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -887,18 +887,10 @@ describe('AwsCompileFunctions', () => {
};

const expectedOutputs = {
FuncLambdaFunctionArn: {
Description: 'Lambda function info',
Value: { 'Fn::GetAtt': ['FuncLambdaFunction', 'Arn'] },
},
FuncLambdaFunctionQualifiedArn: {
Description: 'Current Lambda function version',
Value: { Ref: 'FuncLambdaVersionw6uP8Tcg6K2QR905Rms8iXTlksL6OD1KOWBxTK7wxPI' },
},
AnotherFuncLambdaFunctionArn: {
Description: 'Lambda function info',
Value: { 'Fn::GetAtt': ['AnotherFuncLambdaFunction', 'Arn'] },
},
AnotherFuncLambdaFunctionQualifiedArn: {
Description: 'Current Lambda function version',
Value: {
Expand All @@ -917,7 +909,7 @@ describe('AwsCompileFunctions', () => {
);
});

it('should not create function output objects when `versionFunctions` is false', () => {
it('should not create function output objects when "versionFunctions" is false', () => {
awsCompileFunctions.serverless.service.provider.versionFunctions = false;
awsCompileFunctions.serverless.service.functions = {
func: {
Expand All @@ -928,16 +920,7 @@ describe('AwsCompileFunctions', () => {
},
};

const expectedOutputs = {
FuncLambdaFunctionArn: {
Description: 'Lambda function info',
Value: { 'Fn::GetAtt': ['FuncLambdaFunction', 'Arn'] },
},
AnotherFuncLambdaFunctionArn: {
Description: 'Lambda function info',
Value: { 'Fn::GetAtt': ['AnotherFuncLambdaFunction', 'Arn'] },
},
};
const expectedOutputs = {};

awsCompileFunctions.compileFunctions();

Expand Down
28 changes: 15 additions & 13 deletions lib/plugins/aws/info/getStackInfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,32 +30,34 @@ module.exports = {
if (result) {
outputs = result.Stacks[0].Outputs;

const lambdaArnOutputRegex = this.provider.naming
.getLambdaOutputLogicalIdRegex();

const serviceEndpointOutputRegex = this.provider.naming
.getServiceEndpointRegex();

// Outputs
this.gatheredData.outputs = outputs;

// Functions
this.gatheredData.info.functions = [];
outputs.filter(x => x.OutputKey.match(lambdaArnOutputRegex))
.forEach(x => {
const functionInfo = {};
functionInfo.arn = x.OutputValue;
functionInfo.name = functionInfo.arn.substring(x.OutputValue.lastIndexOf(':') + 1);
this.gatheredData.info.functions.push(functionInfo);
});

// Endpoints
outputs.filter(x => x.OutputKey.match(serviceEndpointOutputRegex))
.forEach(x => {
this.gatheredData.info.endpoint = x.OutputValue;
});
}

return BbPromise.resolve();
})
.then(() => this.provider.getAccountId())
.then((accountId) => {
this.gatheredData.info.functions = [];

this.serverless.service.getAllFunctions().forEach((func) => {
const functionInfo = {};
const name = `${this.serverless.service.service}-${this.options.stage}-${func}`;
const arn = `arn:aws:lambda:${this.options.region}:${accountId}:function:${name}`;
functionInfo.name = name;
functionInfo.arn = arn;
this.gatheredData.info.functions.push(functionInfo);
});

return BbPromise.resolve();
});
},
Expand Down
68 changes: 35 additions & 33 deletions lib/plugins/aws/info/getStackInfo.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,31 @@ const BbPromise = require('bluebird');
describe('#getStackInfo()', () => {
let serverless;
let awsInfo;
let describeStacksStub;
let getAccountIdStub;

beforeEach(() => {
serverless = new Serverless();
serverless.setProvider('aws', new AwsProvider(serverless));
serverless.service.service = 'my-service';
serverless.service.functions = {
hello: {},
world: {},
};
const options = {
stage: 'dev',
region: 'us-east-1',
};
awsInfo = new AwsInfo(serverless, options);

describeStacksStub = sinon.stub(awsInfo.provider, 'request');
getAccountIdStub = sinon.stub(awsInfo.provider, 'getAccountId')
.returns(BbPromise.resolve(12345678));
});

afterEach(() => {
awsInfo.provider.request.restore();
awsInfo.provider.getAccountId.restore();
});

it('attach info from describeStack call to this.gatheredData if result is available', () => {
Expand All @@ -36,16 +47,6 @@ describe('#getStackInfo()', () => {
'Sample template showing how to create a publicly accessible S3 bucket.',
Tags: [],
Outputs: [
{
Description: 'Lambda function info',
OutputKey: 'HelloLambdaFunctionArn',
OutputValue: 'arn:aws:iam::12345678:function:hello',
},
{
Description: 'Lambda function info',
OutputKey: 'WorldLambdaFunctionArn',
OutputValue: 'arn:aws:iam::12345678:function:world',
},
{
Description: 'URL of the service endpoint',
OutputKey: 'ServiceEndpoint',
Expand All @@ -72,19 +73,18 @@ describe('#getStackInfo()', () => {
],
};

const describeStackStub = sinon.stub(awsInfo.provider, 'request')
.returns(BbPromise.resolve(describeStacksResponse));
describeStacksStub.returns(BbPromise.resolve(describeStacksResponse));

const expectedGatheredDataObj = {
info: {
functions: [
{
arn: 'arn:aws:iam::12345678:function:hello',
name: 'hello',
arn: 'arn:aws:lambda:us-east-1:12345678:function:my-service-dev-hello',
name: 'my-service-dev-hello',
},
{
arn: 'arn:aws:iam::12345678:function:world',
name: 'world',
arn: 'arn:aws:lambda:us-east-1:12345678:function:my-service-dev-world',
name: 'my-service-dev-world',
},
],
endpoint: 'ab12cd34ef.execute-api.us-east-1.amazonaws.com/dev',
Expand All @@ -93,16 +93,6 @@ describe('#getStackInfo()', () => {
region: 'us-east-1',
},
outputs: [
{
Description: 'Lambda function info',
OutputKey: 'HelloLambdaFunctionArn',
OutputValue: 'arn:aws:iam::12345678:function:hello',
},
{
Description: 'Lambda function info',
OutputKey: 'WorldLambdaFunctionArn',
OutputValue: 'arn:aws:iam::12345678:function:world',
},
{
Description: 'URL of the service endpoint',
OutputKey: 'ServiceEndpoint',
Expand All @@ -122,8 +112,8 @@ describe('#getStackInfo()', () => {
};

return awsInfo.getStackInfo().then(() => {
expect(describeStackStub.calledOnce).to.equal(true);
expect(describeStackStub.calledWithExactly(
expect(describeStacksStub.calledOnce).to.equal(true);
expect(describeStacksStub.calledWithExactly(
'CloudFormation',
'describeStacks',
{
Expand All @@ -133,19 +123,29 @@ describe('#getStackInfo()', () => {
awsInfo.options.region
)).to.equal(true);

expect(getAccountIdStub.calledOnce).to.equal(true);

expect(awsInfo.gatheredData).to.deep.equal(expectedGatheredDataObj);
});
});

it('should resolve if result is empty', () => {
const describeStacksResponse = null;

const describeStackStub = sinon.stub(awsInfo.provider, 'request')
.returns(BbPromise.resolve(describeStacksResponse));
describeStacksStub.returns(BbPromise.resolve(describeStacksResponse));

const expectedGatheredDataObj = {
info: {
functions: [],
functions: [
{
arn: 'arn:aws:lambda:us-east-1:12345678:function:my-service-dev-hello',
name: 'my-service-dev-hello',
},
{
arn: 'arn:aws:lambda:us-east-1:12345678:function:my-service-dev-world',
name: 'my-service-dev-world',
},
],
endpoint: '',
service: 'my-service',
stage: 'dev',
Expand All @@ -155,8 +155,8 @@ describe('#getStackInfo()', () => {
};

return awsInfo.getStackInfo().then(() => {
expect(describeStackStub.calledOnce).to.equal(true);
expect(describeStackStub.calledWithExactly(
expect(describeStacksStub.calledOnce).to.equal(true);
expect(describeStacksStub.calledWithExactly(
'CloudFormation',
'describeStacks',
{
Expand All @@ -166,6 +166,8 @@ describe('#getStackInfo()', () => {
awsInfo.options.region
)).to.equal(true);

expect(getAccountIdStub.calledOnce).to.equal(true);

expect(awsInfo.gatheredData).to.deep.equal(expectedGatheredDataObj);
});
});
Expand Down
6 changes: 0 additions & 6 deletions lib/plugins/aws/lib/naming.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,6 @@ module.exports = {
getLambdaLogicalIdRegex() {
return /LambdaFunction$/;
},
getLambdaOutputLogicalId(functionName) {
return `${this.getLambdaLogicalId(functionName)}Arn`;
},
getLambdaOutputLogicalIdRegex() {
return /LambdaFunctionArn$/;
},
getLambdaVersionLogicalId(functionName, sha) {
return `${this.getNormalizedFunctionName(functionName)}LambdaVersion${sha
.replace(/[^0-9a-z]/gi, '')}`;
Expand Down
27 changes: 0 additions & 27 deletions lib/plugins/aws/lib/naming.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -218,33 +218,6 @@ describe('#naming()', () => {
});
});

describe('#getLambdaOutputLogicalId()', () => {
it('should normalize the function name and add the logical arn suffix', () => {
expect(
sdk.naming.getLambdaOutputLogicalId('functionName')
).to.equal('FunctionNameLambdaFunctionArn');
});
});

describe('#getLambdaOutputLogicalIdRegex()', () => {
it('should match the suffix', () => {
expect(sdk.naming.getLambdaOutputLogicalIdRegex()
.test('aLambdaFunctionArn')).to.equal(true);
});

it('should not match a name without the suffix', () => {
expect(sdk.naming.getLambdaOutputLogicalIdRegex()
.test('LambdaFunctionArnNotTheSuffix'))
.to.equal(false);
});

it('should match a name with the suffix', () => {
expect(sdk.naming.getLambdaOutputLogicalIdRegex()
.test('AFunctionArnNameLambdaFunctionArn'))
.to.equal(true);
});
});

describe('#getApiGatewayName()', () => {
it('should return the composition of stage and service name', () => {
serverless.service.service = 'myService';
Expand Down
8 changes: 8 additions & 0 deletions lib/plugins/aws/provider/awsProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,14 @@ class AwsProvider {
}
return returnValue;
}

getAccountId() {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!!! ❤️ ... We've needed this for so long already!

return this.request('IAM', 'getUser', {})
.then((result) => {
const arn = result.User.Arn;
return arn.split(':')[4];
});
}
}

module.exports = AwsProvider;
21 changes: 21 additions & 0 deletions lib/plugins/aws/provider/awsProvider.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -545,5 +545,26 @@ describe('AwsProvider', () => {
expect(awsProvider.getStage()).to.equal('dev');
});
});

describe('#getAccountId()', () => {
it('should return the AWS account id', () => {
const accountId = '12345678';

const getUserStub = sinon
.stub(awsProvider, 'request')
.returns(BbPromise.resolve({
User: {
Arn: `arn:aws:iam::${accountId}:user/serverless-user`,
},
}));

return awsProvider.getAccountId()
.then((result) => {
expect(getUserStub.calledOnce).to.equal(true);
expect(result).to.equal(accountId);
awsProvider.request.restore();
});
});
});
});
});