Created
January 7, 2025 21:13
-
-
Save leegilmorecode/b49bf4a8de753f622dbe5aed1fffcf9d to your computer and use it in GitHub Desktop.
Deploy a client static website build to an Amazon S3 bucket and create a cache invalidation for the Amazon CloudFront distribution
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# run example: ./deploy-client.sh <s3-bucket-name> <cloudfront-distribution-id> <path-to-local-files> | |
if [ $# -ne 3 ]; then | |
echo "Usage: $0 <s3-bucket-name> <cloudfront-distribution-id> <path-to-local-files>" | |
exit 1 | |
fi | |
BUCKET_NAME=$1 | |
DISTRIBUTION_ID=$2 | |
LOCAL_PATH=$3 | |
echo "Deleting all objects in S3 bucket: $BUCKET_NAME" | |
aws s3 rm s3://$BUCKET_NAME --recursive | |
if [ $? -ne 0 ]; then | |
echo "Failed to delete objects in the bucket. Exiting." | |
exit 1 | |
fi | |
echo "Copying local files to S3 bucket: $BUCKET_NAME" | |
aws s3 cp $LOCAL_PATH s3://$BUCKET_NAME --recursive | |
if [ $? -ne 0 ]; then | |
echo "Failed to copy files to the bucket. Exiting." | |
exit 1 | |
fi | |
echo "Invalidating CloudFront cache for distribution: $DISTRIBUTION_ID" | |
INVALIDATION_ID=$(aws cloudfront create-invalidation \ | |
--distribution-id $DISTRIBUTION_ID \ | |
--paths "/*" \ | |
--query 'Invalidation.Id' \ | |
--output text) | |
if [ $? -ne 0 ]; then | |
echo "Failed to create CloudFront invalidation. Exiting." | |
exit 1 | |
fi | |
echo "Invalidation created successfully with ID: $INVALIDATION_ID" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment