Skip to content

Commit ae9f6f3

Browse files
author
Allegra Chen
committed
Make the stage property optional and update the logic
1 parent 89668fb commit ae9f6f3

File tree

1 file changed

+33
-32
lines changed

1 file changed

+33
-32
lines changed

index.js

Lines changed: 33 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -27,38 +27,39 @@ class ServerlessLambdaEdgePreExistingCloudFront {
2727
for (let idx = 0; idx < events.length; idx += 1) {
2828
const event = events[idx]
2929

30-
if (event.preExistingCloudFront.stage == `${serverless.service.provider.stage}`) {
31-
const functionArn = await this.getlatestVersionLambdaArn(functionObj.name)
32-
const config = await this.provider.request('CloudFront', 'getDistribution', {
33-
Id: event.preExistingCloudFront.distributionId
34-
})
35-
36-
if (event.preExistingCloudFront.pathPattern === '*') {
37-
config.DistributionConfig.DefaultCacheBehavior.LambdaFunctionAssociations = await this.associateFunction(
38-
config.DistributionConfig.DefaultCacheBehavior.LambdaFunctionAssociations,
39-
event,
40-
functionObj.name,
41-
functionArn
42-
)
43-
} else {
44-
config.DistributionConfig.CacheBehaviors = await this.associateNonDefaultCacheBehaviors(
45-
config.DistributionConfig.CacheBehaviors,
46-
event,
47-
functionObj.name,
48-
functionArn
49-
)
50-
}
51-
52-
this.serverless.cli.consoleLog(
53-
`${functionArn} is associating to ${event.preExistingCloudFront.distributionId} CloudFront Distribution. waiting for deployed status.`
30+
if (event.preExistingCloudFront.stage !== undefined &&
31+
event.preExistingCloudFront.stage != `${serverless.service.provider.stage}`) { continue }
32+
33+
const functionArn = await this.getlatestVersionLambdaArn(functionObj.name)
34+
const config = await this.provider.request('CloudFront', 'getDistribution', {
35+
Id: event.preExistingCloudFront.distributionId
36+
})
37+
38+
if (event.preExistingCloudFront.pathPattern === '*') {
39+
config.DistributionConfig.DefaultCacheBehavior.LambdaFunctionAssociations = await this.associateFunction(
40+
config.DistributionConfig.DefaultCacheBehavior.LambdaFunctionAssociations,
41+
event,
42+
functionObj.name,
43+
functionArn
44+
)
45+
} else {
46+
config.DistributionConfig.CacheBehaviors = await this.associateNonDefaultCacheBehaviors(
47+
config.DistributionConfig.CacheBehaviors,
48+
event,
49+
functionObj.name,
50+
functionArn
5451
)
55-
56-
await this.provider.request('CloudFront', 'updateDistribution', {
57-
Id: event.preExistingCloudFront.distributionId,
58-
IfMatch: config.ETag,
59-
DistributionConfig: config.DistributionConfig
60-
})
6152
}
53+
54+
this.serverless.cli.consoleLog(
55+
`${functionArn} is associating to ${event.preExistingCloudFront.distributionId} CloudFront Distribution. waiting for deployed status.`
56+
)
57+
58+
await this.provider.request('CloudFront', 'updateDistribution', {
59+
Id: event.preExistingCloudFront.distributionId,
60+
IfMatch: config.ETag,
61+
DistributionConfig: config.DistributionConfig
62+
})
6263
}
6364
})
6465
}, Promise.resolve())
@@ -88,10 +89,10 @@ class ServerlessLambdaEdgePreExistingCloudFront {
8889
distributionId: { type: 'string' },
8990
eventType: { type: 'string' },
9091
pathPattern: { type: 'string' },
91-
includeBody: { type: 'boolean' },
92+
includeBody: { type: 'boolean' },
9293
stage: { type: 'string' }
9394
},
94-
required: ['distributionId', 'eventType', 'pathPattern', 'includeBody', 'stage']
95+
required: ['distributionId', 'eventType', 'pathPattern', 'includeBody']
9596
})
9697
}
9798
}

0 commit comments

Comments
 (0)