github.com/rstandt/terraform@v0.12.32-0.20230710220336-b1063613405c/website/docs/configuration-0-11/data-sources.html.md (about)

     1  ---
     2  layout: "docs"
     3  page_title: "Data Sources - 0.11 Configuration Language"
     4  sidebar_current: "docs-conf-old-data-sources"
     5  description: |-
     6    Data sources allow data to be fetched or computed for use elsewhere in Terraform configuration.
     7  ---
     8  
     9  # Data Sources
    10  
    11  -> **Note:** This page is about Terraform 0.11 and earlier. For Terraform 0.12
    12  and later, see
    13  [Configuration Language: Configuring Data Sources](../configuration/data-sources.html).
    14  
    15  *Data sources* allow data to be fetched or computed for use elsewhere
    16  in Terraform configuration. Use of data sources allows a Terraform
    17  configuration to build on information defined outside of Terraform,
    18  or defined by another separate Terraform configuration.
    19  
    20  [Providers](./providers.html) are responsible in
    21  Terraform for defining and implementing data sources. Whereas
    22  a [resource](./resources.html) causes Terraform
    23  to create and manage a new infrastructure component, data sources
    24  present read-only views into pre-existing data, or they compute
    25  new values on the fly within Terraform itself.
    26  
    27  For example, a data source may retrieve remote state data from a
    28  Terraform Cloud workspace, configuration information from Consul, or look up a pre-existing
    29  AWS resource by filtering on its attributes and tags.
    30  
    31  Every data source in Terraform is mapped to a provider based
    32  on longest-prefix matching. For example the `aws_ami`
    33  data source would map to the `aws` provider (if that exists).
    34  
    35  This page assumes you're familiar with the
    36  [configuration syntax](./syntax.html)
    37  already.
    38  
    39  ## Example
    40  
    41  A data source configuration looks like the following:
    42  
    43  ```hcl
    44  # Find the latest available AMI that is tagged with Component = web
    45  data "aws_ami" "web" {
    46    filter {
    47      name   = "state"
    48      values = ["available"]
    49    }
    50  
    51    filter {
    52      name   = "tag:Component"
    53      values = ["web"]
    54    }
    55  
    56    most_recent = true
    57  }
    58  ```
    59  
    60  ## Description
    61  
    62  The `data` block creates a data instance of the given `TYPE` (first
    63  parameter) and `NAME` (second parameter). The combination of the type
    64  and name must be unique.
    65  
    66  Within the block (the `{ }`) is configuration for the data instance. The
    67  configuration is dependent on the type, and is documented for each
    68  data source in the [providers section](/docs/providers/index.html).
    69  
    70  Each data instance will export one or more attributes, which can be
    71  interpolated into other resources using variables of the form
    72  `data.TYPE.NAME.ATTR`. For example:
    73  
    74  ```hcl
    75  resource "aws_instance" "web" {
    76    ami           = "${data.aws_ami.web.id}"
    77    instance_type = "t1.micro"
    78  }
    79  ```
    80  
    81  ### Meta-parameters
    82  
    83  As data sources are essentially a read only subset of resources they also support the same [meta-parameters](./resources.html#meta-parameters) of resources except for the [`lifecycle` configuration block](./resources.html#lifecycle).
    84  
    85  ## Multiple Provider Instances
    86  
    87  Similarly to [resources](./resources.html), the
    88  `provider` meta-parameter can be used where a configuration has
    89  multiple aliased instances of the same provider:
    90  
    91  ```hcl
    92  data "aws_ami" "web" {
    93    provider = "aws.west"
    94  
    95    # ...
    96  }
    97  ```
    98  
    99  See the ["Multiple Provider Instances"](./resources.html#multiple-provider-instances) documentation for resources
   100  for more information.
   101  
   102  ## Data Source Lifecycle
   103  
   104  If the arguments of a data instance contain no references to computed values,
   105  such as attributes of resources that have not yet been created, then the
   106  data instance will be read and its state updated during Terraform's "refresh"
   107  phase, which by default runs prior to creating a plan. This ensures that the
   108  retrieved data is available for use during planning and the diff will show
   109  the real values obtained.
   110  
   111  Data instance arguments may refer to computed values, in which case the
   112  attributes of the instance itself cannot be resolved until all of its
   113  arguments are defined. In this case, refreshing the data instance will be
   114  deferred until the "apply" phase, and all interpolations of the data instance
   115  attributes will show as "computed" in the plan since the values are not yet
   116  known.