Skip to content

Add the deploy to cloudfront distribution based on stages feature #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -41,4 +44,11 @@ lambdaEdgePreExistingCloudFront:
validStages:
- staging
- production
```
```

### 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.
7 changes: 6 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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']
})
Expand Down