githubEdit

How do I invalidate CloudFront cache when deploying updates to my S3-hosted web application?

Context When deploying updates to a web application hosted on S3 and served through CloudFront, the changes may not be immediately visible due to CloudFront's caching mechanism. To ensure that users see the latest version of your application, you need to invalidate the CloudFront cache after deploying new files. Answer You can automate CloudFront cache invalidation by adding it to your GitHub Actions workflow. Here's how to implement this: Add the following step to your GitHub Actions workflow YAML file after your deployment step: yaml - name: Invalidate CloudFront uses: aws-actions/configure-aws-credentials@v1 with: aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} aws-region: us-east-1 - name: Create CloudFront invalidation run: | aws cloudfront create-invalidation \ --distribution-id YOUR_DISTRIBUTION_ID \ --paths "/*" Replace YOUR_DISTRIBUTION_ID with your CloudFront distribution ID. You can find this ID in your AWS Console under the CloudFront distributions section. Make sure your AWS credentials have the necessary permissions to create CloudFront invalidations. This will automatically invalidate the CloudFront cache whenever your workflow runs, ensuring that users always see the latest version of your application.

Last updated

Was this helpful?