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 diff --git a/index.js b/index.js index c4c831d..c2e8e6b 100644 --- a/index.js +++ b/index.js @@ -26,6 +26,10 @@ class ServerlessLambdaEdgePreExistingCloudFront { for (let idx = 0; idx < events.length; idx += 1) { const event = events[idx] + + 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 @@ -85,7 +89,8 @@ 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'] })