Using External Data and Templates
Pull dynamic values into Terraform with the external data source and render configuration files using the templatefile function.
Using External Data and Templates is a free DevOps Bootcamp lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the DevOps Bootcamp learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Beyond Built-in Data Sources
You already query AWS with data sources. Sometimes the data you need lives outside any provider, like the output of a shell script or an internal API. The external data source and template functions fill this gap.
The external Data Source
The external data source runs a program and reads JSON from its stdout. It is an escape hatch for logic Terraform cannot express natively.
data "external" "git_sha" {
program = ["bash", "./scripts/git_sha.sh"]
}The Program Contract
The external program receives a JSON object on stdin and must print a flat JSON object of string values to stdout. Anything else is an error.
#!/bin/bash
echo '{"sha": "abc123"}'Reading External Results
Access returned values through the result map. Every value is a string, so cast if you need another type.
resource "aws_instance" "app" {
ami = "ami-0abcd1234"
instance_type = "t3.micro"
tags = {
GitSha = data.external.git_sha.result.sha
}
}When to Avoid external
The external source runs on every plan and breaks reproducibility if the script is non-deterministic. Prefer a real provider data source when one exists; reserve external for genuine gaps.
Rendering Files with templatefile
The templatefile function reads a template, substitutes variables, and returns a string. It is perfect for cloud-init user data and config files.
resource "aws_instance" "web" {
ami = "ami-0abcd1234"
instance_type = "t3.micro"
user_data = templatefile("init.tftpl", {
hostname = "web-01"
})
}Template Syntax
Inside a .tftpl file, interpolate values with the dollar-brace syntax. Terraform substitutes the passed variables.
#!/bin/bash
hostnamectl set-hostname ${hostname}
echo 'Provisioned by Terraform'Loops in Templates
Templates support directives for iteration. A for directive renders repeated lines, handy for generating lists of hosts.
%{ for ip in ips ~}
server ${ip}:8080
%{ endfor ~}Inline Templates with templatestring
For short snippets you can skip a file and use templatestring (or older format) to build dynamic strings directly in config.
locals {
greeting = templatestring("Hello, $${name}!", { name = "Ops" })
}Combining the Two
A common pattern: an external source fetches a dynamic value, then templatefile injects it into a config rendered as user data.
user_data = templatefile("app.tftpl", {
version = data.external.git_sha.result.sha
})Keeping Renders Idempotent
Because user_data changes force instance replacement, keep template inputs stable. Feeding a constantly-changing value (like a timestamp) will recreate instances on every apply.
Quick Check
Test your understanding of the external data source.
Recap: Dynamic Inputs and Templates
You extended Terraform's data capabilities:
- The
externalsource runs scripts that return JSON. templatefilerenders config and user-data files.- Template directives support loops and interpolation.
- Keep inputs stable to avoid unwanted replacements.
Frequently asked questions
Is the “Using External Data and Templates” lesson free?
Yes — the full text of “Using External Data and Templates” is free to read here on the web, and the DevOps Bootcamp course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the DevOps Bootcamp course, upgrade to CoddyKit PRO.
What will I learn in “Using External Data and Templates”?
Pull dynamic values into Terraform with the external data source and render configuration files using the templatefile function. You practise DevOps Bootcamp with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start DevOps Bootcamp?
No prior experience is required. DevOps Bootcamp on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Using External Data and Templates” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this DevOps Bootcamp lesson?
Yes. Every DevOps Bootcamp lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Querying Data with Data Sources
- Local and Remote Exec Provisioners
- Null Resources and Triggers
- Using External Data and Templates