From 0963817ad50730c4f5b7476d7ef1bba2b231e037 Mon Sep 17 00:00:00 2001 From: mordka Date: Wed, 9 Dec 2020 23:10:54 +0100 Subject: [PATCH 1/5] feat: support intrinsic function as distribution id --- index.js | 30 +++++++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index bfa283e..6c3b535 100644 --- a/index.js +++ b/index.js @@ -31,16 +31,19 @@ class ServerlessLambdaEdgePreExistingCloudFront { event.preExistingCloudFront.stage != `${serverless.service.provider.stage}`) { continue } const functionArn = await this.getlatestVersionLambdaArn(functionObj.name) - + const resolvedDistributionId = await (event.preExistingCloudFront.distributionId['Fn::ImportValue'] + ? this.resolveCfImportValue(this.provider, event.preExistingCloudFront.distributionId['Fn::ImportValue']) + : event.preExistingCloudFront.distributionId + ) this.serverless.cli.consoleLog( - `${functionArn} is associating to ${event.preExistingCloudFront.distributionId} CloudFront Distribution. waiting for deployed status.` + `${functionArn} is associating to ${resolvedDistributionId} CloudFront Distribution. waiting for deployed status.` ) let retryCount = 5 const updateDistribution = async () => { const config = await this.provider.request('CloudFront', 'getDistribution', { - Id: event.preExistingCloudFront.distributionId + Id: resolvedDistributionId }) if (event.preExistingCloudFront.pathPattern === '*') { @@ -61,7 +64,7 @@ class ServerlessLambdaEdgePreExistingCloudFront { await this.provider .request('CloudFront', 'updateDistribution', { - Id: event.preExistingCloudFront.distributionId, + Id: resolvedDistributionId, IfMatch: config.ETag, DistributionConfig: config.DistributionConfig }) @@ -80,6 +83,7 @@ class ServerlessLambdaEdgePreExistingCloudFront { } await updateDistribution() + this.serverless.cli.consoleLog(`${functionArn} has been successfully associated to ${resolvedDistributionId} CloudFront Distribution.`) } }) }, Promise.resolve()) @@ -106,7 +110,9 @@ class ServerlessLambdaEdgePreExistingCloudFront { this.serverless.configSchemaHandler.defineFunctionEvent('aws', 'preExistingCloudFront', { type: 'object', properties: { - distributionId: { type: 'string' }, + distributionId: { + anyOf: [{ type: 'string' }, { type: 'object' }], + }, eventType: { type: 'string' }, pathPattern: { type: 'string' }, includeBody: { type: 'boolean' }, @@ -183,5 +189,19 @@ class ServerlessLambdaEdgePreExistingCloudFront { }) return arn } + + resolveCfImportValue(provider, name, sdkParams = {}) { + return provider.request('CloudFormation', 'listExports', sdkParams).then(result => { + const targetExportMeta = result.Exports.find(exportMeta => exportMeta.Name === name); + if (targetExportMeta) return targetExportMeta.Value; + if (result.NextToken) { + return this.resolveCfImportValue(provider, name, { NextToken: result.NextToken }); + } + + throw new Error( + `Could not resolve Fn::ImportValue with name ${name}. Are you sure this value is exported ?` + ); + }); + } } module.exports = ServerlessLambdaEdgePreExistingCloudFront From 145297d7e6046d5d2aab1a35a799549149933f06 Mon Sep 17 00:00:00 2001 From: mordka Date: Wed, 9 Dec 2020 23:26:09 +0100 Subject: [PATCH 2/5] refactor: show individual event association logs --- index.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index 6c3b535..f5b79a1 100644 --- a/index.js +++ b/index.js @@ -23,7 +23,9 @@ class ServerlessLambdaEdgePreExistingCloudFront { const events = functionObj.events.filter( (event) => event.preExistingCloudFront && this.checkAllowedDeployStage() ) - + this.serverless.cli.consoleLog( + `${functionArn} is associating to ${resolvedDistributionId} CloudFront Distribution. waiting for deployed status.` + ) for (let idx = 0; idx < events.length; idx += 1) { const event = events[idx] @@ -35,9 +37,6 @@ class ServerlessLambdaEdgePreExistingCloudFront { ? this.resolveCfImportValue(this.provider, event.preExistingCloudFront.distributionId['Fn::ImportValue']) : event.preExistingCloudFront.distributionId ) - this.serverless.cli.consoleLog( - `${functionArn} is associating to ${resolvedDistributionId} CloudFront Distribution. waiting for deployed status.` - ) let retryCount = 5 @@ -83,7 +82,8 @@ class ServerlessLambdaEdgePreExistingCloudFront { } await updateDistribution() - this.serverless.cli.consoleLog(`${functionArn} has been successfully associated to ${resolvedDistributionId} CloudFront Distribution.`) + + this.serverless.cli.consoleLog(`Event ${event.preExistingCloudFront.eventType - event.preExistingCloudFront.pathPattern} has been successfully associated to ${resolvedDistributionId} CloudFront Distribution.`) } }) }, Promise.resolve()) From 3e8f0202f30bd203356d358d4619a5191359680c Mon Sep 17 00:00:00 2001 From: mordka Date: Wed, 9 Dec 2020 23:26:28 +0100 Subject: [PATCH 3/5] 1.1.6 --- package-lock.json | 2 +- package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/package-lock.json b/package-lock.json index 31faa12..1c3b900 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "serverless-lambda-edge-pre-existing-cloudfront", - "version": "1.1.5", + "version": "1.1.6", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index b276e55..f7749cb 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "serverless-lambda-edge-pre-existing-cloudfront", - "version": "1.1.5", + "version": "1.1.6", "description": "The Serverless Framework plugin which creates Lambda@Edge against pre-existing CloudFront.", "main": "index.js", "author": "serverless-operations", From 5b7aadaa0930f61eeca7eaf43b29e7ab2b53b2fd Mon Sep 17 00:00:00 2001 From: mordka Date: Wed, 9 Dec 2020 23:41:17 +0100 Subject: [PATCH 4/5] fix: console message --- index.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index f5b79a1..3c824a0 100644 --- a/index.js +++ b/index.js @@ -23,9 +23,6 @@ class ServerlessLambdaEdgePreExistingCloudFront { const events = functionObj.events.filter( (event) => event.preExistingCloudFront && this.checkAllowedDeployStage() ) - this.serverless.cli.consoleLog( - `${functionArn} is associating to ${resolvedDistributionId} CloudFront Distribution. waiting for deployed status.` - ) for (let idx = 0; idx < events.length; idx += 1) { const event = events[idx] @@ -37,6 +34,9 @@ class ServerlessLambdaEdgePreExistingCloudFront { ? this.resolveCfImportValue(this.provider, event.preExistingCloudFront.distributionId['Fn::ImportValue']) : event.preExistingCloudFront.distributionId ) + this.serverless.cli.consoleLog( + `${functionArn} (Event: ${event.preExistingCloudFront.eventType}, pathPattern: ${event.preExistingCloudFront.pathPattern}) is associating to ${resolvedDistributionId} CloudFront Distribution. waiting for deployed status.` + ) let retryCount = 5 From 3a8fee0de7a9000affc7a3dbd77b80a22f38ed86 Mon Sep 17 00:00:00 2001 From: mordka Date: Wed, 9 Dec 2020 23:41:17 +0100 Subject: [PATCH 5/5] fix: remove message --- index.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/index.js b/index.js index 3c824a0..3dce0fc 100644 --- a/index.js +++ b/index.js @@ -82,8 +82,6 @@ class ServerlessLambdaEdgePreExistingCloudFront { } await updateDistribution() - - this.serverless.cli.consoleLog(`Event ${event.preExistingCloudFront.eventType - event.preExistingCloudFront.pathPattern} has been successfully associated to ${resolvedDistributionId} CloudFront Distribution.`) } }) }, Promise.resolve())