Skip to content

Commit

Permalink
add cloudfront cache invalidation command (#36)
Browse files Browse the repository at this point in the history
* refactor and add s3 deployment for bucket deploy

* revert changes of pipeline

* add cloudfront cache invalidation command for build command
  • Loading branch information
raylrui authored Oct 3, 2024
1 parent 3e3f63b commit 7cebe0c
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions deploy/lib/application-stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,15 @@ export class ApplicationStack extends Stack {
blockPublicAccess: BlockPublicAccess.BLOCK_ALL,
});

this.setupS3CloudFrontIntegration(clientBucket, props.aliasDomainName);
this.buildReactApp(clientBucket, props.reactBuildEnvVariables);
const distribution = this.setupS3CloudFrontIntegration(clientBucket, props.aliasDomainName);
this.buildReactApp(clientBucket, props.reactBuildEnvVariables, distribution);
}

private buildReactApp(bucket: IBucket, reactBuildEnvVariables: Record<string, string>) {
private buildReactApp(
bucket: IBucket,
reactBuildEnvVariables: Record<string, string>,
distribution: Distribution
) {
const artifactBucketPrefix = 'artifact-source';

// This CodeBuild responsible for building the React app and publish the assets to S3
Expand All @@ -73,6 +77,8 @@ export class ApplicationStack extends Stack {
'env | grep VITE',
'yarn build',
'aws s3 rm s3://${VITE_BUCKET_NAME}/ --recursive && aws s3 sync ./dist s3://${VITE_BUCKET_NAME}',
// invalidate the cloudfront cache
'aws cloudfront create-invalidation --distribution-id ${VITE_CLOUDFRONT_DISTRIBUTION_ID} --paths "/*"',
],
},
},
Expand All @@ -88,6 +94,10 @@ export class ApplicationStack extends Stack {
value: bucket.bucketName,
type: BuildEnvironmentVariableType.PLAINTEXT,
},
VITE_CLOUDFRONT_DISTRIBUTION_ID: {
value: distribution.distributionId,
type: BuildEnvironmentVariableType.PLAINTEXT,
},
VITE_REGION: { value: 'ap-southeast-2', type: BuildEnvironmentVariableType.PLAINTEXT },
// CodeBuild is smart enough to give permission to these ssm parameters
VITE_COG_APP_CLIENT_ID: {
Expand Down Expand Up @@ -163,7 +173,7 @@ export class ApplicationStack extends Stack {
bucket.grantReadWrite(triggerCodeBuildLambda);
}

private setupS3CloudFrontIntegration(s3Bucket: IBucket, aliasDomainName: string[]) {
private setupS3CloudFrontIntegration(s3Bucket: IBucket, aliasDomainName: string[]): Distribution {
const hostedZoneName = StringParameter.valueForStringParameter(this, '/hosted_zone/umccr/name');
const hostedZoneId = StringParameter.valueForStringParameter(this, '/hosted_zone/umccr/id');

Expand Down Expand Up @@ -223,6 +233,8 @@ export class ApplicationStack extends Stack {
zone: hostedZone,
recordName: 'orcaui',
});

return cloudFrontDistribution;
}
}

Expand Down

0 comments on commit 7cebe0c

Please sign in to comment.