From 89668fb6de2b0c97e3b9abe51ddc2734fbc5407e Mon Sep 17 00:00:00 2001 From: Allegra Chen Date: Tue, 20 Oct 2020 17:22:11 -0400 Subject: [PATCH 1/3] Add the stage property and stage checking for each cloudfront distribution --- index.js | 62 ++++++++++++++++++++++++++++++-------------------------- 1 file changed, 33 insertions(+), 29 deletions(-) diff --git a/index.js b/index.js index c4c831d..0255c20 100644 --- a/index.js +++ b/index.js @@ -26,36 +26,39 @@ class ServerlessLambdaEdgePreExistingCloudFront { for (let idx = 0; idx < events.length; idx += 1) { const event = events[idx] - const functionArn = await this.getlatestVersionLambdaArn(functionObj.name) - const config = await this.provider.request('CloudFront', 'getDistribution', { - Id: event.preExistingCloudFront.distributionId - }) - if (event.preExistingCloudFront.pathPattern === '*') { - config.DistributionConfig.DefaultCacheBehavior.LambdaFunctionAssociations = await this.associateFunction( - config.DistributionConfig.DefaultCacheBehavior.LambdaFunctionAssociations, - event, - functionObj.name, - functionArn - ) - } else { - config.DistributionConfig.CacheBehaviors = await this.associateNonDefaultCacheBehaviors( - config.DistributionConfig.CacheBehaviors, - event, - functionObj.name, - functionArn + if (event.preExistingCloudFront.stage == `${serverless.service.provider.stage}`) { + const functionArn = await this.getlatestVersionLambdaArn(functionObj.name) + const config = await this.provider.request('CloudFront', 'getDistribution', { + Id: event.preExistingCloudFront.distributionId + }) + + if (event.preExistingCloudFront.pathPattern === '*') { + config.DistributionConfig.DefaultCacheBehavior.LambdaFunctionAssociations = await this.associateFunction( + config.DistributionConfig.DefaultCacheBehavior.LambdaFunctionAssociations, + event, + functionObj.name, + functionArn + ) + } else { + config.DistributionConfig.CacheBehaviors = await this.associateNonDefaultCacheBehaviors( + config.DistributionConfig.CacheBehaviors, + event, + functionObj.name, + functionArn + ) + } + + this.serverless.cli.consoleLog( + `${functionArn} is associating to ${event.preExistingCloudFront.distributionId} CloudFront Distribution. waiting for deployed status.` ) + + await this.provider.request('CloudFront', 'updateDistribution', { + Id: event.preExistingCloudFront.distributionId, + IfMatch: config.ETag, + DistributionConfig: config.DistributionConfig + }) } - - this.serverless.cli.consoleLog( - `${functionArn} is associating to ${event.preExistingCloudFront.distributionId} CloudFront Distribution. waiting for deployed status.` - ) - - await this.provider.request('CloudFront', 'updateDistribution', { - Id: event.preExistingCloudFront.distributionId, - IfMatch: config.ETag, - DistributionConfig: config.DistributionConfig - }) } }) }, Promise.resolve()) @@ -85,9 +88,10 @@ class ServerlessLambdaEdgePreExistingCloudFront { distributionId: { type: 'string' }, eventType: { type: 'string' }, pathPattern: { type: 'string' }, - includeBody: { type: 'boolean' } + includeBody: { type: 'boolean' }, + stage: { type: 'string' } }, - required: ['distributionId', 'eventType', 'pathPattern', 'includeBody'] + required: ['distributionId', 'eventType', 'pathPattern', 'includeBody', 'stage'] }) } } From ae9f6f332eaedd285666dcdc2be60133704305fc Mon Sep 17 00:00:00 2001 From: Allegra Chen Date: Wed, 21 Oct 2020 12:06:28 -0400 Subject: [PATCH 2/3] Make the stage property optional and update the logic --- index.js | 65 ++++++++++++++++++++++++++++---------------------------- 1 file changed, 33 insertions(+), 32 deletions(-) diff --git a/index.js b/index.js index 0255c20..c2e8e6b 100644 --- a/index.js +++ b/index.js @@ -27,38 +27,39 @@ class ServerlessLambdaEdgePreExistingCloudFront { for (let idx = 0; idx < events.length; idx += 1) { const event = events[idx] - if (event.preExistingCloudFront.stage == `${serverless.service.provider.stage}`) { - const functionArn = await this.getlatestVersionLambdaArn(functionObj.name) - const config = await this.provider.request('CloudFront', 'getDistribution', { - Id: event.preExistingCloudFront.distributionId - }) - - if (event.preExistingCloudFront.pathPattern === '*') { - config.DistributionConfig.DefaultCacheBehavior.LambdaFunctionAssociations = await this.associateFunction( - config.DistributionConfig.DefaultCacheBehavior.LambdaFunctionAssociations, - event, - functionObj.name, - functionArn - ) - } else { - config.DistributionConfig.CacheBehaviors = await this.associateNonDefaultCacheBehaviors( - config.DistributionConfig.CacheBehaviors, - event, - functionObj.name, - functionArn - ) - } - - this.serverless.cli.consoleLog( - `${functionArn} is associating to ${event.preExistingCloudFront.distributionId} CloudFront Distribution. waiting for deployed status.` + if (event.preExistingCloudFront.stage !== undefined && + event.preExistingCloudFront.stage != `${serverless.service.provider.stage}`) { continue } + + const functionArn = await this.getlatestVersionLambdaArn(functionObj.name) + const config = await this.provider.request('CloudFront', 'getDistribution', { + Id: event.preExistingCloudFront.distributionId + }) + + if (event.preExistingCloudFront.pathPattern === '*') { + config.DistributionConfig.DefaultCacheBehavior.LambdaFunctionAssociations = await this.associateFunction( + config.DistributionConfig.DefaultCacheBehavior.LambdaFunctionAssociations, + event, + functionObj.name, + functionArn + ) + } else { + config.DistributionConfig.CacheBehaviors = await this.associateNonDefaultCacheBehaviors( + config.DistributionConfig.CacheBehaviors, + event, + functionObj.name, + functionArn ) - - await this.provider.request('CloudFront', 'updateDistribution', { - Id: event.preExistingCloudFront.distributionId, - IfMatch: config.ETag, - DistributionConfig: config.DistributionConfig - }) } + + this.serverless.cli.consoleLog( + `${functionArn} is associating to ${event.preExistingCloudFront.distributionId} CloudFront Distribution. waiting for deployed status.` + ) + + await this.provider.request('CloudFront', 'updateDistribution', { + Id: event.preExistingCloudFront.distributionId, + IfMatch: config.ETag, + DistributionConfig: config.DistributionConfig + }) } }) }, Promise.resolve()) @@ -88,10 +89,10 @@ class ServerlessLambdaEdgePreExistingCloudFront { distributionId: { type: 'string' }, eventType: { type: 'string' }, pathPattern: { type: 'string' }, - includeBody: { type: 'boolean' }, + includeBody: { type: 'boolean' }, stage: { type: 'string' } }, - required: ['distributionId', 'eventType', 'pathPattern', 'includeBody', 'stage'] + required: ['distributionId', 'eventType', 'pathPattern', 'includeBody'] }) } } From 0934aee24c72d7a8a38369a8d3793affa9727c36 Mon Sep 17 00:00:00 2001 From: Allegra Chen Date: Wed, 21 Oct 2020 14:20:38 -0400 Subject: [PATCH 3/3] Update README.md --- README.md | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 62a1237..b7c27bb 100644 --- a/README.md +++ b/README.md @@ -19,10 +19,13 @@ functions: handler: lambdaEdge/viewerRequest.handler events: - preExistingCloudFront: + # ---- Mandatory Properties ----- distributionId: xxxxxxx # CloudFront distribution ID you want to associate eventType: viewer-request # Choose event to trigger your Lambda function, which are `viewer-request`, `origin-request`, `origin-response` or `viewer-response` pathPattern: '*' # Specifying the CloudFront behavior includeBody: false # Whether including body or not within request + # ---- Optional Property ----- + stage: dev # Specify the stage at which you want this CloudFront distribution to be updated plugins: - serverless-lambda-edge-pre-existing-cloudfront @@ -41,4 +44,11 @@ lambdaEdgePreExistingCloudFront: validStages: - staging - production -``` \ No newline at end of file +``` + +### How `validStages` and `stage` properties work +This plugin will first check for `validStages` property defined in the `custom` section. If `validStages` is used, then all the `preExistingCloudFront` events are only possible to be updated at the `validStages`. If not used, all the `preExistingCloudFront` events are possible to be updated at any stage. + +Then at all valid stages, the plugin checks - for each `preExistingCloudFront` event - if the provider's stage is the same as the `stage` property defined for each `preExistingCloudFront` event. If they match, then that particular `preExistingCloudFront` event will be updated. + +If `stage` is not used for a `preExistingCloudFront` event, then that event will be updated at all `validStages` or all stages if `validStages` is not used. \ No newline at end of file