python-terraform is a Python module that provides a wrapper around the Terraform command-line interface (CLI) tool. It allows Python programs to execute Terraform commands (e.g., init, plan, apply, destroy) and capture their output, enabling programmatic interaction with infrastructure as code workflows. The library's latest release, 0.14.0, focuses on compatibility with newer Terraform CLI versions. It follows an irregular release cadence, typically releasing new versions to support significant changes in the Terraform CLI.
pip install python-terraformVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to initialize, plan, apply, and destroy a simple Terraform configuration using `python-terraform`. It creates a minimal `main.tf` file and then interacts with it programmatically. `capture_output=True` is used to see the real-time output, and `skip_plan=True` and `force=True` are crucial for non-interactive execution of `apply` and `destroy` respectively.
Ensure your `python-terraform` library version is compatible with your installed Terraform CLI version. Upgrade `python-terraform` to 0.14.0 or newer for Terraform CLI versions 0.14+.
When calling Terraform methods, include `capture_output=True` in the arguments to stream output directly to the console in real-time. Example: `tf.apply(capture_output=True, ...)`.
For `tf.apply()`, use `skip_plan=True`. For `tf.destroy()`, use `force=True`. These flags tell Terraform to proceed without user input. Example: `tf.apply(skip_plan=True)` and `tf.destroy(force=True)`.
Install the Terraform CLI for your operating system and ensure its installation directory is added to your system's PATH. Verify by running `terraform version` in your terminal.
Ensure the `working_dir` points to a valid Terraform root module. Always run `tf.init()` first. Debug the underlying Terraform configuration by manually running `terraform init` and `terraform plan` in the specified directory to identify configuration errors. You can also inspect the `stdout` and `stderr` returned by `tf.init()` for detailed error messages.
Always pass `capture_output=True` to Terraform methods if you need to see real-time output for debugging. Inspect the `stdout` and `stderr` variables returned by the method call for the full error details after execution.
No resource links recorded.