r/Terraform • • 7d ago

terraform_remote_state Data Source

https://developer.hashicorp.com/terraform/language/state/remote-state-data

We have a project underway for subscription vending in Azure, as part of the code base, it has been decided to use the terraform_remote_state data source.

To date I have never seen or used this resource, I have always utilised data blocks and providers.

Is anyone using this or am I right in assuming this is not a good idea, I know its mentioned on the HCP site that using the terraform_remote_state is not the preferred option.

5 Upvotes

7 comments sorted by

3

u/jonathanio 7d ago

I've used it a lot, but generally it's not recommended and it requires full access to the state file in order to work. All potential secrets and sensitive data must be accessed to get to the Terraform outputs you import.

If your CI/CD processes are built well enough and break-glass has the right controls, so that you are confident that the risk of one state accessing multiple states safely is mostly mitigated, it's OK. But you'll have to balance the risks and rewards yourself.

Also, beware of state file loops. That's something very easy to get yourself into if you're not careful in your overall architectural design.

2

u/Downtown_Ant5927 7d ago

We use it for central services outputs such as the network module components: vpc id, subnets, etc. It introduces a tight coupling as the documentation mentioned and you have to stick to best practices for versioning and publishing your central modules. I would probably not use remote state sources everywhere but it did help us by just referencing these central components that everyone needs and not letting them all do data source lookups.

2

u/zMynxx 7d ago

Happens a lot in large enterprises where the responsibility is separated, e.g network owned and managed by the platform team, etc.
No common otherwise however, but it is how terragrunt works with dependencies though behind the scenes.

2

u/Mehmet-Ozturk 6d ago

terraform_remote_state reads snapshots, so budget migration time for sequencing applies.

1

u/dusanodalovic 7d ago

for vending i'd try to skip it. once the subscription exists in azure, the next stack can find it with the azurerm_subscriptions data source filtered by display name. if it needs other values, the vending run can write them to a key vault secret or a tag. that way nobody needs read access to the whole state file. what does the next stack actually need to read from it?

1

u/No-Magazine2625 1d ago

My main concern with terraform_remote_state is the access boundary. Although the data source exposes root outputs, the consuming identity generally needs access to the underlying state snapshot, which may contain sensitive information. For subscription vending, I would consider publishing the required identifiers through a dedicated configuration interface, or using tfe_outputs where HCP Terraform is available.

1

u/magnetik79 17h ago

Never really a fan - since you're now building dependencies between configurations. In terrible cases, I've seen diamond dependencies - where two or more configurations are dependent on each other (chicken before the egg).

Personally, I prefer to drive with variables and well known values feed to configurations - rather than digging into other configuration states.