Package: libterraform
Python libterraformPython binding forTerraform.Installation$pipinstalllibterraformNOTEPlease install version0.3.1or above, which solves the memory leak problem.This library doesnot supportmultithreading.UsageTerraform CLITerraformCommandis used to invoke various Terraform commands.Now, support all commands (plan,apply,destroyetc.), and return aCommandResultobject. TheCommandResultobject has the following properties:retcodeindicates the command return code. A value of 0 or 2 is normal, otherwise is abnormal.valuerepresents command output. Ifjson=Trueis specified when executing the command, the output will be loaded
as json.jsonindicates whether to load the output as json.errorindicates command error output.To get Terraform verison:>>>fromlibterraformimportTerraformCommand>>>TerraformCommand().version()<CommandResultretcode=0json=True>>>>_.value{'terraform_version':'1.2.2','platform':'darwin_arm64','provider_selections':{},'terraform_outdated':False}>>>TerraformCommand().version(json=False)<CommandResultretcode=0json=False>>>>_.value'Terraform v1.2.2\non darwin_arm64\n'Toinitandapplyaccording to Terraform configuration files in the specified directory:>>>fromlibterraformimportTerraformCommand>>>cli=TerraformCommand('your_terraform_configuration_directory')>>>cli.init()<CommandResultretcode=0json=False>>>>cli.apply()<CommandResultretcode=0json=True>Additionally,run()can execute arbitrary commands, returning a tuple(retcode, stdout, stderr).>>>TerraformCommand.run('version')(0,'Terraform v1.2.2\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:\nterraform -help\n\n')Terraform Config ParserTerraformConfigis used to parse Terraform config files.For now, only supplyTerraformConfig.load_config_dirmethod 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*Moduleandhcl.Diagnosticstructures in Terraform
respectively.>>>fromlibterraformimportTerraformConfig>>>mod,_=TerraformConfig.load_config_dir('your_terraform_configuration_directory')>>>mod['ManagedResources'].keys()dict_keys(['time_sleep.wait1','time_sleep.wait2'])Version comparisonlibterraformTerraform0.5.01.3.00.4.01.2.20.3.11.1.7Building & TestingIf you want to develop this library, should first prepare the following environments:GoLang(Version 1.18+)Python(Version 3.7~3.10)GCCThen, initialize git submodule:$gitsubmoduleinit
$gitsubmoduleupdatepip installnecessary tools:$pipinstallpoetrypytestNow, we can build and test:$poetrybuild-fwheel
$pytestWhy 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 thepython-terraformlibrary. 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 adynamic link libraryin 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'sinternal capabilities, such as parsing Terraform config files.