github.com/muratcelep/terraform@v1.1.0-beta2-not-internal-4/website/docs/language/state/remote-state-data.html.md (about)

     1  ---
     2  layout: "language"
     3  page_title: "The terraform_remote_state Data Source"
     4  sidebar_current: "docs-terraform-datasource-remote-state"
     5  description: |-
     6    Retrieves the root module output values from a Terraform state snapshot stored in a remote backend.
     7  ---
     8  
     9  # The `terraform_remote_state` Data Source
    10  
    11  [backends]: /docs/backends/index.html
    12  
    13  The `terraform_remote_state` data source retrieves the root module output values
    14  from some other Terraform configuration, using the latest state snapshot from
    15  the remote backend.
    16  
    17  This data source is built into Terraform, and is always available; you do not
    18  need to require or configure a provider in order to use it.
    19  
    20  -> **Note:** This data source is implemented by a built-in provider, whose
    21  [source address](/docs/language/providers/requirements.html#source-addresses)
    22  is `terraform.io/builtin/terraform`. That provider does not include any other
    23  resources or data sources.
    24  
    25  ## Alternative Ways to Share Data Between Configurations
    26  
    27  Sharing data with root module outputs is convenient, but it has drawbacks.
    28  Although `terraform_remote_state` only exposes output values, its user must have
    29  access to the entire state snapshot, which often includes some sensitive
    30  information.
    31  
    32  When possible, we recommend explicitly publishing data for external consumption
    33  to a separate location instead of accessing it via remote state. This lets you
    34  apply different access controls for shared information and state snapshots.
    35  
    36  To share data explicitly between configurations, you can use pairs of managed
    37  resource types and data sources in various providers, including (but not
    38  limited to) the following:
    39  
    40  | System | Publish with... | Read with... |
    41  |--|--|--|
    42  | Alibaba Cloud DNS<br><small>(for IP addresses and hostnames)</small> | [`alicloud_alidns_record` resource type](https://registry.terraform.io/providers/aliyun/alicloud/latest/docs/resources/alidns_record) | Normal DNS lookups, or [the `dns` provider](https://registry.terraform.io/providers/hashicorp/dns/latest/docs) |
    43  | Amazon Route53<br><small>(for IP addresses and hostnames)</small> | [`aws_route53_record` resource type](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/route53_record) | Normal DNS lookups, or [the `dns` provider](https://registry.terraform.io/providers/hashicorp/dns/latest/docs) |
    44  | Amazon S3 | [`aws_s3_bucket_object` resource type](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket_object) | [`aws_s3_bucket_object` data source](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/s3_bucket_object) |
    45  | Amazon SSM Parameter Store | [`aws_ssm_parameter` resource type](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/ssm_parameter) | [`aws_ssm_parameter` data source](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/ssm_parameter) |
    46  | Azure Automation | [`azurerm_automation_variable_string` resource type](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/automation_variable_string) | [`azurerm_automation_variable_string` data source](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/data-sources/automation_variable_string) |
    47  | Azure DNS<br><small>(for IP addresses and hostnames)</small> | [`azurerm_dns_a_record` resource type](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/dns_a_record), etc | Normal DNS lookups, or [the `dns` provider](https://registry.terraform.io/providers/hashicorp/dns/latest/docs) |
    48  | Google Cloud DNS<br><small>(for IP addresses and hostnames)</small> | [`google_dns_record_set` resource type](https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/dns_record_set) | Normal DNS lookups, or [the `dns` provider](https://registry.terraform.io/providers/hashicorp/dns/latest/docs) |
    49  | Google Cloud Storage | [`google_storage_bucket_object`  resource type](https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/storage_bucket_object) | [`google_storage_bucket_object` data source](https://registry.terraform.io/providers/hashicorp/google/latest/docs/data-sources/storage_bucket_object) and [`http` data source](https://registry.terraform.io/providers/hashicorp/http/latest/docs/data-sources/http) |
    50  | HashiCorp Consul | [`consul_key_prefix` resource type](https://registry.terraform.io/providers/hashicorp/consul/latest/docs/resources/key_prefix) | [`consul_key_prefix` data source](https://registry.terraform.io/providers/hashicorp/consul/latest/docs/data-sources/key_prefix) |
    51  | HashiCorp Terraform Cloud | Normal `outputs` terraform block | [`tfe_outputs` data source](https://registry.terraform.io/providers/hashicorp/tfe/latest/docs/data-sources/outputs) |
    52  | Kubernetes | [`kubernetes_config_map` resource type](https://registry.terraform.io/providers/hashicorp/kubernetes/latest/docs/resources/config_map) | [`kubernetes_config_map` data source](https://registry.terraform.io/providers/hashicorp/kubernetes/latest/docs/data-sources/config_map) |
    53  | OCI Object Storage | [`oci_objectstorage_bucket` resource type](https://registry.terraform.io/providers/hashicorp/oci/latest/docs/resources/objectstorage_object) | [`oci_objectstorage_bucket` data source](https://registry.terraform.io/providers/hashicorp/oci/latest/docs/data-sources/objectstorage_object) |
    54  
    55  -> These are some common options from the Official Terraform providers, but
    56  there are too many configuration storage options for us to list them all
    57  here, including some in partner and community providers.
    58  Any pair of managed resource type and corresponding data source can potentially
    59  be used to share data between Terraform configurations. See individual provider
    60  documentation to find other possibilities.
    61  
    62  A key advantage of using a separate explicit configuration store instead of
    63  `terraform_remote_state` is that the data can potentially also be read by
    64  systems other than Terraform, such as configuration management or scheduler
    65  systems within your compute instances. For that reason, we recommend selecting
    66  a configuration store that your other infrastructure could potentially make
    67  use of. For example:
    68  
    69  * If you wish to share IP addresses and hostnames, you could publish them as
    70  normal DNS `A`, `AAAA`, `CNAME`, and `SRV` records in a private DNS zone and
    71  then configure your other infrastructure to refer to that zone so you can
    72  find infrastructure objects via your system's built-in DNS resolver.
    73  * If you use HashiCorp Consul then publishing data to the Consul key/value
    74  store or Consul service catalog can make that data also accessible via
    75  [Consul Template](https://github.com/hashicorp/consul-template)
    76  or the
    77  [HashiCorp Nomad](https://www.nomadproject.io/docs/job-specification/template)
    78  `template` stanza.
    79  * If you use Kubernetes then you can
    80  [make Config Maps available to your Pods](https://kubernetes.io/docs/tasks/configure-pod-container/configure-pod-configmap/).
    81  
    82  Some of the data stores listed above are specifically designed for storing
    83  small configuration values, while others are generic blob storage systems. For
    84  those generic systems, you can use
    85  [the `jsonencode` function](https://www.terraform.io/docs/language/functions/jsonencode.html)
    86  and
    87  [the `jsondecode` function](https://www.terraform.io/docs/language/functions/jsondecode.html) respectively
    88  to store and retrieve structured data.
    89  
    90  You can encapsulate the implementation details of retrieving your published
    91  configuration data by writing a
    92  [data-only module](/docs/language/modules/develop/composition.html#data-only-modules)
    93  containing the necessary data source configuration and any necessary
    94  post-processing such as JSON decoding. You can then change that module later
    95  if you switch to a different strategy for sharing data between multiple
    96  Terraform configurations.
    97  
    98  ## Usage with Terraform Cloud/Enterprise
    99  
   100  When trying to access remote state outputs in Terraform Cloud/Enterprise, it is recommended to use the `tfe_outputs` data source in the [Terraform Cloud/Enterprise Provider](https://registry.terraform.io/providers/hashicorp/tfe/latest/docs) instead of relying the `terraform_remote_state` data source.
   101  
   102  See the [full documentation](https://registry.terraform.io/providers/hashicorp/tfe/latest/docs/data-sources/outputs) for the `tfe_outputs` data source for more details.
   103  
   104  ## Example Usage (`remote` Backend)
   105  
   106  ```hcl
   107  data "terraform_remote_state" "vpc" {
   108    backend = "remote"
   109  
   110    config = {
   111      organization = "hashicorp"
   112      workspaces = {
   113        name = "vpc-prod"
   114      }
   115    }
   116  }
   117  
   118  # Terraform >= 0.12
   119  resource "aws_instance" "foo" {
   120    # ...
   121    subnet_id = data.terraform_remote_state.vpc.outputs.subnet_id
   122  }
   123  
   124  # Terraform <= 0.11
   125  resource "aws_instance" "foo" {
   126    # ...
   127    subnet_id = "${data.terraform_remote_state.vpc.subnet_id}"
   128  }
   129  ```
   130  
   131  ## Example Usage (`local` Backend)
   132  
   133  ```hcl
   134  data "terraform_remote_state" "vpc" {
   135    backend = "local"
   136  
   137    config = {
   138      path = "..."
   139    }
   140  }
   141  
   142  # Terraform >= 0.12
   143  resource "aws_instance" "foo" {
   144    # ...
   145    subnet_id = data.terraform_remote_state.vpc.outputs.subnet_id
   146  }
   147  
   148  # Terraform <= 0.11
   149  resource "aws_instance" "foo" {
   150    # ...
   151    subnet_id = "${data.terraform_remote_state.vpc.subnet_id}"
   152  }
   153  ```
   154  
   155  ## Argument Reference
   156  
   157  The following arguments are supported:
   158  
   159  * `backend` - (Required) The remote backend to use.
   160  * `workspace` - (Optional) The Terraform workspace to use, if the backend
   161    supports workspaces.
   162  * `config` - (Optional; object) The configuration of the remote backend.
   163    Although this argument is listed as optional, most backends require
   164    some configuration.
   165  
   166      The `config` object can use any arguments that would be valid in the
   167      equivalent `terraform { backend "<TYPE>" { ... } }` block. See
   168      [the documentation of your chosen backend](/docs/language/settings/backends/index.html)
   169      for details.
   170  
   171      -> **Note:** If the backend configuration requires a nested block, specify
   172      it here as a normal attribute with an object value. (For example,
   173      `workspaces = { ... }` instead of `workspaces { ... }`.)
   174  * `defaults` - (Optional; object) Default values for outputs, in case the state
   175    file is empty or lacks a required output.
   176  
   177  ## Attributes Reference
   178  
   179  In addition to the above, the following attributes are exported:
   180  
   181  * (v0.12+) `outputs` - An object containing every root-level
   182    [output](/docs/language/values/outputs.html) in the remote state.
   183  * (<= v0.11) `<OUTPUT NAME>` - Each root-level [output](/docs/language/values/outputs.html)
   184    in the remote state appears as a top level attribute on the data source.
   185  
   186  ## Root Outputs Only
   187  
   188  Only the root-level output values from the remote state snapshot are exposed
   189  for use elsewhere in your module. Resource data and output values from nested
   190  modules are not accessible.
   191  
   192  If you wish to make a nested module output value accessible as a root module
   193  output value, you must explicitly configure a passthrough in the root module.
   194  For example:
   195  
   196  For example:
   197  
   198  ```hcl
   199  module "app" {
   200    source = "..."
   201  }
   202  
   203  output "app_value" {
   204    # This syntax is for Terraform 0.12 or later.
   205    value = module.app.example
   206  }
   207  ```
   208  
   209  In this example, the output value named `example` from the "app" module is
   210  available as the `app_value` root module output value. If this configuration
   211  didn't include the `output "app_value"` block then the data would not be
   212  accessible via `terraform_remote_state`.
   213  
   214  ~> **Warning:** Although `terraform_remote_state` doesn't expose any other
   215  state snapshot information for use in configuration, the state snapshot data
   216  is a single object and so any user or server which has enough access to read
   217  the root module output values will also always have access to the full state
   218  snapshot data by direct network requests. Don't use `terraform_remote_state`
   219  if any of the resources in your configuration work with data that you consider
   220  sensitive.