refactor(AWS Lambda): Resolve object once

This commit is contained in:
Mariusz Nowak 2019-12-20 12:58:55 +01:00
parent 388c2993f0
commit 3e2f59e27c
No known key found for this signature in database
GPG Key ID: B1FBDA8A182B03F2

View File

@ -118,6 +118,7 @@ class AwsCompileFunctions {
compileFunction(functionName) {
return BbPromise.try(() => {
const cfTemplate = this.serverless.service.provider.compiledCloudFormationTemplate;
const functionResource = this.cfLambdaFunctionTemplate();
const functionObject = this.serverless.service.getFunction(functionName);
functionObject.package = functionObject.package || {};
@ -213,8 +214,7 @@ class AwsCompileFunctions {
const splittedArn = arn.split(':');
if (splittedArn[0] === 'arn' && (splittedArn[2] === 'sns' || splittedArn[2] === 'sqs')) {
const dlqType = splittedArn[2];
const iamRoleLambdaExecution = this.serverless.service.provider
.compiledCloudFormationTemplate.Resources.IamRoleLambdaExecution;
const iamRoleLambdaExecution = cfTemplate.Resources.IamRoleLambdaExecution;
let stmt;
functionResource.Properties.DeadLetterConfig = {
@ -271,8 +271,7 @@ class AwsCompileFunctions {
if (typeof arn === 'string') {
const splittedArn = arn.split(':');
if (splittedArn[0] === 'arn' && splittedArn[2] === 'kms') {
const iamRoleLambdaExecution = this.serverless.service.provider
.compiledCloudFormationTemplate.Resources.IamRoleLambdaExecution;
const iamRoleLambdaExecution = cfTemplate.Resources.IamRoleLambdaExecution;
functionResource.Properties.KmsKeyArn = arn;
@ -318,8 +317,7 @@ class AwsCompileFunctions {
mode = 'Active';
}
const iamRoleLambdaExecution = this.serverless.service.provider
.compiledCloudFormationTemplate.Resources.IamRoleLambdaExecution;
const iamRoleLambdaExecution = cfTemplate.Resources.IamRoleLambdaExecution;
functionResource.Properties.TracingConfig = {
Mode: mode,
@ -436,10 +434,7 @@ class AwsCompileFunctions {
[functionLogicalId]: functionResource,
};
Object.assign(
this.serverless.service.provider.compiledCloudFormationTemplate.Resources,
newFunctionObject
);
Object.assign(cfTemplate.Resources, newFunctionObject);
const shouldVersionFunction =
functionObject.versionFunction != null
@ -467,9 +462,7 @@ class AwsCompileFunctions {
);
}
this.serverless.service.provider.compiledCloudFormationTemplate.Resources[
this.provider.naming.getLambdaOnlyVersionLogicalId(functionName)
] = {
cfTemplate.Resources[this.provider.naming.getLambdaOnlyVersionLogicalId(functionName)] = {
Type: 'AWS::Lambda::Version',
Properties: {
FunctionName: { Ref: functionLogicalId },
@ -539,10 +532,7 @@ class AwsCompileFunctions {
[versionLogicalId]: versionResource,
};
Object.assign(
this.serverless.service.provider.compiledCloudFormationTemplate.Resources,
newVersionObject
);
Object.assign(cfTemplate.Resources, newVersionObject);
// Add function versions to Outputs section
const functionVersionOutputLogicalId = this.provider.naming.getLambdaVersionOutputLogicalId(
@ -552,7 +542,7 @@ class AwsCompileFunctions {
newVersionOutput.Value = { Ref: versionLogicalId };
Object.assign(this.serverless.service.provider.compiledCloudFormationTemplate.Outputs, {
Object.assign(cfTemplate.Outputs, {
[functionVersionOutputLogicalId]: newVersionOutput,
});
});