Search code examples
pythonterraformterraform-cdk

How can I deploy with Terraform cdktf via python script?


I'm already using cdktf for python to make my provider & resources configuration, I can also deploy via cdktf cli tool with cdkft deploy and it works well, but i need to make an deploy or detroy via python script python deploy.py, how can I achive this?

# main.py

from constructs import Construct
from cdktf import App, TerraformStack
from imports.proxmox.provider import ProxmoxProvider
from imports.proxmox.lxc import Lxc
from imports.proxmox.vm_qemu import VmQemu

class MyStack(TerraformStack):
    def __init__(self, scope: Construct, id: str):
        super().__init__(scope, id)

        ProxmoxProvider( self, id="proxmox",
            ...
        )

        Lxc(self, id_="test",
            ...
        )


app = App()
MyStack(app, "terraform_test")
app.synth()

The only option I can think is to execute the commands via os module like this:

# deploy.py
import os
os.system('cdktf deploy')

But I don't know if it's the best approach


Solution

  • The only option I can think is to execute the commands via os module like this:
    

    This is currently the best approach. There is this idea of exposing the API commands in a package that can be consumed across languages, but it's unclear if we are going to proceed with this idea anytime soon or not.