2023-01-27 12:20:33 +00:00
# dda-python-terraform
[![pipeline status ](https://gitlab.com/domaindrivenarchitecture/dda-python-terraform/badges/master/pipeline.svg )](https://gitlab.com/domaindrivenarchitecture/dda-python-terraform/-/commits/main)
[<img src="https://domaindrivenarchitecture.org/img/delta-chat.svg" width=20 alt="DeltaChat"> chat over e-mail ](mailto:buero@meissa-gmbh.de?subject=community-chat ) | [<img src="https://meissa-gmbh.de/img/community/Mastodon_Logotype.svg" width=20 alt="team@social.meissa-gmbh.de"> team@social.meissa-gmbh.de ](https://social.meissa-gmbh.de/@team ) | [Website & Blog ](https://domaindrivenarchitecture.org )
2015-12-31 06:48:26 +00:00
## Introduction
2022-08-19 13:50:09 +00:00
dda-python-terraform is a python module provide a wrapper of `terraform` command line tool.
2015-12-31 06:48:26 +00:00
`terraform` is a tool made by Hashicorp, please refer to https://terraform.io/
2015-12-31 06:57:23 +00:00
## Installation
2016-11-19 17:11:21 +00:00
pip install python-terraform
2020-10-19 22:30:02 +00:00
2016-11-19 10:24:33 +00:00
## Usage
2017-05-09 07:39:09 +00:00
#### For any terraform command
2016-11-19 10:24:33 +00:00
2022-08-19 13:50:09 +00:00
from dda_python_terraform import *
2016-12-20 10:53:01 +00:00
t = Terraform()
return_code, stdout, stderr = t.< cmd_name > (*arguments, **options)
2017-05-09 07:39:09 +00:00
2017-05-09 08:09:28 +00:00
**Note**: method name same as reserved keyword like `import` won't be accepted by python interpreter,
to be able to call the method, you could call cmd_name by adding `_cmd` after command name, for example,
`import` here could be called by
2017-05-09 07:39:09 +00:00
2022-08-19 13:50:09 +00:00
from dda_python_terraform import *
2017-05-09 07:39:09 +00:00
t = Terraform()
2017-05-09 08:09:28 +00:00
return_code, stdout, stderr = t.import_cmd(*arguments, **options)
2017-05-09 07:39:09 +00:00
or just call cmd method directly
2022-08-19 13:50:09 +00:00
from dda_python_terraform import *
2017-05-09 07:39:09 +00:00
t = Terraform()
return_code, stdout, stderr = t.cmd(< cmd_name > , *arguments, * *options)
2020-10-19 22:30:02 +00:00
2017-05-09 07:39:09 +00:00
#### For any argument
2017-01-03 15:53:31 +00:00
simply pass the string to arguments of the method, for example,
2017-01-03 15:46:38 +00:00
2020-10-19 22:30:02 +00:00
terraform apply target_dir
2017-01-03 15:53:31 +00:00
--> < instance > .apply('target_dir')
2020-10-19 22:30:02 +00:00
terraform import aws_instance.foo i-abcd1234
2017-01-03 15:53:31 +00:00
--> < instance > .import('aws_instance.foo', 'i-abcd1234')
2017-01-03 15:46:38 +00:00
2017-05-09 07:39:09 +00:00
#### For any options
2020-10-19 22:30:02 +00:00
2017-01-03 15:46:38 +00:00
* dash to underscore
remove first dash, and then use underscore to replace dash symbol as option name
2020-10-19 22:30:02 +00:00
2016-12-20 10:53:01 +00:00
ex. -no-color --> no_color
2017-01-03 15:46:38 +00:00
* for a simple flag option
2020-10-19 22:30:02 +00:00
use ```IsFlagged/None``` as value for raising/not raising flag, for example,
terraform taint -allow-missing
2017-01-03 15:46:38 +00:00
--> < instance > .taint(allow_ missing=IsFlagged)
2020-10-19 22:30:02 +00:00
terraform taint
2017-01-03 15:46:38 +00:00
--> < instance > .taint(allow_ missing=None) or < instance > .taint()
terraform apply -no-color
--> < instance > .apply(no_color=IsFlagged)
2020-10-19 22:30:02 +00:00
2017-01-03 15:46:38 +00:00
* for a boolean value option
2020-10-19 22:30:02 +00:00
2017-01-03 15:46:38 +00:00
assign True or False, for example,
2020-10-19 22:30:02 +00:00
2017-01-03 15:46:38 +00:00
terraform apply -refresh=true --> < instance > .apply(refresh=True)
2020-10-19 22:30:02 +00:00
2017-01-03 15:46:38 +00:00
* if a flag could be used multiple times, assign a list to it's value
2020-10-19 22:30:02 +00:00
2017-01-03 15:46:38 +00:00
terraform apply -target=aws_instance.foo[1] -target=aws_instance.foo[2]
2020-10-19 22:30:02 +00:00
--->
2017-01-03 15:46:38 +00:00
< instance > .apply(target=['aws_instance.foo[1]', 'aws_instance.foo[2]'])
* for the "var" flag, assign dictionary to it
terraform apply -var='a=b' -var='c=d'
--> tf.apply(var={'a':'b', 'c':'d'})
* if an option with None as value, it won't be used
2016-11-19 10:24:33 +00:00
2017-05-11 09:21:31 +00:00
#### Terraform Output
By default, stdout and stderr are captured and returned. This causes the application to appear to hang. To print terraform output in real time, provide the `capture_output` option with any value other than `None` . This will cause the output of terraform to be printed to the terminal in real time. The value of `stdout` and `stderr` below will be `None` .
2022-08-19 13:50:09 +00:00
from dda_python_terraform import Terraform
2017-05-11 09:21:31 +00:00
t = Terraform()
return_code, stdout, stderr = t.< cmd_name > (capture_output=False)
2016-11-19 10:24:33 +00:00
## Examples
2020-10-19 22:30:02 +00:00
### Have a test.tf file under folder "/home/test"
2016-12-20 10:53:01 +00:00
#### 1. apply with variables a=b, c=d, refresh=false, no color in the output
2016-11-19 10:24:33 +00:00
In shell:
cd /home/test
terraform apply -var='a=b' -var='c=d' -refresh=false -no-color
2020-10-19 22:30:02 +00:00
2016-11-19 10:24:33 +00:00
In python-terraform:
2022-08-19 13:50:09 +00:00
from dda_python_terraform import *
2016-12-20 10:53:01 +00:00
tf = Terraform(working_dir='/home/test')
2016-11-24 07:09:16 +00:00
tf.apply(no_color=IsFlagged, refresh=False, var={'a':'b', 'c':'d'})
2020-10-19 22:30:02 +00:00
2016-12-20 10:53:01 +00:00
or
2022-08-19 13:50:09 +00:00
from dda_python_terraform import *
2016-12-20 10:53:01 +00:00
tf = Terraform()
tf.apply('/home/test', no_color=IsFlagged, refresh=False, var={'a':'b', 'c':'d'})
2017-01-03 17:02:11 +00:00
or
2022-08-19 13:50:09 +00:00
from dda_python_terraform import *
2017-01-03 17:02:11 +00:00
tf = Terraform(working_dir='/home/test', variables={'a':'b', 'c':'d'})
tf.apply(no_color=IsFlagged, refresh=False)
2020-10-19 22:30:02 +00:00
2016-12-20 10:53:01 +00:00
#### 2. fmt command, diff=true
2016-11-19 10:24:33 +00:00
In shell:
cd /home/test
2020-10-19 22:30:02 +00:00
terraform fmt -diff=true
2016-11-19 10:24:33 +00:00
In python-terraform:
2020-10-19 22:30:02 +00:00
2022-08-19 13:50:09 +00:00
from dda_python_terraform import *
2016-11-19 10:24:33 +00:00
tf = terraform(working_dir='/home/test')
2016-12-20 10:53:01 +00:00
tf.fmt(diff=True)
2017-03-31 18:32:47 +00:00
2020-10-19 22:30:02 +00:00
2017-01-03 17:02:11 +00:00
## default values
2020-10-19 22:30:02 +00:00
for apply/plan/destroy command, assign with following default value to make
2017-01-03 17:02:11 +00:00
caller easier in python
2017-01-04 06:34:00 +00:00
2017-01-03 17:02:11 +00:00
1. ```input=False```, in this case process won't hang because you missing a variable
1. ```no_color=IsFlagged```, in this case, stdout of result is easier for parsing
2017-01-03 15:09:23 +00:00
## Implementation
2020-10-19 22:30:02 +00:00
IMHO, how terraform design boolean options is confusing.
2017-01-03 15:09:23 +00:00
Take `input=True` and `-no-color` option of `apply` command for example,
2020-10-19 22:30:02 +00:00
they're all boolean value but with different option type.
This make api caller don't have a general rule to follow but to do
2017-01-03 15:09:23 +00:00
a exhaustive method implementation which I don't prefer to.
2020-10-19 22:30:02 +00:00
Therefore I end-up with using `IsFlagged` or `IsNotFlagged` as value of option
2017-01-03 17:02:11 +00:00
like `-no-color` and `True/False` value reserved for option like `refresh=true`
2023-01-27 12:20:33 +00:00
## Development & mirrors
Development happens at: https://repo.prod.meissa.de/meissa/dda-python-terraform
Mirrors are:
2023-07-28 12:30:52 +00:00
* https://gitlab.com/domaindrivenarchitecture/dda-python-terraform (CI issues and PR)
2023-01-27 12:20:33 +00:00
* https://github.com/DomainDrivenArchitecture/dda-python-terraform
2023-07-28 12:30:52 +00:00
For more details about our repository model see: https://repo.prod.meissa.de/meissa/federate-your-repos