Skip to content

Commit 2829a26

Browse files
add configurable default cache behavior lambda@edge
1 parent e7ce424 commit 2829a26

File tree

6 files changed

+53
-29
lines changed

6 files changed

+53
-29
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ distribution:
5252
enabled: true # optional
5353
defaults: # optional
5454
ttl: 15
55+
lambda@edge: # added to cloudfront default cache behavior
56+
viewer-request: arn:aws:lambda:us-east-1:123:function:myFunc:version
5557
origins:
5658
- https://my-bucket.s3.amazonaws.com
5759
```

__tests__/__snapshots__/custom-url-origin.test.js.snap

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
exports[`Input origin as a custom url creates distribution with custom url origin 1`] = `
3+
exports[`Input origin as a custom url creates distribution with custom url origin and sets defaults 1`] = `
44
Object {
55
"DistributionConfig": Object {
66
"Aliases": Object {
@@ -25,7 +25,7 @@ Object {
2525
"Quantity": 2,
2626
},
2727
"Compress": false,
28-
"DefaultTTL": 86400,
28+
"DefaultTTL": 10,
2929
"FieldLevelEncryptionId": "",
3030
"ForwardedValues": Object {
3131
"Cookies": Object {
@@ -42,8 +42,14 @@ Object {
4242
},
4343
},
4444
"LambdaFunctionAssociations": Object {
45-
"Items": Array [],
46-
"Quantity": 0,
45+
"Items": Array [
46+
Object {
47+
"EventType": "origin-request",
48+
"IncludeBody": true,
49+
"LambdaFunctionARN": "arn:aws:lambda:us-east-1:123:function:originRequestFunction",
50+
},
51+
],
52+
"Quantity": 1,
4753
},
4854
"MaxTTL": 31536000,
4955
"MinTTL": 0,

__tests__/custom-url-origin.test.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,14 @@ describe('Input origin as a custom url', () => {
2121
component = await createComponent()
2222
})
2323

24-
it('creates distribution with custom url origin', async () => {
24+
it('creates distribution with custom url origin and sets defaults', async () => {
2525
await component.default({
26+
defaults: {
27+
ttl: 10,
28+
'lambda@edge': {
29+
'origin-request': 'arn:aws:lambda:us-east-1:123:function:originRequestFunction'
30+
}
31+
},
2632
origins: ['https://mycustomorigin.com']
2733
})
2834

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
const validLambdaTriggers = [
2+
'viewer-request',
3+
'origin-request',
4+
'origin-response',
5+
'viewer-response'
6+
]
7+
8+
// adds lambda@edge to cache behavior passed
9+
module.exports = (cacheBehavior, lambdaAtEdgeConfig = {}) => {
10+
Object.keys(lambdaAtEdgeConfig).forEach((eventType) => {
11+
if (!validLambdaTriggers.includes(eventType)) {
12+
throw new Error(
13+
`"${eventType}" is not a valid lambda trigger. See https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/lambda-cloudfront-trigger-events.html for valid event types.`
14+
)
15+
}
16+
17+
cacheBehavior.LambdaFunctionAssociations.Quantity =
18+
cacheBehavior.LambdaFunctionAssociations.Quantity + 1
19+
cacheBehavior.LambdaFunctionAssociations.Items.push({
20+
EventType: eventType,
21+
LambdaFunctionARN: lambdaAtEdgeConfig[eventType],
22+
IncludeBody: true
23+
})
24+
})
25+
}

lib/getDefaultCacheBehavior.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
const addLambdaAtEdgeToCacheBehavior = require('./addLambdaAtEdgeToCacheBehavior')
2+
13
module.exports = (originId, defaults = {}) => {
2-
return {
4+
const defaultCacheBehavior = {
35
TargetOriginId: originId,
46
ForwardedValues: {
57
QueryString: false,
@@ -40,4 +42,8 @@ module.exports = (originId, defaults = {}) => {
4042
},
4143
FieldLevelEncryptionId: ''
4244
}
45+
46+
addLambdaAtEdgeToCacheBehavior(defaultCacheBehavior, defaults['lambda@edge'])
47+
48+
return defaultCacheBehavior
4349
}

lib/parseInputOrigins.js

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
11
const getOriginConfig = require('./getOriginConfig')
22
const getCacheBehavior = require('./getCacheBehavior')
3+
const addLambdaAtEdgeToCacheBehavior = require('./addLambdaAtEdgeToCacheBehavior')
34

4-
const validLambdaTriggers = [
5-
'viewer-request',
6-
'origin-request',
7-
'origin-response',
8-
'viewer-response'
9-
]
105
module.exports = (origins) => {
116
const distributionOrigins = {
127
Quantity: 0,
@@ -26,23 +21,7 @@ module.exports = (origins) => {
2621
const pathPatternConfig = origin.pathPatterns[pathPattern]
2722
const cacheBehavior = getCacheBehavior(pathPattern, pathPatternConfig, originConfig.Id)
2823

29-
const lambdaAtEdge = pathPatternConfig['lambda@edge'] || {}
30-
31-
Object.keys(lambdaAtEdge).forEach((eventType) => {
32-
if (!validLambdaTriggers.includes(eventType)) {
33-
throw new Error(
34-
`"${eventType}" is not a valid lambda trigger. See https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/lambda-cloudfront-trigger-events.html for valid event types.`
35-
)
36-
}
37-
38-
cacheBehavior.LambdaFunctionAssociations.Quantity =
39-
cacheBehavior.LambdaFunctionAssociations.Quantity + 1
40-
cacheBehavior.LambdaFunctionAssociations.Items.push({
41-
EventType: eventType,
42-
LambdaFunctionARN: lambdaAtEdge[eventType],
43-
IncludeBody: true
44-
})
45-
})
24+
addLambdaAtEdgeToCacheBehavior(cacheBehavior, pathPatternConfig['lambda@edge'])
4625

4726
distributionCacheBehaviors = {
4827
Quantity: 0,

0 commit comments

Comments
 (0)