Home Linux & Systems Cybersecurity Cloud & DevOps Networks & Infrastructure SIEM & Monitoring DFIR & Threat Intel Development & Other All categories Projects About Tools

Terraform Version Management with tfenv

Leer en espanol
Terraform Version Management with tfenv

Table of contents

This manual aims to establish the standardized procedure for the efficient installation and management of multiple versions of the Terraform framework using the tfenv tool. ===

Purpose

This manual aims to establish the standardized procedure for the efficient installation and management of multiple versions of the framework Terraform using the tool tfenv. The ability to switch between versions is essential in multi-project environments or that require support for specific modules.

1. Introduction to tfenv

tfenv is a Terraform version manager (Terraform Version Manager), similar to tools like nvm (for Node.js) or pyenv (for Python).

Key Features

  • Quick Installation: Install any version of Terraform with a single command.

  • Dynamic Alternation: Change the global version of Terraform instantly.

  • Insulation by Project: Define a specific version of Terraform per project directory.

  • Compatibility: Ensure environment consistency across CI/CD pipelines and team environments.

2. Installation and Configuration of tfenv

2.1. Repository Cloning

Clone the repository tfenv in the directory home of the user (~/.tfenv).

CODE
git clone https://github.com/tfutils/tfenv.git ~/.tfenv

2.2. PATH Configuration

Integrate the binary directory tfenv (~/.tfenv/bin) to the PATH system to make commands accessible.

BASH
echo 'export PATH=""$HOME/.tfenv/bin:$PATH""' >> ~/.bashrc
echo 'export PATH=""$HOME/.tfenv/bin:$PATH""' >> ~/.zshrc

Note: After executing the command, it is necessary reload configuration shell to apply the changes.

  • For Bash: source ~/.bashrc

  • For Zsh: source ~/.zshrc

23. Installation Verification

Confirm that tfenv has been installed successfully by running the version command.

CODE
tfenv --version

3. Terraform Version Management

3.1. Version Installation

Use the command install to download and add specific versions to the local environment.

Aim Command
Install a specific version tfenv install 1.7.5
Install another version (example) tfenv install 1.6.0
Install the latest stable version tfenv install latest

3.2. Version List

Aim Command Description
Check installed versions tfenv list Shows the Terraform versions available locally.
See available versions tfenv list-remote Lists all Terraform versions available in the HashiCorp registry.

 

3.3. Global Version Switching

The command use activates the desired version of Terraform globally for the system.

PHP
tfenv use 1.7.5

Verification: Confirm the active version globally:

TERRAFORM
terraform version

4. Insulation by Project (Recommended)

Best practice in work environments is to lock in the Terraform version within the project, ensuring that all team members and CI/CD processes use the same one. release.

4.1. Version Definition

Inside the root directory of your project (where the files are located .tf), create a file called .terraform-version containing only the required version number.

BASH
# Ejemplo: Estandarizando la versión 1.7.5 para el proyecto
echo "1.7.5" > .terraform-version

4.2. Automated Behavior

When you run any Terraform command or tfenv inside this directory, tfenv will automatically detect the file .terraform-version and:

  1. Will install the version if it is not present locally.
  2. will change to that version, temporarily overriding the global version of the system.

This file must be commissioned and uploaded to the repository to ensure uniformity across the team.

5. Maintenance

5.1. Update

To install and downgrade to the latest available version of Terraform:

PHP
tfenv install latest
tfenv use latest

5.2. Uninstallation

To remove a version of Terraform that is no longer needed:

CODE
tfenv uninstall 1.6.0

:wq!

 

 

Comments