Skip to content

Commit 56cee85

Browse files
authored
Merge pull request #4 from danielcondemarin/add-defaults
add configurable defaults
2 parents 1d621df + c607b80 commit 56cee85

File tree

8 files changed

+70
-34
lines changed

8 files changed

+70
-34
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ distribution:
5050
inputs:
5151
region: us-east-1
5252
enabled: true # optional
53+
defaults: # optional
54+
ttl: 15
55+
lambda@edge: # added to cloudfront default cache behavior
56+
viewer-request: arn:aws:lambda:us-east-1:123:function:myFunc:version
5357
origins:
5458
- https://my-bucket.s3.amazonaws.com
5559
```

__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 {
@@ -29,7 +29,7 @@ Object {
2929
"Quantity": 2,
3030
},
3131
"Compress": false,
32-
"DefaultTTL": 86400,
32+
"DefaultTTL": 10,
3333
"FieldLevelEncryptionId": "",
3434
"ForwardedValues": Object {
3535
"Cookies": Object {
@@ -46,8 +46,14 @@ Object {
4646
},
4747
},
4848
"LambdaFunctionAssociations": Object {
49-
"Items": Array [],
50-
"Quantity": 0,
49+
"Items": Array [
50+
Object {
51+
"EventType": "origin-request",
52+
"IncludeBody": true,
53+
"LambdaFunctionARN": "arn:aws:lambda:us-east-1:123:function:originRequestFunction",
54+
},
55+
],
56+
"Quantity": 1,
5157
},
5258
"MaxTTL": 31536000,
5359
"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: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
module.exports = (originId) => {
2-
return {
1+
const addLambdaAtEdgeToCacheBehavior = require('./addLambdaAtEdgeToCacheBehavior')
2+
3+
module.exports = (originId, defaults = {}) => {
4+
const defaultCacheBehavior = {
35
TargetOriginId: originId,
46
ForwardedValues: {
57
QueryString: false,
@@ -31,7 +33,7 @@ module.exports = (originId) => {
3133
}
3234
},
3335
SmoothStreaming: false,
34-
DefaultTTL: 86400,
36+
DefaultTTL: defaults.ttl || 86400,
3537
MaxTTL: 31536000,
3638
Compress: false,
3739
LambdaFunctionAssociations: {
@@ -40,4 +42,8 @@ module.exports = (originId) => {
4042
},
4143
FieldLevelEncryptionId: ''
4244
}
45+
46+
addLambdaAtEdgeToCacheBehavior(defaultCacheBehavior, defaults['lambda@edge'])
47+
48+
return defaultCacheBehavior
4349
}

lib/index.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,10 @@ const createCloudFrontDistribution = async (cf, s3, inputs) => {
5656
distributionConfig.Origins = Origins
5757

5858
// set first origin declared as the default cache behavior
59-
distributionConfig.DefaultCacheBehavior = getDefaultCacheBehavior(Origins.Items[0].Id)
59+
distributionConfig.DefaultCacheBehavior = getDefaultCacheBehavior(
60+
Origins.Items[0].Id,
61+
inputs.defaults
62+
)
6063

6164
if (CacheBehaviors) {
6265
distributionConfig.CacheBehaviors = CacheBehaviors
@@ -108,7 +111,10 @@ const updateCloudFrontDistribution = async (cf, s3, distributionId, inputs) => {
108111
await updateBucketsPolicies(s3, Origins, s3CanonicalUserId)
109112
}
110113

111-
params.DistributionConfig.DefaultCacheBehavior = getDefaultCacheBehavior(Origins.Items[0].Id)
114+
params.DistributionConfig.DefaultCacheBehavior = getDefaultCacheBehavior(
115+
Origins.Items[0].Id,
116+
inputs.defaults
117+
)
112118
params.DistributionConfig.Origins = Origins
113119

114120
if (CacheBehaviors) {

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, options) => {
116
const distributionOrigins = {
127
Quantity: 0,
@@ -30,23 +25,7 @@ module.exports = (origins, options) => {
3025
const pathPatternConfig = origin.pathPatterns[pathPattern]
3126
const cacheBehavior = getCacheBehavior(pathPattern, pathPatternConfig, originConfig.Id)
3227

33-
const lambdaAtEdge = pathPatternConfig['lambda@edge'] || {}
34-
35-
Object.keys(lambdaAtEdge).forEach((eventType) => {
36-
if (!validLambdaTriggers.includes(eventType)) {
37-
throw new Error(
38-
`"${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.`
39-
)
40-
}
41-
42-
cacheBehavior.LambdaFunctionAssociations.Quantity =
43-
cacheBehavior.LambdaFunctionAssociations.Quantity + 1
44-
cacheBehavior.LambdaFunctionAssociations.Items.push({
45-
EventType: eventType,
46-
LambdaFunctionARN: lambdaAtEdge[eventType],
47-
IncludeBody: true
48-
})
49-
})
28+
addLambdaAtEdgeToCacheBehavior(cacheBehavior, pathPatternConfig['lambda@edge'])
5029

5130
distributionCacheBehaviors.Quantity = distributionCacheBehaviors.Quantity + 1
5231
distributionCacheBehaviors.Items.push(cacheBehavior)

serverless.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,10 @@ class CloudFront extends Component {
3636
})
3737

3838
if (this.state.id) {
39-
if (!equals(this.state.origins, inputs.origins)) {
39+
if (
40+
!equals(this.state.origins, inputs.origins) ||
41+
!equals(this.state.defaults, inputs.defaults)
42+
) {
4043
this.context.debug(`Updating CloudFront distribution of ID ${this.state.id}.`)
4144
this.state = await updateCloudFrontDistribution(cf, s3, this.state.id, inputs)
4245
}
@@ -47,6 +50,7 @@ class CloudFront extends Component {
4750

4851
this.state.region = inputs.region
4952
this.state.origins = inputs.origins
53+
this.state.defaults = inputs.defaults
5054
await this.save()
5155

5256
this.context.debug(`CloudFront deployed successfully with URL: ${this.state.url}.`)

0 commit comments

Comments
 (0)