Skip to main content
Join the official Python Developers Survey 2018 and win valuable prizes: Start the survey!

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

Project description


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.

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 = "${}"

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Filename, size & hash SHA256 hash help File type Python version Upload date
terraform_external_data-0.0.3.tar.gz (3.0 kB) Copy SHA256 hash SHA256 Source None Jan 7, 2018

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page