mauvehed's dotfiles for personal and work environments

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:

  1. List available Terraform versions:

    tfenv list-remote
    
  2. Install a specific version:

    tfenv install <version>
    # Example: tfenv install 1.0.0
    

    To install the latest stable version:

    tfenv install latest
    
  3. Select a Terraform version to use:

    • Globally:
      tfenv use <version>
      # Example: tfenv use 1.0.0
      
    • Per project (recommended): Create a .terraform-version file in your project's root directory containing the desired version number:
      # .terraform-version
      1.0.0
      
      tfenv will automatically pick up this version when you cd into the directory.
  4. 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.

  1. 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 init
    

    This command also initializes backend configuration (for state storage) and downloads provider plugins.

  2. 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=tfplan
    

    This is a good way to check whether the proposed changes match your expectations before applying them.

  3. 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
    
  4. terraform destroy: Destroys all remote objects managed by a particular Terraform configuration.

    terraform destroy
    

    Use 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 -check
    
  • terraform validate: Validates the syntax and arguments of configuration files.
    terraform validate
    
  • terraform 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 list
    
  • terraform workspace list/select/new/delete: Manages Terraform workspaces for different environments (e.g., dev, staging, prod).

Resources#

This document provides fundamental guidance. Terraform is a comprehensive tool; always refer to the official documentation for detailed information and best practices.