Terraform Usage#
This document provides a guide to using Terraform for managing infrastructure as code, including version management with tfenv.
Overview#
Terraform is an open-source infrastructure as code (IaC) software tool that enables you to safely and predictably create, change, and improve infrastructure. It codifies APIs into declarative configuration files.
tfenv is a Terraform version manager, allowing you to easily switch between different versions of Terraform on a per-project basis.
Installation#
tfenv (Terraform Version Manager)#
It's recommended to install tfenv first to manage your Terraform versions.
Installation (macOS with Homebrew):
brew install tfenv
For other installation methods (e.g., Git clone), refer to the tfenv installation guide.
Post-installation:
Ensure tfenv is added to your shell's PATH. If installed via Homebrew, this is usually handled automatically. Otherwise, you might need to add $(brew --prefix tfenv)/bin (or the equivalent for your installation method) to your PATH.
Terraform (via tfenv)#
Once tfenv is installed, you can install specific Terraform versions:
-
List available Terraform versions:
tfenv list-remote -
Install a specific version:
tfenv install <version> # Example: tfenv install 1.0.0To install the latest stable version:
tfenv install latest -
Select a Terraform version to use:
- Globally:
tfenv use <version> # Example: tfenv use 1.0.0 - Per project (recommended): Create a
.terraform-versionfile in your project's root directory containing the desired version number:# .terraform-version 1.0.0tfenvwill automatically pick up this version when youcdinto the directory.
- Globally:
-
Verify installation:
terraform --version which terraform # Should point to a tfenv shim
Basic Terraform Workflow#
Terraform commands are typically run within a directory containing your .tf configuration files.
-
terraform init: Initializes a working directory containing Terraform configuration files. This is the first command that should be run after writing a new Terraform configuration or cloning an existing one.terraform initThis command also initializes backend configuration (for state storage) and downloads provider plugins.
-
terraform plan: Creates an execution plan. Terraform performs a refresh, unless explicitly disabled, and then determines what actions are necessary to achieve the desired state specified in the configuration files.terraform plan # Save the plan to a file: # terraform plan -out=tfplanThis is a good way to check whether the proposed changes match your expectations before applying them.
-
terraform apply: Applies the changes required to reach the desired state of the configuration, or the pre-determined changes if a plan file is provided.terraform apply # Apply a saved plan (prompts for confirmation): # terraform apply tfplan # Auto-approve (use with caution): # terraform apply -auto-approve # Or apply a saved plan without prompting: # terraform apply -auto-approve tfplan -
terraform destroy: Destroys all remote objects managed by a particular Terraform configuration.terraform destroyUse with extreme caution, as this will remove your managed infrastructure.
Common Terraform Commands#
terraform fmt: Rewrites Terraform configuration files to a canonical format and style.terraform fmt # Check for formatting issues: terraform fmt -checkterraform validate: Validates the syntax and arguments of configuration files.terraform validateterraform output: Reads an output variable from a Terraform state file.terraform output <output_variable_name>terraform state list: Lists resources within a Terraform state file.terraform state listterraform workspace list/select/new/delete: Manages Terraform workspaces for different environments (e.g., dev, staging, prod).
Resources#
- Official Terraform Documentation: https://developer.hashicorp.com/terraform/docs
- Terraform CLI Commands: https://developer.hashicorp.com/terraform/cli/commands
tfenvGitHub Repository: https://github.com/tfutils/tfenv- Terraform Registry (Providers & Modules): https://registry.terraform.io/
This document provides fundamental guidance. Terraform is a comprehensive tool; always refer to the official documentation for detailed information and best practices.