Where can I find the module a resource was built with if I look at the TF State file? Is there an example of one?
hi @a_s_persram,
The terraform show command will format each resource’s name with the full module address. If you wanted to look at the state directly, each resource has a module field showing the configuration address of the module.
Thank you jbardin: I’ve been looking at State files on the net and there seems to be a difference in format. Is the below example the latest?
“resources”: [
{
“module”: “module.ecs”,
“mode”: “managed”,
“type”: “aws_ecs_cluster”,
“name”: “this”,
“provider”: “provider[" Terraform Registry "]”,
“instances”: [
{
“schema_version”: 0,
“attributes”: {
“name”: “demo-ecs-cluster”,
“arn”: “arn:aws:ecs:us-east-1:123456789012:cluster/demo-ecs-cluster”,
“id”: “arn:aws:ecs:us-east-1:123456789012:cluster/demo-ecs-cluster”,
“capacity_providers”: ,
“tags”: {
“Environment”: “dev”
}
}
}
]
}
While the state file format is not documented and not guaranteed to be stable, the general structure hasn’t changed since early on in Terraform’s development. The "module" field you see there is what names the resource’s containing module.
When you say that the format is not guaranteed to be stable, do you mean that it may change in say, the next version? Your comment about ‘early on in…’, I was looking at a state file and it had a root key called ‘modules’; resources were in that key, Can I assume that the file I was looking at was from an old version?
It’s not likely, just pointing out that it’s not technically a published stable format. Practically the overall structure will not change outside of a new major release for compatibility reasons; only minor details have changed, and those are usually additive. If you have a state with a top level "modules" key, it is very old. The current format was committed in the code 8 years ago.
Thanks for the insight. My research found both formats; I wasn’t sure which one was up to date. I asked ChatGPT but it mentioned version 4 and I couldn’t find references to v4.