Skip to content

fix: retry on stale ETag #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 26, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 44 additions & 24 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,35 +27,55 @@ class ServerlessLambdaEdgePreExistingCloudFront {
for (let idx = 0; idx < events.length; idx += 1) {
const event = events[idx]
const functionArn = await this.getlatestVersionLambdaArn(functionObj.name)
const config = await this.provider.request('CloudFront', 'getDistribution', {
Id: event.preExistingCloudFront.distributionId
})

if (event.preExistingCloudFront.pathPattern === '*') {
config.DistributionConfig.DefaultCacheBehavior.LambdaFunctionAssociations = await this.associateFunction(
config.DistributionConfig.DefaultCacheBehavior.LambdaFunctionAssociations,
event,
functionObj.name,
functionArn
)
} else {
config.DistributionConfig.CacheBehaviors = await this.associateNonDefaultCacheBehaviors(
config.DistributionConfig.CacheBehaviors,
event,
functionObj.name,
functionArn
)
}

this.serverless.cli.consoleLog(
`${functionArn} is associating to ${event.preExistingCloudFront.distributionId} CloudFront Distribution. waiting for deployed status.`
)

await this.provider.request('CloudFront', 'updateDistribution', {
Id: event.preExistingCloudFront.distributionId,
IfMatch: config.ETag,
DistributionConfig: config.DistributionConfig
})
let retryCount = 5

const updateDistribution = async () => {
const config = await this.provider.request('CloudFront', 'getDistribution', {
Id: event.preExistingCloudFront.distributionId
})

if (event.preExistingCloudFront.pathPattern === '*') {
config.DistributionConfig.DefaultCacheBehavior.LambdaFunctionAssociations = await this.associateFunction(
config.DistributionConfig.DefaultCacheBehavior.LambdaFunctionAssociations,
event,
functionObj.name,
functionArn
)
} else {
config.DistributionConfig.CacheBehaviors = await this.associateNonDefaultCacheBehaviors(
config.DistributionConfig.CacheBehaviors,
event,
functionObj.name,
functionArn
)
}

await this.provider
.request('CloudFront', 'updateDistribution', {
Id: event.preExistingCloudFront.distributionId,
IfMatch: config.ETag,
DistributionConfig: config.DistributionConfig
})
.catch(async (error) => {
if (error.providerError.code === 'PreconditionFailed' && retryCount > 0) {
this.serverless.cli.consoleLog(
`received precondition failed error, retrying... (${retryCount}/5)`
)
retryCount -= 1
await new Promise((res) => setTimeout(res, 5000))
return updateDistribution()
}
this.serverless.cli.consoleLog(error)
throw error
})
}

await updateDistribution()
}
})
}, Promise.resolve())
Expand Down