Skip to content

Commit 4f2eb14

Browse files
committed
Handle IncludeBody for lambda triggers correecntly
1 parent 6076ed9 commit 4f2eb14

File tree

4 files changed

+68
-13
lines changed

4 files changed

+68
-13
lines changed

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,9 @@ distribution:
108108
ttl: 10
109109
lambda@edge:
110110
viewer-request: arn:aws:lambda:us-east-1:123:function:myFunc:version # lambda ARN including version
111+
response-request: # can also send object to not include body
112+
arn: lambda-arn
113+
includeBody: false
111114
```
112115
113116
#### Private S3 Content

__tests__/__snapshots__/lambda-at-edge.test.js.snap

+3-3
Original file line numberDiff line numberDiff line change
@@ -50,17 +50,17 @@ Object {
5050
},
5151
Object {
5252
"EventType": "origin-request",
53-
"IncludeBody": true,
53+
"IncludeBody": false,
5454
"LambdaFunctionARN": "arn:aws:lambda:us-east-1:123:function:originRequestFunction",
5555
},
5656
Object {
5757
"EventType": "origin-response",
58-
"IncludeBody": true,
58+
"IncludeBody": false,
5959
"LambdaFunctionARN": "arn:aws:lambda:us-east-1:123:function:originResponseFunction",
6060
},
6161
Object {
6262
"EventType": "viewer-response",
63-
"IncludeBody": true,
63+
"IncludeBody": false,
6464
"LambdaFunctionARN": "arn:aws:lambda:us-east-1:123:function:viewerResponseFunction",
6565
},
6666
],

__tests__/lambda-at-edge.test.js

+38-5
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,14 @@ describe('Input origin as a custom url', () => {
2424
'/some/path': {
2525
ttl: 10,
2626
'lambda@edge': {
27-
'viewer-request': 'arn:aws:lambda:us-east-1:123:function:viewerRequestFunction',
28-
'origin-request': 'arn:aws:lambda:us-east-1:123:function:originRequestFunction',
27+
'viewer-request': {
28+
arn: 'arn:aws:lambda:us-east-1:123:function:viewerRequestFunction',
29+
includeBody: true
30+
},
31+
'origin-request': {
32+
arn: 'arn:aws:lambda:us-east-1:123:function:originRequestFunction',
33+
includeBody: false
34+
},
2935
'origin-response': 'arn:aws:lambda:us-east-1:123:function:originResponseFunction',
3036
'viewer-response': 'arn:aws:lambda:us-east-1:123:function:viewerResponseFunction'
3137
}
@@ -48,17 +54,17 @@ describe('Input origin as a custom url', () => {
4854
{
4955
EventType: 'origin-request',
5056
LambdaFunctionARN: 'arn:aws:lambda:us-east-1:123:function:originRequestFunction',
51-
IncludeBody: true
57+
IncludeBody: false
5258
},
5359
{
5460
EventType: 'origin-response',
5561
LambdaFunctionARN: 'arn:aws:lambda:us-east-1:123:function:originResponseFunction',
56-
IncludeBody: true
62+
IncludeBody: false
5763
},
5864
{
5965
EventType: 'viewer-response',
6066
LambdaFunctionARN: 'arn:aws:lambda:us-east-1:123:function:viewerResponseFunction',
61-
IncludeBody: true
67+
IncludeBody: false
6268
}
6369
]
6470
}
@@ -92,4 +98,31 @@ describe('Input origin as a custom url', () => {
9298
)
9399
}
94100
})
101+
102+
it('throws error when includeBody given for event types other than request', async () => {
103+
expect.assertions(1)
104+
105+
try {
106+
await component.default({
107+
origins: [
108+
{
109+
url: 'https://exampleorigin.com',
110+
pathPatterns: {
111+
'/some/path': {
112+
ttl: 10,
113+
'lambda@edge': {
114+
'viewer-response': {
115+
arn: 'arn:aws:lambda:us-east-1:123:function:viewerRequestFunction',
116+
includeBody: true
117+
}
118+
}
119+
}
120+
}
121+
}
122+
]
123+
})
124+
} catch (err) {
125+
expect(err.message).toEqual('"includeBody" not allowed for viewer-response lambda triggers.')
126+
}
127+
})
95128
})

lib/addLambdaAtEdgeToCacheBehavior.js

+24-5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,27 @@ const validLambdaTriggers = [
44
'origin-response',
55
'viewer-response'
66
]
7+
const triggersAllowedBody = ['viewer-request', 'origin-request']
8+
9+
const makeCacheItem = (eventType, lambdaConfig) => {
10+
let arn, includeBody
11+
if (typeof lambdaConfig === 'string') {
12+
arn = lambdaConfig
13+
includeBody = triggersAllowedBody.includes(eventType)
14+
} else {
15+
;({ arn, includeBody } = lambdaConfig)
16+
if (includeBody && !triggersAllowedBody.includes(eventType)) {
17+
throw new Error(
18+
`"includeBody" not allowed for ${eventType} lambda triggers.`
19+
)
20+
}
21+
}
22+
return {
23+
EventType: eventType,
24+
LambdaFunctionARN: arn,
25+
IncludeBody: includeBody
26+
}
27+
}
728

829
// adds lambda@edge to cache behavior passed
930
module.exports = (cacheBehavior, lambdaAtEdgeConfig = {}) => {
@@ -14,12 +35,10 @@ module.exports = (cacheBehavior, lambdaAtEdgeConfig = {}) => {
1435
)
1536
}
1637

38+
cacheBehavior.LambdaFunctionAssociations.Items.push(
39+
makeCacheItem(eventType, lambdaAtEdgeConfig[eventType])
40+
)
1741
cacheBehavior.LambdaFunctionAssociations.Quantity =
1842
cacheBehavior.LambdaFunctionAssociations.Quantity + 1
19-
cacheBehavior.LambdaFunctionAssociations.Items.push({
20-
EventType: eventType,
21-
LambdaFunctionARN: lambdaAtEdgeConfig[eventType],
22-
IncludeBody: true
23-
})
2443
})
2544
}

0 commit comments

Comments
 (0)