Python binding for Terraform.

Overview

Python libterraform

libterraform libterraform libterraform Test libterraform

Python binding for Terraform.

Installation

$ pip install libterraform

NOTE

  • Please install version 0.3.1 or above, which solves the memory leak problem.
  • This library does not support multithreading.

Usage

Terraform CLI

TerraformCommand is used to invoke various Terraform commands.

Now, support all commands (plan, apply, destroy etc.), and return a CommandResult object. The CommandResult object has the following properties:

  • retcode indicates the command return code. A value of 0 or 2 is normal, otherwise is abnormal.
  • value represents command output. If json=True is specified when executing the command, the output will be loaded as json.
  • json indicates whether to load the output as json.
  • error indicates command error output.

To get Terraform verison:

>>> from libterraform import TerraformCommand
>>> TerraformCommand().version()
<CommandResult retcode=0 json=True>
>>> _.value
{'terraform_version': '1.1.7', 'platform': 'darwin_arm64', 'provider_selections': {}, 'terraform_outdated': False}
>>> TerraformCommand().version(json=False)
<CommandResult retcode=0 json=False>
>>> _.value
'Terraform v1.1.7\non darwin_arm64\n'

To init and apply according to Terraform configuration files in the specified directory:

>>> from libterraform import TerraformCommand
>>> cli = TerraformCommand('your_terraform_configuration_directory')
>>> cli.init()
<CommandResult retcode=0 json=False>
>>> cli.apply()
<CommandResult retcode=0 json=True>

Additionally, run() can execute arbitrary commands, returning a tuple (retcode, stdout, stderr).

>>> TerraformCommand.run('version')
(0, 'Terraform v1.1.7\non darwin_arm64\n', '')
>>> TerraformCommand.run('invalid')
(1, '', 'Terraform has no command named "invalid".\n\nTo see all of Terraform\'s top-level commands, run:\n  terraform -help\n\n')

Terraform Config Parser

TerraformConfig is used to parse Terraform config files.

For now, only supply TerraformConfig.load_config_dir method which reads the .tf and .tf.json files in the given directory as config files and then combines these files into a single Module. This method returns (mod, diags) which are both dict, corresponding to the *Module and hcl.Diagnostic structures in Terraform respectively.

>>> from libterraform import TerraformConfig
>>> mod, _ = TerraformConfig.load_config_dir('your_terraform_configuration_directory')
>>> mod['ManagedResources'].keys()
dict_keys(['time_sleep.wait1', 'time_sleep.wait2'])

Building & Testing

If you want to develop this library, should first prepare the following environments:

  • GoLang (Version 1.17.x or 1.16.x)
  • Python (Version 3.6~3.10)
  • GCC

Then, initialize git submodule:

$ git submodule init
$ git submodule update

pip install necessary tools:

$ pip install poetry pytest

Now, we can build and test:

$ poetry build -f wheel
$ pytest

Why use this library?

Terraform is a great tool for deploying resources. If you need to call the Terraform command in the Python program for deployment, a new process needs to be created to execute the Terraform command on the system. A typical example of this is the python-terraform library. Doing so has the following problems:

  • Requires Terraform commands on the system.
  • The overhead of starting a new process is relatively high.

This library compiles Terraform as a dynamic link library in advance, and then loads it for calling. So there is no need to install Terraform, nor to start a new process.

In addition, since the Terraform dynamic link library is loaded, this library can further call Terraform's internal capabilities, such as parsing Terraform config files.

You might also like...
PRAW, an acronym for "Python Reddit API Wrapper", is a python package that allows for simple access to Reddit's API.

PRAW: The Python Reddit API Wrapper PRAW, an acronym for "Python Reddit API Wrapper", is a Python package that allows for simple access to Reddit's AP

PRAW, an acronym for "Python Reddit API Wrapper", is a python package that allows for simple access to Reddit's API.

PRAW: The Python Reddit API Wrapper PRAW, an acronym for "Python Reddit API Wrapper", is a Python package that allows for simple access to Reddit's AP

alpaca-trade-api-python is a python library for the Alpaca Commission Free Trading API.

alpaca-trade-api-python is a python library for the Alpaca Commission Free Trading API. It allows rapid trading algo development easily, with support for both REST and streaming data interfaces

