Janier
Janier

Reputation: 4452

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?

Upvotes: 6

Views: 7412

Answers (3)

mikedanylov
mikedanylov

Reputation: 837

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 "/*"

Upvotes: 13

user1607158
user1607158

Reputation: 498

Please note that one problem with these solutions (as I have found out the hard way) is that the CloudFront Invalidation will probably run before the CodeDeploy has finished deploying to all EC2 instances.

Some factors are your Deployment Config, and how many EC2 instances you have. I'm thinking of adding some checks to this kind of process that gets the deployment status and will invalidate after a success

Upvotes: 0

Mark B
Mark B

Reputation: 201058

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

Upvotes: 6

Related Questions