Skip to content

Commit 3a081ed

Browse files
authored
Merge pull request #4 from mAiNiNfEcTiOn/master
fix: Race condition when updating the distribution
2 parents 7637be0 + 40def6d commit 3a081ed

File tree

1 file changed

+21
-11
lines changed

1 file changed

+21
-11
lines changed

index.js

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,21 @@ class ServerlessLambdaEdgePreExistingCloudFront {
1111

1212
this.hooks = {
1313
'after:aws:deploy:finalize:cleanup': async () => {
14-
this.serverless.service.getAllFunctions().forEach(async (functionName) => {
15-
const functionObj = this.serverless.service.getFunction(functionName)
16-
if (functionObj.events) {
17-
functionObj.events.forEach(async (event) => {
18-
if (event.preExistingCloudFront && this.checkAllowedDeployStage()) {
14+
await this.serverless.service
15+
.getAllFunctions()
16+
.filter((functionName) => {
17+
const functionObj = this.serverless.service.getFunction(functionName)
18+
return functionObj.events
19+
})
20+
.reduce((promiseOutput, functionName) => {
21+
return promiseOutput.then(async () => {
22+
const functionObj = this.serverless.service.getFunction(functionName)
23+
const events = functionObj.events.filter(
24+
(event) => event.preExistingCloudFront && this.checkAllowedDeployStage()
25+
)
26+
27+
for (let idx = 0; idx < events.length; idx += 1) {
28+
const event = events[idx]
1929
const functionArn = await this.getlatestVersionLambdaArn(functionObj.name)
2030
const config = await this.provider.request('CloudFront', 'getDistribution', {
2131
Id: event.preExistingCloudFront.distributionId
@@ -37,18 +47,18 @@ class ServerlessLambdaEdgePreExistingCloudFront {
3747
)
3848
}
3949

40-
this.provider.request('CloudFront', 'updateDistribution', {
50+
this.serverless.cli.consoleLog(
51+
`${functionArn} is associating to ${event.preExistingCloudFront.distributionId} CloudFront Distribution. waiting for deployed status.`
52+
)
53+
54+
await this.provider.request('CloudFront', 'updateDistribution', {
4155
Id: event.preExistingCloudFront.distributionId,
4256
IfMatch: config.ETag,
4357
DistributionConfig: config.DistributionConfig
4458
})
45-
this.serverless.cli.consoleLog(
46-
`${functionArn} is associating to ${event.preExistingCloudFront.distributionId} CloudFront Distribution. waiting for deployed status.`
47-
)
4859
}
4960
})
50-
}
51-
})
61+
}, Promise.resolve())
5262
}
5363
}
5464
}

0 commit comments

Comments
 (0)