Skip to main content
OpenTofu is an open-source fork of Terraform that provides a robust Infrastructure-as-Code (IaC) solution. It maintains compatibility with Terraform while offering additional features and community-driven development.

OpenTofu in env zero

env zero provides full support for OpenTofu deployments, allowing you to manage your infrastructure using OpenTofuโ€™s declarative configuration language. OpenTofu is the default Terraform binary in env zero, providing a reliable and open-source alternative to Terraform.

Configuration options

OpenTofu version selection

You can specify which version of OpenTofu to use in your template configuration:
  1. Navigate to your template settings
  2. Open the Advanced settings section
  3. Select your preferred OpenTofu version from the dropdown
OpenTofu version selection in Advanced settings

Working directory

Configure the working directory where your OpenTofu configuration files are located:
  • Root directory: Use the repository root (default)
  • Subdirectory: Specify a path relative to the repository root
If your OpenTofu files are in a subdirectory, specify the path in the Working Directory field under Advanced settings.

OpenTofu-specific features

State management

OpenTofu uses the same state management concepts as Terraform:
  • Local state: Stored temporarily during deployment
  • Remote state: Recommended for production environments
  • State locking: Prevents concurrent modifications
State persistenceenv zero stores the local state between deployments by default. For production environments, configure a remote backend in your OpenTofu configuration to ensure state persistence and team collaboration.

Variable handling

OpenTofu supports multiple variable input methods:
  • Environment variables: Set in env zeroโ€™s variable management
  • Variable files: .tfvars files in your repository
  • Command-line variables: Passed via -var flags
  • Variable precedence: Command-line > environment > variable files

Provider configuration

Configure OpenTofu providers in your configuration files:
main.tf
terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 5.0"
    }
  }
}

provider "aws" {
  region = var.aws_region
}

Deployment workflow

env zero follows OpenTofuโ€™s standard deployment workflow:
  1. Initialize: tofu init - Download providers and modules
  2. Plan: tofu plan - Generate execution plan
  3. Apply: tofu apply - Execute the planned changes
  4. Destroy: tofu destroy - Remove resources (when needed)

Plan and apply process

The plan step generates a detailed execution plan showing what changes will be made. The apply step uses this plan to make the actual changes to your infrastructure.
env zero stores plan files between the plan and apply steps to ensure consistency and allow for review and approval workflows.

Best practices

Remote backend configuration

Configure a remote backend for state management:
backend.tf
terraform {
  backend "s3" {
    bucket         = "your-terraform-state-bucket"
    key            = "path/to/your/state.tfstate"
    region         = "us-west-2"
    encrypt        = true
    dynamodb_table = "terraform-state-lock"
  }
}

Module usage

Organize your infrastructure using OpenTofu modules:
main.tf
module "vpc" {
  source = "./modules/vpc"
  
  vpc_cidr = "10.0.0.0/16"
  environment = var.environment
}

Variable management

Use consistent variable naming and types:
variables.tf
variable "environment" {
  description = "Environment name"
  type        = string
  validation {
    condition     = contains(["dev", "staging", "prod"], var.environment)
    error_message = "Environment must be dev, staging, or prod."
  }
}

Troubleshooting

Common issues

Provider version conflicts State file issues
  • Use remote backends for production environments
  • Enable state locking to prevent concurrent modifications
  • Regularly backup your state files
Variable resolution
  • Check variable precedence order
  • Verify environment variables are set correctly in env zero
  • Use tofu console to debug variable values

Debugging deployments

Enable detailed logging by setting the TF_LOG environment variable:
  • TF_LOG=DEBUG - Most verbose logging
  • TF_LOG=INFO - Standard information level
  • TF_LOG=WARN - Warning and error messages only
Set the TF_LOG environment variable in your templateโ€™s environment variables section for detailed OpenTofu execution logs.

Migration from Terraform

OpenTofu maintains compatibility with Terraform configurations:
  1. Configuration files: No changes required to .tf files
  2. State files: Compatible with Terraform state format
  3. Providers: Use the same provider ecosystem
  4. Modules: Existing Terraform modules work without modification
Your existing Terraform configurations will work with OpenTofu without any modifications.

Additional resources

โŒ˜I