🖥️ Python - P1 Monitor API Asynchronous Python Client

🖥️ Asynchronous Python client for the P1 Monitor

Volt is yet another discord api wrapper for Python. It supports python 3.8 +

Volt Volt is yet another discord api wrapper for Python. It supports python 3.8 + How to install [Currently Not Supported.] pip install volt.py Speed

Bagas Mirror&Leech Bot is a multipurpose Telegram Bot written in Python for mirroring files on the Internet to our beloved Google Drive. Based on python-aria-mirror-bot
Bagas Mirror&Leech Bot is a multipurpose Telegram Bot written in Python for mirroring files on the Internet to our beloved Google Drive. Based on python-aria-mirror-bot

- [ MAYBE UPDATE & ADD MORE MODULE ] Bagas Mirror&Leech Bot Bagas Mirror&Leech Bot is a multipurpose Telegram Bot written in Python for mirroring file

A python Discord wrapper made in well, python.

discord.why A python Discord wrapper made in well, python. Made to be used by devs who want something a bit more, general. Basic Examples Sending a me

A wrapper for aqquiring Choice Coin directly through a Python Terminal. Leverages the TinyMan Python-SDK.

CHOICE_TinyMan_Wrapper A wrapper that allows users to acquire Choice Coin directly through their Terminal using ALGO and various Algorand Standard Ass

Python On WhatsApp - Run your python codes on whatsapp along with talking to a chatbot
Python On WhatsApp - Run your python codes on whatsapp along with talking to a chatbot

Python On WhatsApp Run your python codes on whatsapp along with talking to a chatbot This is a small python project to run python on whatsapp. and i c

