terrapy is a lightweight CLI tool that automates the generation, validation, and application of Terraform templates using Python. It integrates seamlessly with the Terraform CLI and provides a programmable interface to work with infrastructure-as-code.
- 🔧 Render Terraform configuration files from templates
- ✅ Automatically validate rendered configurations
- 📈 Visualize Terraform resource graphs
- 🚀 Apply infrastructure changes via Terraform CLI
- 🧩 CLI integration using frameworks like Click or Typer
Make sure you have the following installed:
- Python 3.7+
- Terraform CLI
graphviz
and its Python bindings:sudo apt install graphviz # Linux brew install graphviz # macOS pip install graphviz
To render and validate a Terraform module, use the templates
command. You can specify the module name and optionally provide a path to the variable file (terrapy.vars
by default):
python main.py templates <module-name> [--vars <path-to-vars-file>]
To apply a Terraform module (e.g., aws_s3_bucket
), use the apply
command:
python main.py apply <module-name>
Each macro file should define one or more Jinja2 macros.
Example: macros/greet.j2
{# hello.j2 #}
{% macro hello(name) %}
Hello, {{ name }}!
{% endmacro %}
And then, you can use it as shown below in the jinja2 templates.
macros['hello'].hello("Alice")