Last active
March 3, 2024 21:02
-
-
Save mortn/6489e9304193244421fd372a2658e4a3 to your computer and use it in GitHub Desktop.
Bash script to setup latest version of Terraform
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 | |
# Function to extract the latest version number from the Terraform website | |
get_latest_version() { | |
curl -s https://releases.hashicorp.com/terraform/index.json | jq -r '.versions[].version' | grep -E '^[0-9]+\.[0-9]+\.[0-9]+$' | sort -V | tail -n1 | |
} | |
# Function to download and install Terraform | |
install_terraform() { | |
version=$1 | |
curl -LO https://releases.hashicorp.com/terraform/${version}/terraform_${version}_linux_amd64.zip | |
unzip terraform_${version}_linux_amd64.zip | |
sudo mv terraform /usr/local/bin/ | |
rm terraform_${version}_linux_amd64.zip | |
} | |
latest_version=$(get_latest_version) | |
if [ -z "$latest_version" ]; then | |
echo "Failed to retrieve the latest version of Terraform." | |
exit 1 | |
fi | |
echo "Downloading and installing Terraform version: $latest_version" | |
install_terraform $latest_version | |
echo "Terraform installation complete." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment