Sling Academy
Home/DevOps/3 ways to switch between different versions of Terraform

3 ways to switch between different versions of Terraform

Last updated: February 03, 2024

Overview

Managing multiple versions of Terraform can be essential for DevOps professionals and teams working on different projects or with various infrastructures. This comprehensive tutorial will guide you through the processes of switching between different Terraform versions, from basic techniques to more advanced practices. Understanding how to manage Terraform versions effectively can help ensure compatibility and prevent potential deployment issues.

Understanding Terraform Versioning

Terraform uses semantic versioning, which includes a major, minor, and patch version. These versions represent backward compatibility, new features, and bug fixes, respectively. When working on different projects, you may encounter the need to use specific Terraform versions to maintain compatibility with modules or providers.

Method 1: Using Terraform Version Manager (tfenv)

tfenv is a version manager for Terraform, much like nvm is for Node.js or rbenv is for Ruby. It simplifies the process of swapping between Terraform versions. Here’s how to install and use it:

  1. First, install tfenv on your system. For macOS users, you can use Homebrew:
    brew install tfenv

    Linux users can clone the tfenv repository and add it to their PATH:

    git clone https://github.com/tfutils/tfenv.git ~/.tfenv
    echo 'export PATH="$HOME/.tfenv/bin:$PATH"' >> ~/.bash_profile
    source ~/.bash_profile
  2. To install a specific Terraform version, use:
    tfenv install 0.12.29
  3. To switch between installed Terraform versions, use:
    tfenv use 0.12.29

Running terraform version should now display the version you selected.

Method 2: Manually Managing Terraform Binaries

If you prefer manual management or need a solution that tfenv doesn’t cover, you can manually switch Terraform versions by managing the binaries yourself:

  1. Download the desired Terraform version from the Terraform website.
  2. Unzip the package and move the Terraform binary to a desired directory.
  3. Update your system’s PATH variable to point to the directory containing the Terraform binary you wish to use.

Remember to remove or update the PATH variable when switching versions to avoid conflicts.

Method 3: Using Docker to Manage Terraform Versions

A modern approach to managing Terraform versions involves using Docker. By pulling specific Terraform version tags from Docker Hub, you can easily switch between versions without altering your system configuration:

  1. Pull the desired Terraform version from Docker Hub:
    docker pull hashicorp/terraform:0.12.29
  2. Use the Docker image to run Terraform commands. Replace the version in the following command with the one you wish to use:
    docker run -i -t hashicorp/terraform:0.12.29 version

This method encapsulates Terraform in a container, preventing version conflicts and keeping your system clean.

Conclusion

Switching between different Terraform versions is a crucial skill for anyone working with infrastructure as code. Whether you prefer tfenv, manual management, or using Docker, each method has its advantages. Understanding and utilizing these techniques will help maintain project compatibility and ensure smooth deployments across various environments.

Next Article: How to upgrade Terraform provider versions

Previous Article: Terraform CLI commands: The complete cheat sheet

Series: Terraform Tutorials

DevOps

You May Also Like

  • How to reset Ubuntu to factory settings (4 approaches)
  • Making GET requests with cURL: A practical guide (with examples)
  • Git: What is .DS_Store and should you ignore it?
  • NGINX underscores_in_headers: Explained with examples
  • How to use Jenkins CI with private GitHub repositories
  • Terraform: Understanding State and State Files (with Examples)
  • SHA1, SHA256, and SHA512 in Terraform: A Practical Guide
  • CSRF Protection in Jenkins: An In-depth Guide (with examples)
  • Terraform: How to Merge 2 Maps
  • Terraform: How to extract filename/extension from a path
  • JSON encoding/decoding in Terraform: Explained with examples
  • Sorting Lists in Terraform: A Practical Guide
  • Terraform: How to trigger a Lambda function on resource creation
  • How to use Terraform templates
  • Understanding terraform_remote_state data source: Explained with examples
  • Jenkins Authorization: A Practical Guide (with examples)
  • Solving Jenkins Pipeline NotSerializableException: groovy.json.internal.LazyMap
  • Understanding Artifacts in Jenkins: A Practical Guide (with examples)
  • Using Jenkins with AWS EC2 and S3: A Practical Guide