Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle cloudfront cache after deployment

we have our Angular2 code in S3 .And we access it via Cloudfront. It works fine. But after a deployment to Angular2 , we want every code to be invalidated from Cloudfront. What are the best approaches for clearing cache after deployment? How to handle cloudfront caching?

like image 737
Janier Avatar asked Dec 06 '22 15:12

Janier


2 Answers

You can do both deployment and cache invalidation with the help of aws-cli.

#!/bin/bash

# enable cloudfront cli
aws configure set preview.cloudfront true

# deploy angular bundles
aws s3 sync $LOCAL s3://$S3_BUCKET \
    --region=eu-central-1 \
    --cache-control max-age=$CACHE_TIME

# invalidate cache in cloudfront
aws cloudfront create-invalidation \
    --distribution-id $CLOUDFRONT_DISTRO_ID \
    --paths "/*"
like image 174
mikedanylov Avatar answered Dec 09 '22 16:12

mikedanylov


You will need to call the CloudFront API (or use the web console) to invalidate the cache. Here is the documentation

like image 37
Mark B Avatar answered Dec 09 '22 15:12

Mark B