Copy files to S3 and invalidate CloudFront cache


Copy to S3

To copy a local file to S3:

$ aws s3 cp build/index.html s3://mybucket/index.html

To copy multiple HTML files:

$ aws s3 cp build/ s3://mybucket/ --exclude '*' --include '*.html'

The reason --exclude comes before --include is because all files are included by default.

If you want to have multiple includes:

$ aws s3 cp build/ s3://mybucket/ --exclude '*' --include '*.html' --include '*.js'

There’s also the --recursive option to match files in subdirectories.

The next step is to invalidate the files from the CloudFront edge caches.

Invalidate CloudFront

To invalidate a single file:

$ aws cloudfront create-invalidation --distribution-id $CDN_DISTRIBUTION_ID --paths '/index.html'

To invalidate multiple HTML files using the * wildcard:

$ aws cloudfront create-invalidation --distribution-id $CDN_DISTRIBUTION_ID --paths '/*'

When using the * wildcard character, make sure to wrap the path with quotes (' or ") or else shell expansion will occur.



If you enjoyed this post, please consider supporting this site!