Skip to content

Commit cea4c69

Browse files
authored
Merge pull request #15 from AllegraChen/add-deployment-based-on-stages
Add the deploy to cloudfront distribution based on stages feature
2 parents 305fd32 + 0934aee commit cea4c69

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

README.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,13 @@ functions:
1919
handler: lambdaEdge/viewerRequest.handler
2020
events:
2121
- preExistingCloudFront:
22+
# ---- Mandatory Properties -----
2223
distributionId: xxxxxxx # CloudFront distribution ID you want to associate
2324
eventType: viewer-request # Choose event to trigger your Lambda function, which are `viewer-request`, `origin-request`, `origin-response` or `viewer-response`
2425
pathPattern: '*' # Specifying the CloudFront behavior
2526
includeBody: false # Whether including body or not within request
27+
# ---- Optional Property -----
28+
stage: dev # Specify the stage at which you want this CloudFront distribution to be updated
2629

2730
plugins:
2831
- serverless-lambda-edge-pre-existing-cloudfront
@@ -41,4 +44,11 @@ lambdaEdgePreExistingCloudFront:
4144
validStages:
4245
- staging
4346
- production
44-
```
47+
```
48+
49+
### How `validStages` and `stage` properties work
50+
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.
51+
52+
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.
53+
54+
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.

index.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ class ServerlessLambdaEdgePreExistingCloudFront {
2626

2727
for (let idx = 0; idx < events.length; idx += 1) {
2828
const event = events[idx]
29+
30+
if (event.preExistingCloudFront.stage !== undefined &&
31+
event.preExistingCloudFront.stage != `${serverless.service.provider.stage}`) { continue }
32+
2933
const functionArn = await this.getlatestVersionLambdaArn(functionObj.name)
3034
const config = await this.provider.request('CloudFront', 'getDistribution', {
3135
Id: event.preExistingCloudFront.distributionId
@@ -85,7 +89,8 @@ class ServerlessLambdaEdgePreExistingCloudFront {
8589
distributionId: { type: 'string' },
8690
eventType: { type: 'string' },
8791
pathPattern: { type: 'string' },
88-
includeBody: { type: 'boolean' }
92+
includeBody: { type: 'boolean' },
93+
stage: { type: 'string' }
8994
},
9095
required: ['distributionId', 'eventType', 'pathPattern', 'includeBody']
9196
})

0 commit comments

Comments
 (0)