Skip to main content
This is a pre-production deployment of Warehouse. Changes made here affect the production instance of PyPI (
Help us improve Python packaging - Donate today!

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

Release History

This version
History Node


History Node


History Node


Download Files

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

File Name & Hash SHA256 Hash Help Version File Type Upload Date
(3.0 kB) Copy SHA256 Hash SHA256
Source Jan 7, 2018

Supported By

WebFaction WebFaction Technical Writing Elastic Elastic Search Pingdom Pingdom Monitoring Dyn Dyn DNS Sentry Sentry Error Logging CloudAMQP CloudAMQP RabbitMQ Heroku Heroku PaaS Kabu Creative Kabu Creative UX & Design Fastly Fastly CDN DigiCert DigiCert EV Certificate Rackspace Rackspace Cloud Servers DreamHost DreamHost Log Hosting