All Projects → beelit94 → Python Terraform

beelit94 / Python Terraform

Licence: mit

Programming Languages

python
139335 projects - #7 most used programming language

Labels

Projects that are alternatives of or similar to Python Terraform

90DaysOfDevOps
This repository is my documenting repository for learning the world of DevOps. I started this journey on the 1st January 2022 and I plan to run to March 31st for a complete 90-day romp on spending an hour a day including weekends to get a foundational knowledge across a lot of different areas that make up DevOps.
Stars: ✭ 1,575 (+439.38%)
Mutual labels:  terraform
Modules.tf Lambda
Infrastructure as code generator - from visual diagrams created with Cloudcraft.co to Terraform
Stars: ✭ 267 (-8.56%)
Mutual labels:  terraform
Cf To Tf
CLI tool for generating Terraform configuration and state for existing CloudFormation resources
Stars: ✭ 283 (-3.08%)
Mutual labels:  terraform
Terraform Aws Eks Cluster
Terraform module for provisioning an EKS cluster
Stars: ✭ 256 (-12.33%)
Mutual labels:  terraform
Tarmak
A toolkit for Kubernetes cluster provisioning and lifecycle management
Stars: ✭ 263 (-9.93%)
Mutual labels:  terraform
Provisioning
Kubernetes cluster provisioning using Terraform.
Stars: ✭ 277 (-5.14%)
Mutual labels:  terraform
terraform-aws-organization-access-group
Terraform module to create an IAM Group and Policy to grant permissions to delegated IAM users in the Organization's master account to access a member account
Stars: ✭ 16 (-94.52%)
Mutual labels:  terraform
Terraform Aws Gitlab Runner
Terraform module for AWS GitLab runners on ec2 (spot) instances
Stars: ✭ 292 (+0%)
Mutual labels:  terraform
Terraform Provider Ansible
"Logical" provider for integrating with an Ansible Dynamic Inventory script.
Stars: ✭ 262 (-10.27%)
Mutual labels:  terraform
Kubenow
Deploy Kubernetes. Now!
Stars: ✭ 285 (-2.4%)
Mutual labels:  terraform
Cloudblock
Cloudblock automates deployment of secure ad-blocking for all of your devices - even when mobile. Step-by-step text and video guides included! Compatible clouds include AWS, Azure, Google Cloud, and Oracle Cloud. Cloudblock deploys Wireguard VPN, Pi-Hole DNS Ad-blocking, and DNS over HTTPS in a cloud provider - or locally - using Terraform and Ansible.
Stars: ✭ 257 (-11.99%)
Mutual labels:  terraform
Terraform Provider Kafka
Terraform provider for managing Apache Kafka Topics + ACLs
Stars: ✭ 256 (-12.33%)
Mutual labels:  terraform
Vim Terraform Completion
A (Neo)Vim Autocompletion and linter for Terraform, a HashiCorp tool
Stars: ✭ 280 (-4.11%)
Mutual labels:  terraform
Electriceye
Continuously monitor your AWS services for configurations that can lead to degradation of confidentiality, integrity or availability. All results will be sent to Security Hub for further aggregation and analysis.
Stars: ✭ 255 (-12.67%)
Mutual labels:  terraform
Terragrunt Infrastructure Live Example
A repo used to show examples file/folder structures you can use with Terragrunt and Terraform
Stars: ✭ 286 (-2.05%)
Mutual labels:  terraform
homelab
My self-hosting infrastructure, fully automated from empty disk to operating services
Stars: ✭ 4,451 (+1424.32%)
Mutual labels:  terraform
Infrastructure As Code Training
Materials for learning how to use infrastructure-as-code
Stars: ✭ 268 (-8.22%)
Mutual labels:  terraform
Terraform Provider Digitalocean
Terraform DigitalOcean provider
Stars: ✭ 296 (+1.37%)
Mutual labels:  terraform
Terraform Ecs Fargate
A Terraform template used for provisioning web application stacks on AWS ECS Fargate
Stars: ✭ 293 (+0.34%)
Mutual labels:  terraform
Iam Policy Json To Terraform
Small tool to convert an IAM Policy in JSON format into a Terraform aws_iam_policy_document
Stars: ✭ 282 (-3.42%)
Mutual labels:  terraform

Introduction

python-terraform is a python module provide a wrapper of terraform command line tool. terraform is a tool made by Hashicorp, please refer to https://terraform.io/

Code style: black pre-commit

Status

Build Status

Installation

pip install python-terraform

Usage

For any terraform command

from python_terraform import *
t = Terraform()
return_code, stdout, stderr = t.<cmd_name>(*arguments, **options)

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

from python_terraform import *
t = Terraform()
return_code, stdout, stderr = t.import_cmd(*arguments, **options)

or just call cmd method directly

from python_terraform import *
t = Terraform()
return_code, stdout, stderr = t.cmd(<cmd_name>, *arguments, **options)

For any argument

simply pass the string to arguments of the method, for example,

terraform apply target_dir
    --> <instance>.apply('target_dir')
terraform import aws_instance.foo i-abcd1234
    --> <instance>.import('aws_instance.foo', 'i-abcd1234')

For any options

  • dash to underscore

    remove first dash, and then use underscore to replace dash symbol as option name

      ex. -no-color --> no_color
    
  • for a simple flag option

    use IsFlagged/None as value for raising/not raising flag, for example,

      terraform taint -allow-missing
         --> <instance>.taint(allow_missing=IsFlagged)
      terraform taint
         --> <instance>.taint(allow_missing=None) or <instance>.taint()
      terraform apply -no-color
         --> <instance>.apply(no_color=IsFlagged)
    
  • for a boolean value option

    assign True or False, for example,

      terraform apply -refresh=true --> <instance>.apply(refresh=True)
    
  • if a flag could be used multiple times, assign a list to it's value

      terraform apply -target=aws_instance.foo[1] -target=aws_instance.foo[2]
      --->
      <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

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.

from python_terraform import Terraform
t = Terraform()
return_code, stdout, stderr = t.<cmd_name>(capture_output=False)

Examples

Have a test.tf file under folder "/home/test"

1. apply with variables a=b, c=d, refresh=false, no color in the output

In shell:

cd /home/test
terraform apply -var='a=b' -var='c=d' -refresh=false -no-color

In python-terraform:

from python_terraform import *
tf = Terraform(working_dir='/home/test')
tf.apply(no_color=IsFlagged, refresh=False, var={'a':'b', 'c':'d'})

or

from python_terraform import *
tf = Terraform()
tf.apply('/home/test', no_color=IsFlagged, refresh=False, var={'a':'b', 'c':'d'})

or

from python_terraform import *
tf = Terraform(working_dir='/home/test', variables={'a':'b', 'c':'d'})
tf.apply(no_color=IsFlagged, refresh=False)

2. fmt command, diff=true

In shell:

cd /home/test
terraform fmt -diff=true

In python-terraform:

from python_terraform import *
tf = terraform(working_dir='/home/test')
tf.fmt(diff=True)

default values

for apply/plan/destroy command, assign with following default value to make caller easier in python

  1. input=False, in this case process won't hang because you missing a variable
  2. no_color=IsFlagged, in this case, stdout of result is easier for parsing

Implementation

IMHO, how terraform design boolean options is confusing. Take input=True and -no-color option of apply command for example, 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 a exhaustive method implementation which I don't prefer to. Therefore I end-up with using IsFlagged or IsNotFlagged as value of option like -no-color and True/False value reserved for option like refresh=true

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].