Skip to content

Instantly share code, notes, and snippets.

@rkmax
Created July 2, 2019 15:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rkmax/d6f18ccbd95c56ab7ea198e57cb380e2 to your computer and use it in GitHub Desktop.
Save rkmax/d6f18ccbd95c56ab7ea198e57cb380e2 to your computer and use it in GitHub Desktop.
Deploys a directory to s3 bucket
#!/usr/bin/env bash
usage() {
cat << EOF
NAME
deploy-cloudfront-page
SYNOPSIS
deploy-cloudfront-page </path/directory/dist> <s3-bucket-name> [cf-distribution-id]
DESCRIPTION
Deploys a directory to s3 bucket. The path to deploy and s3 bucket are mandatory
cloudfront distribution id is optional, if it's present an cloud-front invalidation
attempt will be make
EOF
}
check_aws_installation() {
command -v aws >/dev/null 2>&1 || {
echo >&2 "Amazon AWS cli tools are required to be installed. Aborting.";
exit 1;
}
}
deploy() {
local local_directory="$(echo ${1} |sed 's:/*$::' )/" # ensure always have a end trailing slash
local s3_bucket=${2}
aws s3 sync --acl public-read ${local_directory} s3://${s3_bucket}
}
invalidate() {
local cf_distribution=${1}
aws cloudfront create-invalidation --distribution-id ${cf_distribution} --paths '/*'
}
if [[ -z ${1} ]]; then
echo "path to deploy is mandatory"
usage
exit 1
fi
if [[ -z ${2} ]]; then
echo "bucket-name is mandatory"
usage
exit 1
fi
check_aws_installation
deploy "$@"
if [[ -n "${3}" ]]; then
invalidate ${3}
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment