Skip to content

Add CloudFront invalidation to CodePipeline build stage - #4

Draft
ak4dev with Copilot wants to merge 2 commits into
masterfrom
copilot/review-buildspec-for-cloudfront
Draft

Add CloudFront invalidation to CodePipeline build stage#4
ak4dev with Copilot wants to merge 2 commits into
masterfrom
copilot/review-buildspec-for-cloudfront

Conversation

Copilot AI commented Mar 14, 2026

Copy link
Copy Markdown
Contributor

The buildspec.yml had no post-deployment step, leaving CloudFront serving stale assets after each pipeline run.

Changes

  • buildspec.yml — new post_build phase with three guarded steps:
    • aws s3 sync dist/ s3://$S3_BUCKET --delete — syncs built assets, removes stale files
    • aws cloudfront create-invalidation on $DISTRIBUTION_ID — primary distribution invalidation
    • Loop over space-separated $EXTRA_DISTRIBUTION_IDS — handles multiple distributions (aligns with the DEV_DIST_ID/IO_DIST_ID pattern already in package.json)

All steps are no-ops when their env var is unset — PR/local builds are unaffected.

Required CodeBuild configuration

Env var Value
S3_BUCKET S3 origin bucket name
DISTRIBUTION_ID Primary CloudFront distribution ID
EXTRA_DISTRIBUTION_IDS (optional) Space-separated additional distribution IDs

The CodeBuild IAM role also needs s3:PutObject/s3:DeleteObject/s3:ListBucket on the bucket and cloudfront:CreateInvalidation on the distributions.

Original prompt

Review the buildspec.yml. This is deployed via an AWS code pipeline. Is it possible to invalidate the cloudfront distributions upon deployment in the pipeline

Created from VS Code.


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Co-authored-by: ak4dev <92195328+ak4dev@users.noreply.github.com>
Copilot AI changed the title [WIP] Review buildspec.yml for CloudFront invalidation on deployment Add CloudFront invalidation to CodePipeline build stage Mar 14, 2026
Copilot AI requested a review from ak4dev March 14, 2026 12:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants