Skip to content

Instantly share code, notes, and snippets.

@JoshSalway
Last active October 4, 2023 07:26
Show Gist options
  • Save JoshSalway/594108f4d388c733806d112af90b0695 to your computer and use it in GitHub Desktop.
Save JoshSalway/594108f4d388c733806d112af90b0695 to your computer and use it in GitHub Desktop.
#!/bin/zsh
install_aws_cli () {
echo "Downloading AWS CLI..."
curl "https://awscli.amazonaws.com/AWSCLIV2.pkg" -o "AWSCLIV2.pkg"
echo "Installing AWS CLI..."
sudo installer -pkg AWSCLIV2.pkg -target /
rm -f AWSCLIV2.pkg
echo "Package file deleted successfully!"
echo "AWS CLI installed successfully!"
}
echo "Checking current AWS CLI version..."
if ! command -v aws &> /dev/null; then
echo "AWS CLI not installed. Installing now..."
install_aws_cli
else
current_version=$(aws --version 2>&1 | grep -oE 'aws-cli/[0-9]+\.[0-9]+\.[0-9]+' | cut -d'/' -f2)
echo "Current AWS CLI version: $current_version"
echo "Checking for the latest version..."
latest_version=$(curl -s https://raw.githubusercontent.com/aws/aws-cli/v2/CHANGELOG.rst | grep -oE '[0-9]+\.[0-9]+\.[0-9]+' | head -n 1)
echo "Latest AWS CLI version: $latest_version"
if [[ "$latest_version" != "$current_version" ]]; then
echo "Upgrading AWS CLI..."
install_aws_cli
else
echo "AWS CLI is already up to date. No upgrade necessary."
fi
fi
@JoshSalway
Copy link
Author

JoshSalway commented Jul 12, 2023

AWS CLI Upgrade Script

This script checks the current version of the AWS Command Line Interface (CLI) installed on your machine and compares it with the latest version available in the AWS CLI GitHub repository. If an upgrade is required, it automatically downloads and installs the latest version.

Usage
To use the script, follow these steps:

Open a terminal.

Download the script using curl:

curl -O https://gist.githubusercontent.com/JoshSalway/594108f4d388c733806d112af90b0695/raw/443c7d49acc9afbe065cdfcae3166a1dc3f9f40a/awscli-upgrade.sh

Make the script executable:

chmod +x awscli-upgrade.sh

Run the script:

./awscli-upgrade.sh

The script will display the current version of the AWS CLI installed on your machine and compare it with the latest version available. If an upgrade is necessary, it will automatically download and install the latest version.

Note: Make sure you have the necessary permissions to execute the script and install software on your machine.


Setting up an Alias:

To make it easier to run the script, you can set up an alias. Here's how:

Open your shell's configuration file. For example, for Bash, open ~/.bashrc or ~/.bash_profile. For Zsh, open ~/.zshrc.

Add the following line to create an alias:

alias awscli-upgrade="/path/to/awscli-upgrade.sh"

Replace /path/to/awscli-upgrade.sh with the actual path where you saved the script.

Save the file and exit the editor.

Restart your terminal or run the following command to reload the shell configuration:

source ~/.bashrc

Replace ~/.bashrc with the path to your shell's configuration file.

You can now use the awscli-upgrade alias to run the script:
awscli-upgrade

Please note that the script assumes you have the necessary permissions to install software on your machine. Use it at your own risk.

@JoshSalway
Copy link
Author

JoshSalway commented Oct 4, 2023

Example output:

Checking current AWS CLI version...
Current AWS CLI version: 2.13.1
Checking for the latest version...
Latest AWS CLI version: 2.13.23
Upgrading AWS CLI...
Downloading AWS CLI...
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 36.3M  100 36.3M    0     0  6236k      0  0:00:05  0:00:05 --:--:-- 6476k
Installing AWS CLI...
Password:
installer: Package name is AWS Command Line Interface
installer: Upgrading at base path /
installer: The upgrade was successful.
Package file deleted successfully!
AWS CLI installed successfully!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment