From fb188d9bf32ea2d2bb5f78ef3b1e934f2138ecff Mon Sep 17 00:00:00 2001 From: Octave Date: Thu, 17 Oct 2024 14:04:06 +0200 Subject: [PATCH] fix(cloudfront): re-implement waitUntilDistributionDeployed function --- src/aws-services.ts | 39 ++++++++++++++++++++++++++++++++++++++- src/cloudfront/index.ts | 1 + 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/src/aws-services.ts b/src/aws-services.ts index abe4aa0..e01547c 100644 --- a/src/aws-services.ts +++ b/src/aws-services.ts @@ -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 @@ -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 => { + + 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, diff --git a/src/cloudfront/index.ts b/src/cloudfront/index.ts index 9bf78ab..b0094f1 100644 --- a/src/cloudfront/index.ts +++ b/src/cloudfront/index.ts @@ -87,6 +87,7 @@ export const findDeployedCloudfrontDistribution = async ( client: cloudfront, maxWaitTime: 1500, }, { Id: distribution.Id }) + logger.info(`[CloudFront] ✅ Distribution deployed: ${distribution.Id}`) }