Skip to content

Commit 2c6dd6e

Browse files
committed
add readme
1 parent 65b5356 commit 2c6dd6e

File tree

3 files changed

+75
-6
lines changed

3 files changed

+75
-6
lines changed

LICENSE.txt

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2020 Serverless Operations, inc
4+
5+
The following license applies to all parts of this software except as
6+
documented below:
7+
8+
====
9+
10+
Permission is hereby granted, free of charge, to any person obtaining a copy
11+
of this software and associated documentation files (the "Software"), to deal
12+
in the Software without restriction, including without limitation the rights
13+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14+
copies of the Software, and to permit persons to whom the Software is
15+
furnished to do so, subject to the following conditions:
16+
17+
The above copyright notice and this permission notice shall be included in all
18+
copies or substantial portions of the Software.
19+
20+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26+
SOFTWARE.
27+
28+
====
29+
30+
All files located in the node_modules and external directories are
31+
externally maintained libraries used by this software which have their
32+
own licenses; we recommend you read them, as their terms may differ from
33+
the terms above.

README.md

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Serverless Lambda Edge PreExisting CloudFront
2+
The Serverless Framework plugin which associates Lambda@Edge against pre-existing CloudFront distribution.
3+
4+
## Install
5+
6+
You can install this plugin from npm registry.
7+
8+
```shell
9+
$ npm install --save-dev serverless-lambda-edge-pre-existing-cloudfront
10+
```
11+
12+
## How it works
13+
14+
Here is configuration in your serverless.yml
15+
16+
```yaml
17+
functions:
18+
viewerRequest:
19+
handler: lambdaEdge/viewerRequest.handler
20+
events:
21+
- preExistingCloudFront:
22+
distributionId: xxxxxxx # CloudFront distribution ID you want to associate
23+
eventType: viewer-request # Choose event to trigger your Lambda function, which are `viewer-request`, `origin-request`, `origin-response` or `viewer-response`
24+
pathPattern: '*' # Specifying the CloudFront behavior
25+
includeBody: false # Whether including body or not within request
26+
27+
plugins:
28+
- serverless-lambda-edge-pre-existing-cloudfront
29+
```

index.js

+13-6
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class ServerlessLambdaEdgePreExistingCloudFront {
1616
if (functionObj.events) {
1717
functionObj.events.forEach(async (event) => {
1818
if (event.preExistingCloudFront) {
19+
const functionArn = await this.getlatestVersionLambdaArn(functionObj.name)
1920
const config = await this.provider.request('CloudFront', 'getDistribution', {
2021
Id: event.preExistingCloudFront.distributionId
2122
})
@@ -24,13 +25,15 @@ class ServerlessLambdaEdgePreExistingCloudFront {
2425
config.DistributionConfig.DefaultCacheBehavior.LambdaFunctionAssociations = await this.associateFunction(
2526
config.DistributionConfig.DefaultCacheBehavior.LambdaFunctionAssociations,
2627
event,
27-
functionObj.name
28+
functionObj.name,
29+
functionArn
2830
)
2931
} else {
3032
config.DistributionConfig.CacheBehaviors = await this.associateNonDefaultCacheBehaviors(
3133
config.DistributionConfig.CacheBehaviors,
3234
event,
33-
functionObj.name
35+
functionObj.name,
36+
functionArn
3437
)
3538
}
3639

@@ -39,6 +42,9 @@ class ServerlessLambdaEdgePreExistingCloudFront {
3942
IfMatch: config.ETag,
4043
DistributionConfig: config.DistributionConfig
4144
})
45+
this.serverless.cli.consoleLog(
46+
`${functionArn} is associating to ${event.preExistingCloudFront.distributionId} CloudFront Distribution. waiting for deployed status.`
47+
)
4248
}
4349
})
4450
}
@@ -47,26 +53,27 @@ class ServerlessLambdaEdgePreExistingCloudFront {
4753
}
4854
}
4955

50-
async associateNonDefaultCacheBehaviors(cacheBehaviors, event, functionName) {
56+
async associateNonDefaultCacheBehaviors(cacheBehaviors, event, functionName, functionArn) {
5157
for (let i = 0; i < cacheBehaviors.Items.length; i++) {
5258
if (event.preExistingCloudFront.pathPattern === cacheBehaviors.Items[i].PathPattern) {
5359
cacheBehaviors.Items[i].LambdaFunctionAssociations = await this.associateFunction(
5460
cacheBehaviors.Items[i].LambdaFunctionAssociations,
5561
event,
56-
functionName
62+
functionName,
63+
functionArn
5764
)
5865
}
5966
}
6067
return cacheBehaviors
6168
}
6269

63-
async associateFunction(lambdaFunctionAssociations, event, functionName) {
70+
async associateFunction(lambdaFunctionAssociations, event, functionName, functionArn) {
6471
const originals = lambdaFunctionAssociations.Items.filter(
6572
(x) => x.EventType !== event.preExistingCloudFront.eventType
6673
)
6774
lambdaFunctionAssociations.Items = originals
6875
lambdaFunctionAssociations.Items.push({
69-
LambdaFunctionARN: await this.getlatestVersionLambdaArn(functionName),
76+
LambdaFunctionARN: functionArn,
7077
IncludeBody: event.preExistingCloudFront.includeBody,
7178
EventType: event.preExistingCloudFront.eventType
7279
})

0 commit comments

Comments
 (0)