Provides a decorator that implements terraform's external program protocol for data sources.
Switch branches/tags
Clone or download
Latest commit f9eeb29 Oct 23, 2018



Provides a decorator that implements terraform's external program protocol for data sources.

  • Reads in JSON from stdin.
  • Validates input.
  • Outputs JSON to stdout.
  • Validates output.
  • Prints human-readable, one-line errors on stderr.
  • Returns a non-zero status code on errors.

This saves you from fiddling with the spec and makes it easy to write several external data scripts without duplicating code.

The wrapped function must expect its first positional argument to be a dictionary of the query data.

See the contributing guide for instructions on developing and running tests.

Example Usage

  1. As always, create and activate a venv (Python 3) or virtualenv (Python 2).

    python3 -m venv env3
    source env3/bin/activate
  2. Install terraform_external_data in the env.

    pip install terraform_external_data
  3. Write a script with a data collection function decorated by terraform_external_data (the @ syntax below). Your function must take at least one argument, the query data passed in by terraform. For example,

    from terraform_external_data import terraform_external_data
    def get_cool_data(query):
        Dummy function that simulates data collection with a count.
        Here you could reach out to an API, inspect local files, etc.
        count = 0
        for i in range(3):
            count += 1
        # Terraform requires the values you return be strings,
        # so terraform_external_data will error if they aren't.
        return {query['thing_to_count']: str(count)}
    if __name__ == '__main__':
        # Always protect Python scripts from import side effects with
        # a condition to check the __name__. Not specifically necessary
        # for terraform_external_data, but it's a best practice in general.
  4. Add a data resource to your terraform file. For example,

    data "external" "cars_count" {
      program = ["python", "${path.module}/"]
      query = {
        # This is the query data your function will receive as an argument.
        thing_to_count = "cars"
    # Reference the data like any terraform var. This example uses an
    # output so it doesn't modify infrastructure.
    output "cars_count" {
      value = "${}"