{"id":14957996,"url":"https://github.com/prodesire/py-libterraform","last_synced_at":"2025-04-10T10:32:29.290Z","repository":{"id":41051198,"uuid":"468260239","full_name":"Prodesire/py-libterraform","owner":"Prodesire","description":"Python binding for Terraform.","archived":false,"fork":false,"pushed_at":"2024-07-17T02:48:57.000Z","size":80,"stargazers_count":46,"open_issues_count":2,"forks_count":9,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-24T09:21:16.731Z","etag":null,"topics":["python","terraform"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Prodesire.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-03-10T08:41:47.000Z","updated_at":"2025-03-21T08:46:57.000Z","dependencies_parsed_at":"2022-09-21T11:11:21.282Z","dependency_job_id":"7c5f4b9e-17be-4ba0-bed4-9e62d5c8ad89","html_url":"https://github.com/Prodesire/py-libterraform","commit_stats":{"total_commits":34,"total_committers":2,"mean_commits":17.0,"dds":0.08823529411764708,"last_synced_commit":"c1a2e79af90245902b013b7f48ad5f4f04b5a23f"},"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Prodesire%2Fpy-libterraform","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Prodesire%2Fpy-libterraform/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Prodesire%2Fpy-libterraform/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Prodesire%2Fpy-libterraform/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Prodesire","download_url":"https://codeload.github.com/Prodesire/py-libterraform/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248199136,"owners_count":21063641,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["python","terraform"],"created_at":"2024-09-24T13:15:57.291Z","updated_at":"2025-04-10T10:32:29.262Z","avatar_url":"https://github.com/Prodesire.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Python libterraform\n\n[![libterraform](https://img.shields.io/pypi/v/libterraform.svg)](https://pypi.python.org/pypi/libterraform)\n[![libterraform](https://img.shields.io/pypi/l/libterraform.svg)](https://pypi.python.org/pypi/libterraform)\n[![libterraform](https://img.shields.io/pypi/pyversions/libterraform.svg)](https://pypi.python.org/pypi/libterraform)\n[![Test](https://github.com/Prodesire/py-libterraform/actions/workflows/test.yml/badge.svg)](https://github.com/Prodesire/py-libterraform/actions/workflows/test.yml)\n[![libterraform](https://img.shields.io/pypi/dm/libterraform)](https://pypi.python.org/pypi/libterraform)\n\nPython binding for [Terraform](https://www.terraform.io/).\n\n## Installation\n\n```bash\n$ pip install libterraform\n```\n\n\u003e **NOTE**\n\u003e - Please install version **0.3.1** or above, which solves the memory leak problem.\n\u003e - This library does **not support** multithreading.\n\n## Usage\n\n### Terraform CLI\n\n`TerraformCommand` is used to invoke various Terraform commands.\n\nNow, support all commands (`plan`, `apply`, `destroy` etc.), and return a `CommandResult` object. The `CommandResult`\nobject has the following properties:\n\n- `retcode` indicates the command return code. A value of 0 or 2 is normal, otherwise is abnormal.\n- `value` represents command output. If `json=True` is specified when executing the command, the output will be loaded\n  as json.\n- `json` indicates whether to load the output as json.\n- `error` indicates command error output.\n\nTo get Terraform verison:\n\n```python\n\u003e\u003e\u003e from libterraform import TerraformCommand\n\u003e\u003e\u003e TerraformCommand().version()\n\u003cCommandResult retcode=0 json=True\u003e\n\u003e\u003e\u003e _.value\n{'terraform_version': '1.8.4', 'platform': 'darwin_arm64', 'provider_selections': {}, 'terraform_outdated': True}\n\u003e\u003e\u003e TerraformCommand().version(json=False)\n\u003cCommandResult retcode=0 json=False\u003e\n\u003e\u003e\u003e _.value\n'Terraform v1.8.4\\non darwin_arm64\\n'\n```\n\nTo `init` and `apply` according to Terraform configuration files in the specified directory:\n\n```python\n\u003e\u003e\u003e from libterraform import TerraformCommand\n\u003e\u003e\u003e cli = TerraformCommand('your_terraform_configuration_directory')\n\u003e\u003e\u003e cli.init()\n\u003cCommandResult retcode=0 json=False\u003e\n\u003e\u003e\u003e cli.apply()\n\u003cCommandResult retcode=0 json=True\u003e\n```\n\nAdditionally, `run()` can execute arbitrary commands, returning a tuple `(retcode, stdout, stderr)`.\n\n```python\n\u003e\u003e\u003e TerraformCommand.run('version')\n(0, 'Terraform v1.8.4\\non darwin_arm64\\n', '')\n\u003e\u003e\u003e TerraformCommand.run('invalid')\n(1, '', 'Terraform has no command named \"invalid\".\\n\\nTo see all of Terraform\\'s top-level commands, run:\\n  terraform -help\\n\\n')\n```\n\n### Terraform Config Parser\n\n`TerraformConfig` is used to parse Terraform config files.\n\nFor now, only supply `TerraformConfig.load_config_dir` method which reads the .tf and .tf.json files in the given\ndirectory as config files and then combines these files into a single Module. This method returns `(mod, diags)`\nwhich are both dict, corresponding to\nthe [*Module](https://github.com/hashicorp/terraform/blob/2a5420cb9acf8d5f058ad077dade80214486f1c4/internal/configs/module.go#L14)\nand [hcl.Diagnostic](https://github.com/hashicorp/hcl/blob/v2.11.1/diagnostic.go#L26) structures in Terraform\nrespectively.\n\n```python\n\u003e\u003e\u003e from libterraform import TerraformConfig\n\u003e\u003e\u003e mod, _ = TerraformConfig.load_config_dir('your_terraform_configuration_directory')\n\u003e\u003e\u003e mod['ManagedResources'].keys()\ndict_keys(['time_sleep.wait1', 'time_sleep.wait2'])\n```\n\n## Version comparison\n\n| libterraform                                          | Terraform                                                   |\n|-------------------------------------------------------|-------------------------------------------------------------|\n| [0.8.0](https://pypi.org/project/libterraform/0.8.0/) | [1.8.4](https://github.com/hashicorp/terraform/tree/v1.8.4) |\n| [0.7.0](https://pypi.org/project/libterraform/0.7.0/) | [1.6.6](https://github.com/hashicorp/terraform/tree/v1.6.6) |\n| [0.6.0](https://pypi.org/project/libterraform/0.6.0/) | [1.5.7](https://github.com/hashicorp/terraform/tree/v1.5.7) |\n| [0.5.0](https://pypi.org/project/libterraform/0.5.0/) | [1.3.0](https://github.com/hashicorp/terraform/tree/v1.3.0) |\n| [0.4.0](https://pypi.org/project/libterraform/0.4.0/) | [1.2.2](https://github.com/hashicorp/terraform/tree/v1.2.2) |\n| [0.3.1](https://pypi.org/project/libterraform/0.3.1/) | [1.1.7](https://github.com/hashicorp/terraform/tree/v1.1.7) |\n\n## Building \u0026 Testing\n\nIf you want to develop this library, should first prepare the following environments:\n\n- [GoLang](https://go.dev/dl/) (Version 1.21.5+)\n- [Python](https://www.python.org/downloads/) (Version 3.7~3.12)\n- GCC\n\nThen, initialize git submodule:\n\n```bash\n$ git submodule init\n$ git submodule update\n```\n\n`pip install` necessary tools:\n\n```bash\n$ pip install poetry pytest\n```\n\nNow, we can build and test:\n\n```bash\n$ poetry build -f wheel\n$ pytest\n```\n\n## Why use this library?\n\nTerraform is a great tool for deploying resources. If you need to call the Terraform command in the Python program for\ndeployment, a new process needs to be created to execute the Terraform command on the system. A typical example of this\nis the [python-terraform](https://github.com/beelit94/python-terraform) library. Doing so has the following problems:\n\n- Requires Terraform commands on the system.\n- The overhead of starting a new process is relatively high.\n\nThis library compiles Terraform as a **dynamic link library** in advance, and then loads it for calling. So there is no\nneed to install Terraform, nor to start a new process.\n\nIn addition, since the Terraform dynamic link library is loaded, this library can further call Terraform's\n**internal capabilities**, such as parsing Terraform config files.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fprodesire%2Fpy-libterraform","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fprodesire%2Fpy-libterraform","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fprodesire%2Fpy-libterraform/lists"}