Hello, in this post, I will show something very useful for me in the next steps of the project, which is the iteration of Terraform, calling Python scripts, sending parameters and returning variables.
In another post, this example will be used for Terraform to call pythons that read Blueprints files and return the information ready for resource creation.

I won’t use anything else in the example other than the Python call with completely simple code, so it’s easier to understand how it works and create the complexity you need.

For testing purposes, I will not use K8s, but Docker for ease. But nothing changes.

The example code is at: https://github.com/faustobranco/devops-db/tree/master/knowledge-base/terraform/python

See that in the main file, I call the Python script just below, I have the query parameter with a topic_name variable, this variable will be read in the Python script, as shown below.

main.tf

data "external" "python" {
  program = ["python3", "${path.module}/example.py"]
  query = {
    topic_name = "DevOps-DB Example"
  }
}

Now in the Python file, these variables that are sent from Terraform are handled from sys.stdin.read() as a JSON.
Next, in the main method, I assemble a simple JSON that will be sent as a print to Terraform.

import json
import sys


def main(input_parameter):
    print(json.dumps({ 'var_source': 'python', 'topic': input_parameter }, ensure_ascii=False))

if __name__ == "__main__":
    input = sys.stdin.read()
    input_json = json.loads(input)
    main(input_parameter=input_json.get('topic_name'))

Now back in Terraform, retrieve these values ​​like this:

output "python_source_all" {
  value = data.external.python.result
}

output "python_source_var_source" {
  value = data.external.python.result.var_source
}

output "python_source_topic" {
  value = data.external.python.result.topic
}

For the tests:

docker run --rm -it -v $PWD:/python -w /python ubuntu_terraform:1.0.0 terraform plan
Initializing the backend...
Initializing provider plugins...
- Reusing previous version of hashicorp/external from the dependency lock file
- Using previously-installed hashicorp/external v2.3.4

Terraform has been successfully initialized!

You may now begin working with Terraform. Try running "terraform plan" to see
any changes that are required for your infrastructure. All Terraform commands
should now work.

If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.
data.external.python: Reading...
data.external.python: Read complete after 0s [id=-]

Changes to Outputs:
  + python_source_all        = {
      + topic      = "DevOps-DB Example"
      + var_source = "python"
    }
  + python_source_topic      = "DevOps-DB Example"
  + python_source_var_source = "python"
  + servers                  = [
      + {
          + instance_name = "DevOps-DB Example-python-01"
        },
      + {
          + instance_name = "DevOps-DB Example-python-02"
        },
      + {
          + instance_name = "DevOps-DB Example-python-03"
        },
      + {
          + instance_name = "DevOps-DB Example-python-04"
        },
      + {
          + instance_name = "DevOps-DB Example-python-05"
        },
      + {
          + instance_name = "DevOps-DB Example-python-06"
        },
      + {
          + instance_name = "DevOps-DB Example-python-07"
        },
      + {
          + instance_name = "DevOps-DB Example-python-08"
        },
      + {
          + instance_name = "DevOps-DB Example-python-09"
        },
    ]

You can apply this plan to save these new output values to the Terraform state, without changing any real infrastructure.

───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────

Note: You didn't use the -out option to save this plan, so Terraform can't guarantee to take exactly these actions if you run "terraform apply" now.