Skip to content

Commit

Permalink
Merge pull request #72 from lalalilo/fix-aws-sdk-wait-until-distribut…
Browse files Browse the repository at this point in the history
…ion-deployed

fix(cloudfront): re-implement waitUntilDistributionDeployed function
  • Loading branch information
OctaveLaventure authored Oct 17, 2024
2 parents d4a35f1 + fb188d9 commit 95ea829
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
39 changes: 38 additions & 1 deletion src/aws-services.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { ACM, waitUntilCertificateValidated } from '@aws-sdk/client-acm'
import { CloudFront, waitUntilDistributionDeployed, waitUntilInvalidationCompleted } from '@aws-sdk/client-cloudfront'
import { CloudFront, Distribution, waitUntilInvalidationCompleted } from '@aws-sdk/client-cloudfront'
import { IAM } from '@aws-sdk/client-iam'
import { Lambda } from '@aws-sdk/client-lambda'
import { Route53 } from '@aws-sdk/client-route-53'
import { S3 } from '@aws-sdk/client-s3'
import { logger } from './logger'

// Bucket region must be fixed so that website endpoint is fixe
// https://docs.aws.amazon.com/fr_fr/general/latest/gr/s3.html
Expand Down Expand Up @@ -61,6 +62,42 @@ export const getS3DomainName = (domainName: string) =>
export const getOriginId = (domainName: string) =>
`S3-Website-${getS3DomainName(domainName)}`


// re-implement aws-sdk's waitUntilDistributionDeployed() because the aws-sdk one is bugged
const waitUntilDistributionDeployed = async (params: {
client: CloudFront,
maxWaitTime: number,
}, distribution: { Id: string }): Promise<Distribution> => {

return new Promise((resolve, reject) => {
const timeout = setTimeout(() => {
clearInterval(interval)
reject(new Error(`[CloudFront] ❌ Distribution not deployed after ${params.maxWaitTime} seconds`))
}, params.maxWaitTime * 1000)

const interval = setInterval(async () => {
try {
const { Distribution } = await params.client.getDistribution({ Id: distribution.Id })

if (!Distribution) {
return
}

logger.info(`[CloudFront] 🔄 Checking distribution status: ${Distribution.Id} has status ${Distribution.Status}`)
if (Distribution.Status === 'Deployed') {
clearInterval(interval)
clearTimeout(timeout)
resolve(Distribution)
}
} catch (error) {
clearInterval(interval)
clearTimeout(timeout)
reject(error)
}
}, 10000)
});
}

export const waitUntil = {
distributionDeployed: waitUntilDistributionDeployed,
certificateValidated: waitUntilCertificateValidated,
Expand Down
1 change: 1 addition & 0 deletions src/cloudfront/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ export const findDeployedCloudfrontDistribution = async (
client: cloudfront,
maxWaitTime: 1500,
}, { Id: distribution.Id })

logger.info(`[CloudFront] ✅ Distribution deployed: ${distribution.Id}`)
}

Expand Down

0 comments on commit 95ea829

Please sign in to comment.