Skip to content

Instantly share code, notes, and snippets.

@duijf
Created September 27, 2023 10:44
Show Gist options
  • Save duijf/5e1c703fe46239e9cec6480981224387 to your computer and use it in GitHub Desktop.
Save duijf/5e1c703fe46239e9cec6480981224387 to your computer and use it in GitHub Desktop.
Demo: Terraform external data soruce

Demo: Terraform external data source

Prerequisite: Nix or install Terraform by hand.

$ nix run nixpkgs#terraform init
[...]

$ nix run nixpkgs#terraform plan
[...]
Changes to Outputs:
  + foo = {
      + bar = {}
      + foo = [
          + 1,
          + 2,
          + 3,
        ]
    }

You can apply this plan to save these new output values to the Terraform state, without changing any real infrastructure.
data "external" "foo" {
# Outputs `Dict[str, Path]` on stdout. E.g. `{"stuff": "processed-data.json"}`
# You can construct `processed-data.json` however you like: from HTTP APIs,
# databases, or by processing some other configuration file.
#
# This works around a Terraform limitation: external data sources cannot output
# arbitrary JSON. But if they output a file path, we can use `jsondecode()`.
program = ["${path.module}/process.py"]
}
output "foo" {
value = jsondecode(file(data.external.foo.result["stuff"]))
}
#!/usr/bin/env python3
import json
from pathlib import Path
# Can also be in a tempdir, etc.
outfile = Path("processed-data.json")
outfile.write_text(json.dumps({"foo": [1, 2, 3], "bar": {}}))
print(json.dumps({"stuff": str(outfile)}))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment