Skip to content

Instantly share code, notes, and snippets.

@amancevice
Last active March 30, 2021 13:39
Show Gist options
  • Save amancevice/6060f2b82f15f3831f7a40c5b2b33b06 to your computer and use it in GitHub Desktop.
Save amancevice/6060f2b82f15f3831f7a40c5b2b33b06 to your computer and use it in GitHub Desktop.
Terraform version switching

Terraform Version Switching

Add the following to your bash profile:

function terraform-use {
  vsn=$1
  pkg="terraform_${vsn}_darwin_amd64.zip"
  url="https://releases.hashicorp.com/terraform/$vsn/$pkg"
  tf="$( which terraform || echo /usr/local/bin/terraform )"
  if [ -e "$tf-$vsn" ]; then
    ln -Fs "$tf-$vsn" "$tf"
  elif curl --head --fail "$url" 2> /dev/null; then
    curl -o "/tmp/$pkg" "$url"
    (
      cd /tmp/
      unzip -o "/tmp/$pkg"
      rm "/tmp/$pkg"
      mv terraform "$tf-$vsn"
    )
    ln -Fs "$tf-$vsn" "$tf"
  else
    echo "ERROR \`$url\` not found"
    return 1
  fi
  terraform -version
}

Usage:

terraform-use x.y.z
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment