Skip to content

Instantly share code, notes, and snippets.

@ashemedai
Last active November 24, 2022 12:21
Show Gist options
  • Select an option

  • Save ashemedai/759313479704f71f22bd7e5eb0793702 to your computer and use it in GitHub Desktop.

Select an option

Save ashemedai/759313479704f71f22bd7e5eb0793702 to your computer and use it in GitHub Desktop.
AWS CLI v2 installer/update script
#!/bin/sh
AWSCLI_DIST="awscliv2.zip"
LOCAL_BIN_DIR="${HOME}/.local/bin"
LOCAL_SHARE_DIR="${HOME}/.local/share/aws"
V2_SHARE_DIR="${LOCAL_SHARE_DIR}/v2"
if [ -z "$(which curl)" ] || [ -z "$(which unzip)" ]; then
echo "Please make sure curl and unzip are installed"
exit 1
fi
TMP_DIR=$(mktemp -d)
echo "Fetching latest AWS CLI v2 release file"
curl -sL "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "${TMP_DIR}/${AWSCLI_DIST}"
echo "Unpacking AWS CLI v2 files"
unzip -q "${TMP_DIR}/${AWSCLI_DIST}" -d "${TMP_DIR}"
if [ -z "$(which aws)" ]; then
echo "Installing AWS CLI v2 to your home directory"
"${TMP_DIR}"/aws/install -b "${LOCAL_BIN_DIR}" -i "${LOCAL_SHARE_DIR}"
echo 'Add ${HOME}/.local/bin to your PATH environment variable as needed to run "aws" directly'
else
echo "Updating existing AWS CLI v2 installation"
"${TMP_DIR}"/aws/install -u -b "${LOCAL_BIN_DIR}" -i "${LOCAL_SHARE_DIR}"
fi
if [ "$(ls -1 ${V2_SHARE_DIR} | wc -l)" -gt 5 ]; then
# No auto-cleanup since that needs semver comparisons
echo "You have 5 or more AWS CLI v2 versions in ${V2_SHARE_DIR}, please consider cleaning older versions"
echo "Currently occupying: $(du -sh ${V2_SHARE_DIR} | cut -f1)"
fi
echo "Cleaning installation files from ${TMP_DIR}"
rm -rf "${TMP_DIR}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment