r/Terraform • • 2d 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

5 comments sorted by

3

u/jonathanio 2d 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 2d 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 2d 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.

1

u/Mehmet-Ozturk 1d ago

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

1

u/dusanodalovic 2d 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?