Python wrapper for HashiCorp's Terraform command-line tool providing programmatic access to all Terraform functionality
npx @tessl/cli install tessl/pypi-python-terraform@0.14.0A Python wrapper for HashiCorp's Terraform command-line tool that enables developers to execute Terraform commands programmatically within Python applications. This library provides a comprehensive interface that mirrors Terraform's CLI functionality, supporting all standard commands with proper argument handling and Python-friendly option conversion.
pip install python-terraformfrom python_terraform import Terraform, IsFlagged, IsNotFlaggedFor all exports including state management and error handling:
from python_terraform import (
Terraform,
Tfstate,
TerraformCommandError,
VariableFiles,
IsFlagged,
IsNotFlagged
)from python_terraform import Terraform, IsFlagged
# Initialize terraform wrapper
tf = Terraform(working_dir='/path/to/terraform/project')
# Initialize terraform project
return_code, stdout, stderr = tf.init()
# Plan the changes
return_code, stdout, stderr = tf.plan()
# Apply the changes
return_code, stdout, stderr = tf.apply()
# Check outputs
outputs = tf.output()
print(outputs)
# Destroy infrastructure when done
return_code, stdout, stderr = tf.destroy()
# Using as context manager (automatically cleans up temp files)
with Terraform(working_dir='/path/to/terraform/project') as tf:
tf.init()
tf.apply()The library follows a straightforward design centered around the main Terraform class:
IsFlagged/IsNotFlagged sentinel values for boolean command-line flags.tfvars.json files for complex variable passingEssential terraform commands including init, plan, apply, and destroy. These form the core infrastructure lifecycle management functionality.
def init(self, dir_or_plan: Optional[str] = None, backend_config: Optional[Dict[str, str]] = None,
reconfigure: Type[TerraformFlag] = IsFlagged, backend: bool = True, **kwargs) -> CommandOutput
def plan(self, dir_or_plan: Optional[str] = None, detailed_exitcode: Type[TerraformFlag] = IsFlagged,
**kwargs) -> CommandOutput
def apply(self, dir_or_plan: Optional[str] = None, input: bool = False, skip_plan: bool = True,
no_color: Type[TerraformFlag] = IsFlagged, **kwargs) -> CommandOutput
def destroy(self, dir_or_plan: Optional[str] = None, force: Type[TerraformFlag] = IsFlagged,
**kwargs) -> CommandOutputTerraform workspace operations for managing multiple environments and configurations within the same terraform project.
def set_workspace(self, workspace: str, *args, **kwargs) -> CommandOutput
def create_workspace(self, workspace: str, *args, **kwargs) -> CommandOutput
def delete_workspace(self, workspace: str, *args, **kwargs) -> CommandOutput
def show_workspace(self, **kwargs) -> CommandOutputGeneric command execution interface and utility functions for building terraform commands and managing execution.
def cmd(self, cmd: str, *args, capture_output: Union[bool, str] = True,
raise_on_error: bool = True, synchronous: bool = True, **kwargs) -> CommandOutput
def generate_cmd_string(self, cmd: str, *args, **kwargs) -> List[str]
def output(self, *args, capture_output: bool = True, **kwargs) -> Union[None, str, Dict[str, str], Dict[str, Dict[str, str]]]Operations for reading and managing terraform state files, including parsing state data and accessing resource information.
def read_state_file(self, file_path: Optional[str] = None) -> NoneState file parsing through the Tfstate class:
@staticmethod
def load_file(file_path: str) -> TfstateThe main Terraform class constructor accepts several parameters for configuring default behavior:
class Terraform:
def __init__(
self,
working_dir: Optional[str] = None,
targets: Optional[Sequence[str]] = None,
state: Optional[str] = None,
variables: Optional[Dict[str, str]] = None,
parallelism: Optional[str] = None,
var_file: Optional[str] = None,
terraform_bin_path: Optional[str] = None,
is_env_vars_included: bool = True,
):
"""
Initialize Terraform wrapper.
Args:
working_dir: Working directory for terraform operations
targets: Default target resources for apply/destroy/plan
state: Path to state file relative to working directory
variables: Default variables for terraform commands
parallelism: Default parallelism value for operations
var_file: Default variable file path(s)
terraform_bin_path: Path to terraform binary (default: "terraform")
is_env_vars_included: Include environment variables in command execution
"""
def __exit__(self, exc_type, exc_value, traceback) -> None:
"""Context manager exit - cleans up temporary variable files"""# Command output tuple type
CommandOutput = Tuple[Optional[int], Optional[str], Optional[str]]
# Flag classes for terraform boolean options
class TerraformFlag:
"""Base class for terraform flag values"""
class IsFlagged(TerraformFlag):
"""Sentinel class indicating a flag should be enabled"""
class IsNotFlagged(TerraformFlag):
"""Sentinel class indicating a flag should be disabled"""
# Custom exception for terraform command failures
class TerraformCommandError(subprocess.CalledProcessError):
def __init__(self, ret_code: int, cmd: str, out: Optional[str], err: Optional[str]): ...
out: Optional[str]
err: Optional[str]
# Variable file manager for complex variable passing
class VariableFiles:
def __init__(self): ...
def create(self, variables: Dict[str, str]) -> str:
"""Create temporary .tfvars.json file and return filename"""
def clean_up(self) -> None:
"""Remove all created temporary files"""
# Module constants
COMMAND_WITH_SUBCOMMANDS = {"workspace"} # Commands that require subcommand handlingBy default, terraform commands that return non-zero exit codes will raise a TerraformCommandError exception. This can be disabled by passing raise_on_error=False to any command method.
try:
return_code, stdout, stderr = tf.apply()
except TerraformCommandError as e:
print(f"Terraform command failed with return code {e.returncode}")
print(f"Error output: {e.err}")