Brett
Brett

Reputation: 6030

Is it possible to access all resources in a terraform remote state that aren't declared as outputs

I am trying to get some references from terraform remote state, and have noticed some differences between terraform state resources / data, and using a terraform_remote_state data object.

For example, I have a terraform module that created an AWS managed directory, with no outputs. Within that module, I can see all resources in the state - e.g. terraform state show aws_directory_service_directory.ad gives me details on the directory - the directory ID, DNS server addresses, etc.

$ terraform state list
aws_directory_service_directory.ad
$ terraform state show aws_directory_service_directory.ad
# aws_directory_service_directory.ad:
resource "aws_directory_service_directory" "ad" {
    access_url        = "REDACTED"
    alias             = "REDACTED"
    dns_ip_addresses  = []
    .... etc ....
}

If I then create a new module and add a terraform_remote_state data object, i cannot access any properties of the directory - data.terraform_remote_state.ad.outputs is empty. From within this new module, if I only have the remote state data object, and apply (with no resources), and then use terraform console and show data.terraform_remote_state.ad, it looks like:

$ terraform console
> data.terraform_remote_state.ad
{
  "backend" = ".."
  "config" = { remote_state config shown here }
  "outputs" = {}
}

So the resources are in the state, but not accessible directly. Is this expected behaviour? Is there any way to access the resources in the remote state, or would I need to add attributes into outputs and use data.terraform_remote_state.ad.outputs.whatever_attributes?

Upvotes: 2

Views: 3593

Answers (1)

Marcin
Marcin

Reputation: 238687

You can only access outputs. From docs:

terraform_remote_state only exposes output values

You have to modify the parent module of the other setup and add required outputs.

The other way would be to develop your own, fully custom data source to provide info that you need.

Upvotes: 3

Related Questions