Utility providers: random, time and terraform_data

Intermediate · 10 min

Use the random, time and built-in terraform_data providers to hold values steady, delay a step or trigger replacement, and leave null_resource behind.

Before thisProviders and the registryDependencies, implicit and explicit

Jeff Brown Tech · 28 min · from 17:50

Utility providers: random, time and terraform_data

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by Jeff Brown TechReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • The `random` provider is a logical provider: it generates a value once, stores it in state, and holds it steady until its `keepers` change, so plans stay stable.
  • A `random_password` result is stored in state in plain text. Marking it sensitive only hides it from output.
  • The `time` provider keeps timestamps stable (`time_static`), rotates them (`time_rotating`), or delays a step (`time_sleep`), instead of calling `timestamp()` on every plan.
  • `terraform_data` is built into Terraform since 1.4. It replaces `null_resource` for storing a value and for triggering replacement through `triggers_replace`.
  • `time_sleep` guarantees a delay, not readiness. The provider's own docs call it a workaround.

Randomness in a tool that hates surprises

Terraform’s goal is a plan that converges: run it twice and the second plan is empty. Now you need a unique suffix for a bucket name, a password for a database, a timestamp for a tag, or a ten-second pause between creating a role and using it. Each of those is “a value that changes”, which is exactly what makes plans noisy.

A small group of utility providers exist to make such values behave. They manage no cloud. Each generates something once, keeps it in state, and holds it until you ask for a new one.

random: managed randomness

The provider’s own docs describe random as a logical provider that works entirely inside Terraform and talks to no service. They say the point is managed randomness: resources that generate values when created and hold them steady until their inputs change. Written from the docs and not run:

resource "random_pet" "suffix" {
  length = 2
}

resource "random_password" "db" {
  length  = 24
  special = true
}

resource "aws_s3_bucket" "logs" {
  bucket = "acme-logs-${random_pet.suffix.id}"
}

The first apply picks a name such as two words joined by a dash, and every later plan sees the stored value and changes nothing. That is the difference from calling a random function in an expression, which would show a new value on every plan.

The docs describe the control for it. Each random resource takes a keepers map: “arbitrary key/value pairs that should be selected such that they remain the same until new random values are desired”. When a keeper changes, the resource is recreated and a new value generated. I wrote a small stand-in with the same rule, not Terraform itself:

trigger=v1 -> replaced (new id 153de823)
trigger=v1 -> no change (id 153de823)
trigger=v1 -> no change (id 153de823)
trigger=v2 -> replaced (new id 5c0e6bac)
trigger=v2 -> no change (id 5c0e6bac)

Same input, same value. New input, new value. That is all keepers is. The docs’ own example ties a pet name to an AMI ID, so a new image gets a new server name.

Two warnings from the docs. The results of the provider are not sufficiently random for cryptographic use unless a resource says so, and random_password does say so: the page states it uses a cryptographic random number generator. And the random result is stored in state. The provider docs say generated values are “stored in the Terraform state”, so a random_password sits in the state file in plain text, sensitive or not. The sensitive values lesson goes into that, and its docs note a newer ephemeral variant of random_password for resources that accept write-only arguments, first introduced in Terraform 1.11.

time: stable and delayed time

The time provider has four resources worth knowing. Their descriptions in the docs:

  • time_static keeps a UTC timestamp in state, which “prevents perpetual differences caused by using the timestamp() function”.
  • time_offset does the same for a timestamp offset from a base time.
  • time_rotating proposes recreating the resource when the current time passes a rotation time. The docs note the rotation only happens when Terraform runs.
  • time_sleep delays creation or destruction.

time_sleep earns a warning from its own docs, which call it “a workaround for issues that should be reported and handled in downstream Terraform Provider logic”. It fixes the problem from the dependencies lesson, where a thing exists but is not ready yet:

resource "time_sleep" "wait_for_iam" {
  create_duration = "30s"
  depends_on      = [aws_iam_role_policy_attachment.app]
}

resource "aws_instance" "app" {
  # ...
  depends_on = [time_sleep.wait_for_iam]
}

Written from the docs, not run. It guarantees thirty seconds, not readiness. If thirty is not enough on a bad day, the failure returns. Use it sparingly, and file the bug upstream if you can.

terraform_data: a resource that does nothing

Sometimes you need a node in the graph that stores a value or forces something to be replaced when an input changes, with no real object behind it. For years that was null_resource from the null provider. The null provider’s docs now say: “On Terraform 1.4 and later, use the terraform_data resource type instead.”

terraform_data is built in to Terraform, through a built-in provider at terraform.io/builtin/terraform, so there is nothing to install. Per its docs:

  • input stores a value, and output returns it,
  • triggers_replace stores a value and the resource is replaced when it changes.
resource "terraform_data" "config_version" {
  triggers_replace = [var.config_version]
}

Anything that lists terraform_data.config_version in its replace_triggered_by lifecycle setting can then be rebuilt whenever you bump config_version. The lifecycle group returns to that. The docs also say it is useful for triggering provisioners when there is no better resource to attach them to. A later lesson explains why provisioners are a last resort.

The null docs mention a moved block that can migrate an existing null_resource to terraform_data, on Terraform 1.9 and later. OpenTofu: I did not check its docs for terraform_data, so confirm it before assuming it matches.

Registry numbers

The registry shows how much these small providers are used. The last-published versions I saw on 28 November 2026 were random 3.9.1, time 0.14.2 and null 3.3.2, with roughly 3.7, 1.5 and 2.8 billion downloads respectively. The null provider still gets releases, so old advice to use it is not broken, only superseded.

A rule of thumb

Use a utility provider when a value has to be generated once and kept. Do not use random for anything that must be recoverable, such as a password you will need to type in. State holds it, so you can read it back with terraform output, but the stored copy is the only one. If it is lost, so is the password.

About the video

The embed is “Using Random Provider”, from 17:50 to the end (27:51), of Jeff Brown Tech’s “Terraform for Beginners | Easy Tutorial to Get Started”, November 2025. The first half of the same video is used in two earlier lessons. It covers the random provider only, so time and terraform_data come from the providers’ own docs. The deeper link is a 2022 video on null_resource, which the null provider’s docs now say to replace. English was judged from title, channel and English auto-captions. I judged the content from the chapter list.

Where this fits

That finishes the providers group, and the three groups the first agent owns. The next group goes inside what Terraform has been quietly keeping the whole time: inside the state file.

Check yourself

  1. 01Why not use `timestamp()` to stamp a resource's creation time?

    `timestamp()` changes on every plan, so the resource shows a diff every time. `time_static` records the time once in state and keeps it until its triggers change.

  2. 02You want a database password regenerated only when you bump a `rotation` version variable. How?

    Use `random_password` with `keepers = { rotation = var.rotation }`. When the keeper changes, the resource is recreated and a new password is produced. Otherwise the stored one is reused.

  3. 03A tutorial uses `null_resource` with `triggers`. What does the null provider's own documentation say to use instead on current Terraform?

    `terraform_data`, on Terraform 1.4 and later. Its `triggers_replace` argument replaces `triggers`.

  4. 04Where does a value from `random_pet` live between runs?

    In the state file. The resource generates randomness when created and stores it there, which is why random values do not change on every plan.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark