Join our Newsletter — 33% off our NHI Course
Home› FAQ› Cyber Security› What is the difference between a Terraform resource…
Cyber Security

What is the difference between a Terraform resource and a Terraform data source?

← Back to all FAQ
By NHI Mgmt Group Editorial Team Updated October 30, 2026 Domain: Cyber Security

A Terraform resource creates, updates, or deletes infrastructure objects that Terraform manages directly. A data source only reads existing information and exposes it for use elsewhere in the configuration. In practice, resources define desired state, while data sources supply context such as IDs, attributes, or remote values that help other resources be configured correctly.

How Terraform resources and data sources differ in stateful configuration

A Terraform resource is the part of configuration that declares and manages an object Terraform is responsible for creating or changing. A data source is read-only: it looks up something that already exists and makes its attributes available to the rest of the configuration. That distinction matters because it separates desired state from external context.

Resources belong in the control plane of your configuration. They describe what Terraform should bring into existence, modify, or remove, so the plan can converge infrastructure toward the declared end state. Data sources sit on the lookup side of that model, pulling in IDs, metadata, or remote attributes without claiming ownership of the object being read.

The practical difference shows up in how each one behaves during planning and apply. A resource can create drift if the real object changes outside Terraform, because Terraform expects to manage the object lifecycle. A data source does not correct drift on its own, but it can still influence downstream changes by feeding live values into resources that do get managed.

When to use each one in real Terraform work

Use a resource when Terraform should be the system of record for the object. That includes things like networks, instances, policies, roles, or any object whose lifecycle you want encoded and reviewed as code. Use a data source when the object exists elsewhere, is owned by another team or system, or should be discovered dynamically rather than declared.

That distinction is especially important when you need a stable reference into shared infrastructure. For example, a module may need the ID of an existing VPC, subnet, image, or secret store. A data source gives you that input without asking Terraform to recreate the object, which helps avoid accidental takeover of shared assets.

In larger environments, this boundary also improves module design. Resources define what a module owns. Data sources define what it depends on. If you blur that line, plans become harder to reason about, and teams can misread a lookup as ownership or an owned object as a lookup.

How to avoid misusing resources and data sources

The most common mistake is using a resource for something that should be referenced, or using a data source for something that should be managed. The first creates ownership where none was intended. The second creates a false sense of control, because Terraform can consume a value from the environment without enforcing that the upstream object remains valid or unchanged.

Another common failure mode is assuming a data source is always harmless. It is read-only in Terraform, but the values it returns can still drive replacement, privilege changes, or environment-specific behaviour in downstream resources. That makes the lookup path operationally important even when it does not appear in the apply diff as a managed object.

For IaC hygiene, keep module interfaces explicit: owned objects should be resources, imported or shared objects should be data sources, and the difference should be obvious in code review. The Infrastructure as Code Security Guide covers Terraform state, drift, and module governance in more depth, which is where this distinction usually becomes material in practice.

Standards & Framework Alignment

This section maps relevant standards and security frameworks to the operational risks and controls described in this guidance.

NIST SP 800-53 Rev 5 sets the technical controls, while ISO/IEC 27001:2022 defines the regulatory obligations.

FrameworkControl / ReferenceRelevance
NIST SP 800-53 Rev 5CM-8 — System Component InventoryTerraform data sources depend on accurate knowledge of existing components.
CM-2 — Baseline ConfigurationTerraform resources define the managed baseline for infrastructure objects.
AC-6 — Least PrivilegeTerraform modules should only manage or read infrastructure they are authorized to access.
Recommendation — Inventory existing infrastructure so data sources reference owned assets instead of assuming them. Define managed infrastructure through approved baselines and review changes before apply. Restrict Terraform credentials so modules can only create or read the resources they need.
ISO/IEC 27001:2022A.5.9 — Inventory of information and other associated assetsData sources rely on accurate asset discovery for shared infrastructure references.
A.8.9 — Configuration managementResources and data sources both depend on controlled infrastructure configuration.
Recommendation — Maintain an accurate asset inventory so shared infrastructure can be referenced safely. Control infrastructure changes through reviewed configuration and preserve declared ownership boundaries.

Practitioner Guidance

What to verify: Before you approve a module, check whether each Terraform block is actually asserting ownership or only discovering context. If a shared object is modeled as a resource, you can accidentally take lifecycle control of infrastructure you do not own.

Decision rule: If the object must be created, updated, or deleted by Terraform, model it as a resource; if it already exists and should only be read, model it as a data source. If the same object flips between those roles across environments, treat that as a design smell and standardise the ownership model.

Practitioner takeaway: The real distinction is not syntax, it is ownership. Resources express managed desired state, while data sources express dependency on external state, and that line should stay crisp if you want predictable plans and safer module boundaries.

Free weekly newsletter

Subscribe to the NHI & AI Identity Journal

The latest on NHI and Agentic AI security – articles, research, breaches, news and events every week.

Bonus 33% off our NHI Course when you subscribe.

NHIMG Editorial Note
Reviewed and updated by the NHIMG editorial team on October 30, 2026.
NHI Mgmt Group — the #1 independent authority on Non-Human Identity, IAM, and Agentic AI security. nhimg.org