Join our Newsletter — 33% off our NHI Course
Home FAQ Cyber Security When should organisations prioritise data sources over variables…
Cyber Security

When should organisations prioritise data sources over variables or remote state in Terraform?

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

Organisations should prioritise data sources when the needed value already exists outside the module, must be discovered dynamically, or should stay separate from the calling configuration. Variables are better for intentional inputs, while remote state is only suitable when cross-stack sharing is unavoidable. The choice should reflect security, coupling, and how frequently the underlying value changes.

Why This Matters for Security Teams

Choosing between data sources, variables, and remote state affects more than Terraform style. It determines where truth lives, how much coupling a module inherits, and whether a value is discovered at runtime or injected as an assumption. In security-sensitive infrastructure, those decisions shape drift tolerance, reuse, and the blast radius of a misconfigured stack. The wrong choice often looks convenient at first, then becomes a maintenance and trust problem once environments multiply.

Variables are best when the module should accept an intentional input and nothing more. Data sources are better when the configuration must resolve an existing object from the provider rather than rely on a caller to pass it correctly. Remote state is the weakest of the three for sharing, because it exposes one stack’s outputs as a dependency for another stack. That is useful, but it creates an operational and access-control relationship that should be treated as deliberate, not default.

In practice, many teams discover the coupling problem only after a shared output changes or an assumed value stops matching the real environment.

How It Works in Practice

The practical decision starts with provenance. If the value is owned by the current module, or the caller should consciously supply it, use a variable. If the value already exists in the target environment and Terraform should look it up from the provider, use a data source. If the value comes from another stack and there is no better way to expose it, remote state can bridge the gap, but that should be a controlled exception rather than the normal pattern.

That distinction matters because each mechanism creates a different trust model. Variables assume the caller is authoritative. Data sources assume the provider can reliably return the current object. Remote state assumes one stack may consume another stack’s outputs, which means the consumer inherits the producer’s lifecycle and access constraints. The more sensitive the value, the more important it is to avoid turning a convenience path into a hidden dependency.

A simple rule set helps:

  • Use a

    variable

    when the value is a deliberate input and should be explicit in the calling configuration.
  • Use a

    data source

    when the object already exists and should be resolved dynamically from the provider.
  • Use

    remote state

    only when cross-stack sharing is unavoidable and the dependency is stable enough to manage.

In security terms, data sources usually reduce drift risk because they reference the live provider object, while variables can become stale if they duplicate external truth. Remote state can be appropriate for outputs such as VPC IDs, subnet IDs, or shared platform names, but it should not become a general data bus between modules. The more the consumer depends on producer internals, the harder it is to change, rotate, or isolate either stack without unintended impact. For shared infrastructure patterns, the NHI-management guidance on lifecycle, visibility, and rotation is a useful analogue for thinking about long-lived dependencies, especially when automation spans many systems. Ultimate Guide to NHIs

These controls tend to break down when teams use remote state to share values across environments that change independently, because the consumer then inherits coupling and permissions it does not actually need.

Common Variations and Edge Cases

Tighter separation often increases lookup overhead, so teams have to balance clarity against the cost of repeated provider queries or extra wiring between stacks. That trade-off becomes visible in larger platforms where shared values are stable, but the surrounding environments are not.

One common edge case is a value that exists outside the module but is not meant to be discovered generically. In that case, a variable can still be the right choice if the caller is the governance boundary and the input is intentionally curated. Another case is where remote state is technically available but would expose more information than the consumer needs. In those situations, a data source or a narrower exported output is usually safer than exposing an entire state file.

Another nuance is change frequency. If the underlying object changes often, data sources keep the configuration aligned with the provider’s current truth. If the object is meant to be fixed by policy, a variable can make that decision more visible. Remote state sits in the middle, but best practice is evolving toward minimizing direct state coupling and publishing only the outputs that downstream stacks truly require. For organisations standardising shared infrastructure, the broad NHI visibility and governance statistics in Ultimate Guide to NHIs reinforce the broader principle that hidden dependencies age badly when they are not inventoried and reviewed.

What practitioners often underestimate is that the safest-looking option is not always the most stable one. A module can be “clean” syntactically and still be operationally brittle if it depends on opaque remote outputs that no one treats as a contract.

Standards & Framework Alignment

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

CIS Controls v8 and NIST CSF 2.0 set the governance and control requirements practitioners need to meet.

FrameworkControl / ReferenceRelevance
CIS Controls v8CIS Control 6 — Access Control ManagementTerraform state sharing affects access paths and least-privilege exposure.
Recommendation — Restrict access to shared state and exported outputs to only the stacks that need them.
NIST CSF 2.0PR.AC — Identity Management, Authentication, and Access ControlModule inputs, data lookups, and remote state all shape who can trust and use configuration values.
PR.DS — Data SecurityRemote state and shared outputs can expose sensitive infrastructure data if handled loosely.
Recommendation — Define trusted sources for infrastructure values and limit cross-stack access to approved dependencies. Protect state files and exported outputs as sensitive data with strict storage and access controls.

Practitioner Guidance

What to prioritise: Prioritise the mechanism that best matches ownership of truth. If the module should discover an existing object, use a data source; if it should accept a deliberate input, use a variable; if it must consume another stack’s output, treat remote state as a constrained integration point.

What to verify: Verify that the chosen mechanism does not duplicate the same value in two places. If a variable mirrors an external object, or remote state duplicates something the provider can already resolve, the configuration is more fragile than it looks.

Decision rule: If removing the dependency on remote state would not materially change the architecture, remove it. If the value can be obtained directly from the provider, prefer the data source and keep the dependency explicit at the edge of the module.

Practitioner takeaway: The best default is the one that makes the value easiest to trust, not the easiest to pass around, and remote state should be justified as a deliberate cross-stack contract rather than a convenience.

Deepen Your Knowledge

Sign up to our weekly newsletter — get 33% off our NHI Foundation Level Course

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