Skip to content

Commit e7ce424

Browse files
add configurable default ttl
1 parent 4c7b4c1 commit e7ce424

File tree

4 files changed

+17
-5
lines changed

4 files changed

+17
-5
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ distribution:
5050
inputs:
5151
region: us-east-1
5252
enabled: true # optional
53+
defaults: # optional
54+
ttl: 15
5355
origins:
5456
- https://my-bucket.s3.amazonaws.com
5557
```

lib/getDefaultCacheBehavior.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module.exports = (originId) => {
1+
module.exports = (originId, defaults = {}) => {
22
return {
33
TargetOriginId: originId,
44
ForwardedValues: {
@@ -31,7 +31,7 @@ module.exports = (originId) => {
3131
}
3232
},
3333
SmoothStreaming: false,
34-
DefaultTTL: 86400,
34+
DefaultTTL: defaults.ttl || 86400,
3535
MaxTTL: 31536000,
3636
Compress: false,
3737
LambdaFunctionAssociations: {

lib/index.js

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

2929
// set first origin declared as the default cache behavior
30-
distributionConfig.DefaultCacheBehavior = getDefaultCacheBehavior(Origins.Items[0].Id)
30+
distributionConfig.DefaultCacheBehavior = getDefaultCacheBehavior(
31+
Origins.Items[0].Id,
32+
inputs.defaults
33+
)
3134

3235
if (CacheBehaviors) {
3336
distributionConfig.CacheBehaviors = CacheBehaviors
@@ -65,7 +68,10 @@ const updateCloudFrontDistribution = async (cf, distributionId, inputs) => {
6568

6669
const { Origins, CacheBehaviors } = parseInputOrigins(inputs.origins)
6770

68-
params.DistributionConfig.DefaultCacheBehavior = getDefaultCacheBehavior(Origins.Items[0].Id)
71+
params.DistributionConfig.DefaultCacheBehavior = getDefaultCacheBehavior(
72+
Origins.Items[0].Id,
73+
inputs.defaults
74+
)
6975
params.DistributionConfig.Origins = Origins
7076

7177
if (CacheBehaviors) {

serverless.js

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

3333
if (this.state.id) {
34-
if (!equals(this.state.origins, inputs.origins)) {
34+
if (
35+
!equals(this.state.origins, inputs.origins) ||
36+
!equals(this.state.defaults, inputs.defaults)
37+
) {
3538
this.context.debug(`Updating CloudFront distribution of ID ${this.state.id}.`)
3639
this.state = await updateCloudFrontDistribution(cf, this.state.id, inputs)
3740
}
@@ -42,6 +45,7 @@ class CloudFront extends Component {
4245

4346
this.state.region = inputs.region
4447
this.state.origins = inputs.origins
48+
this.state.defaults = inputs.defaults
4549
await this.save()
4650

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

0 commit comments

Comments
 (0)