Comments
  • Could not find a version that satisfies the requirement libterraform

    Could not find a version that satisfies the requirement libterraform

    $ pip install libterraform ERROR: Could not find a version that satisfies the requirement libterraform (from versions: none) ERROR: No matching distribution found for libterraform $

    opened by judab5ericom 0
  • ERROR: Could not build wheels for libterraform which use PEP 517 and cannot be installed directly

    ERROR: Could not build wheels for libterraform which use PEP 517 and cannot be installed directly

    getting the following error when trying to compile local Installing build dependencies ... done Getting requirements to build wheel ... done Preparing wheel metadata ... done /home/judab/.local/lib/python3.8/site-packages/pkg_resources/init.py:123: PkgResourcesDeprecationWarning: 0.23ubuntu1 is an invalid version and will not be supported in a future release warnings.warn( /home/judab/.local/lib/python3.8/site-packages/pkg_resources/init.py:123: PkgResourcesDeprecationWarning: 0.1.36ubuntu1 is an invalid version and will not be supported in a future release warnings.warn( Building wheels for collected packages: libterraform Building wheel for libterraform (PEP 517) ... error ERROR: Command errored out with exit status 1: command: /usr/bin/python3 /tmp/tmpjxsbgvg0 build_wheel /tmp/tmpmuv6vjmq cwd: /tmp/pip-req-build-4kwbn1bw Complete output (33 lines):

    sigs.k8s.io/json/internal/golang/encoding/json

    /home/judab/go/pkg/mod/sigs.k8s.io/[email protected]/internal/golang/encoding/json/encode.go:1249:12: sf.IsExported undefined (type reflect.StructField has no field or method IsExported) /home/judab/go/pkg/mod/sigs.k8s.io/[email protected]/internal/golang/encoding/json/encode.go:1255:18: sf.IsExported undefined (type reflect.StructField has no field or method IsExported) - Patching go-plugin package - Building libterraform Traceback (most recent call last): File "build.py", line 72, in build({}) File "build.py", line 54, in build subprocess.check_call( File "/usr/lib/python3.8/subprocess.py", line 364, in check_call raise CalledProcessError(retcode, cmd) subprocess.CalledProcessError: Command '['go', 'build', '-buildmode=c-shared', '-o=libterraform.so', 'github.com/hashicorp/terraform']' returned non-zero exit status 2. Traceback (most recent call last): File "/tmp/tmpjxsbgvg0", line 280, in main() File "/tmp/tmpjxsbgvg0", line 263, in main json_out['return_val'] = hook(**hook_input['kwargs']) File "/tmp/tmpjxsbgvg0", line 204, in build_wheel return _build_backend().build_wheel(wheel_directory, config_settings, File "/tmp/pip-build-env-e48g7589/overlay/lib/python3.8/site-packages/poetry/core/masonry/api.py", line 68, in build_wheel return unicode(WheelBuilder.make_in(poetry, Path(wheel_directory))) File "/tmp/pip-build-env-e48g7589/overlay/lib/python3.8/site-packages/poetry/core/masonry/builders/wheel.py", line 78, in make_in wb.build() File "/tmp/pip-build-env-e48g7589/overlay/lib/python3.8/site-packages/poetry/core/masonry/builders/wheel.py", line 106, in build self._build(zip_file) File "/tmp/pip-build-env-e48g7589/overlay/lib/python3.8/site-packages/poetry/core/masonry/builders/wheel.py", line 152, in _build self._run_build_script(self._package.build_script) File "/tmp/pip-build-env-e48g7589/overlay/lib/python3.8/site-packages/poetry/core/masonry/builders/wheel.py", line 202, in _run_build_script subprocess.check_call([self.executable.as_posix(), build_script]) File "/usr/lib/python3.8/subprocess.py", line 364, in check_call raise CalledProcessError(retcode, cmd) subprocess.CalledProcessError: Command '['/usr/bin/python3', 'build.py']' returned non-zero exit status 1.

    ERROR: Failed building wheel for libterraform Failed to build libterraform ERROR: Could not build wheels for libterraform which use PEP 517 and cannot be installed directly

    opened by judab5ericom 0
Releases(v0.4.0)
  • v0.3.1(Apr 19, 2022)

  • v0.3.0(Apr 6, 2022)

    TerraformCommand supports all Terraform commands. Compared with v0.2.1, the current version supports the following commands:

    • fmt
    • force unlock (The corresponding function is force_unlock)
    • graph
    • import (The corresponding function is import_resource)
    • refresh
    • state and all sub commands of state
    • taint
    • untaint
    • test
    • workspace and all sub commands of workspace

    The project adds Makefile so we can use make command, like make init, make test, make build etc.

    Source code(tar.gz)
    Source code(zip)
  • v0.2.1(Mar 31, 2022)

    Implement TerraformCommand which is used to invoke various Terraform commands.

    Currently, supports version and all main commands (init, validate, plan, show, apply and destroy), returning a CommandResult object. The CommandResult object has the following properties:

    • retcode indicates the command return code. A value of 0 or 2 is normal, otherwise is abnormal.
    • value represents command output. If json=True is specified when executing the command, the output will be loaded as json.
    • json indicates whether to load the output as json.
    • error indicates command error output.
    Source code(tar.gz)
    Source code(zip)
  • v0.1.0(Mar 11, 2022)

    Implement TerraformConfig which is used to parse Terraform config files.

    For now, only supply TerraformConfig.load_config_dir method which reads the .tf and .tf.json files in the given directory as config files and then combines these files into a single Module. This method returns (mod, diags) which are both dict, corresponding to the *Module and hcl.Diagnostic structures in Terraform respectively.

    Source code(tar.gz)
    Source code(zip)
Owner
Prodesire
Pythonista. Microsoft MVP.
Prodesire
Stop writing scripts to interact with your APIs. Call them as CLIs instead.

Zum Stop writing scripts to interact with your APIs. Call them as CLIs instead. Zum (German word roughly meaning "to the" or "to" depending on the con

Daniel Leal 84 Nov 17, 2022
A simple Facebook Account generator, written in python (needs different Email so Accounts do not get banned)

FacebookAccountGenerator FAB is a Facebook-Account generating script, written in python Installation Use the package manager pip to install selenium p

MrOverload 7 Jan 05, 2023
A Telegram bot to extracting text from images. All languages supported.

OCR Bot A Telegram bot to extracting text from images. All languages supported. Deploy to Heroku Local Deploying Clone the repo git clone https://gith

6 Oct 21, 2022
arweave-nft-uploader is a Python tool to improve the experience of uploading NFTs to the Arweave storage for use with the Metaplex Candy Machine.

arweave-nft-uploader arweave-nft-uploader is a Python tool to improve the experience of uploading NFTs to the Arweave storage for use with the Metaple

0xEnrico 84 Dec 26, 2022
🔏 Discordちゃんねる ◆wGFzKUzY7E

使い方 discord.pyをインストール. python -m pip install -r requirements.txtを実行. bot.pyと同じ階層に.tokenを用意. bot.pyを実行. ※現状、使用しているライブラリの関係でWindowsOSは未対応です。 コマンド ニックネーム

Gattxxa 3 Feb 02, 2022
A simple library for interacting with Amazon SQS.

qoo is a very simple Amazon SQS client, written in Python. It aims to be much more straight-forward to use than boto3, and specializes only in Amazon

Jacobi Petrucciani 2 Oct 30, 2020
An API that allows you to get full information about TikTok videos

TikTok-API An API that allows you to get full information about TikTok videos without using any third party sources and only the TikTok API. ##API onl

FC 13 Dec 20, 2021
Python Markov Chain chatbot running on Telegram

Hanasubot Hanasubot (Japanese 話すボット, talking bot) is a Python chatbot running on Telegram. The bot is based on Markov Chains so it can learn your word

12 Dec 27, 2022
Shiny Wechat Pay SDK for Python

WeChat third-party Python SDK master: Read the Documentation Features Common public platforms passively respond and actively call APIs WeChat Pay API

Obrisk 18 Sep 05, 2022
Simple Discord bot for the Collectez community.

Harvey - Discord Bot Simple Discord bot for the Collectez community. Features Ping the current status of Collectez's Teztools node. Steal emojis from

delintkhaum 1 Dec 26, 2021
This script books automatically a slot on Doctolib in one of the public vaccination centers in Berlin.

BOOKING IN BERLINS VACCINATION CENTERS This python script books automatically a slot on Doctolib in one of the public vaccination centers in Berlin. T

17 Jan 13, 2022
Generate Heroku-like random names to use in your python applications

HaikunatorPY Generate Heroku-like random names to use in your python applications. Installation pip install haikunator Usage Haikunator is pretty sim

Atrox 116 Nov 15, 2022
A curated list of awesome Amazon Web Services (AWS) libraries, open source repos, guides, blogs, and other resources.

A curated list of awesome Amazon Web Services (AWS) libraries, open source repos, guides, blogs, and other resources. Featuring the Fiery Meter of AWSome.

Donne Martin 11.1k Jan 04, 2023
An automated tool that fetches information about your crypto stake and generates historical data in time.

Introduction Yield explorer is a WIP! I needed a tool that would show me historical data and performance of my staked crypto but was unable to find a

Sedat Can Yalçın 42 Nov 26, 2022
Start multiple bots using one script. VK RAID BOTNET

MultiRaidBotnet Start multiple bots using one script. VK RAID BOTNET Русский launcher.py - главный скрипт, запускающий весь ботнет config.py - в нём х

2 Jul 22, 2022
Demo of using Telegram to send alert message

MIAI_Telegram Demo of using Telegram to send alert message Video link: https://youtu.be/oZ9CsIrlMgg #MìAI Fanpage: http://facebook.com/miaiblog Group

4 Jun 20, 2021
A Python script to update Spotify Playlist data every 5 minutes.

Spotify Playlist Updater A Python script to update Spotify Playlist data every 5 minutes. Description An automatic playlist updater using Spotify API

6 Nov 24, 2022
a discord bot for searching your movies, and bot return movie url for you :)

IMDb Discord Bot how to run this bot. the first step you must create prefixes.json file the second step you must create a virtualenv if you use window

Mehdi Radfar 6 Dec 20, 2022
Forward Propagation, Backward Regression and Pose Association for Hand Tracking in the Wild (CVPR 2022)

HandLer This repository contains the code and data for the following paper: Forward Propagation, Backward Regression, and Pose Association for Hand Tr

<a href=[email protected]"> 17 Oct 02, 2022
A discord.py bot template with easy deployment through Github Actions

discord.py bot template A discord.py bot template with easy deployment through Github Actions. You can use this template to just run a Python instance

Thomas Van Iseghem 1 Feb 09, 2022