github.com/eliastor/durgaform@v0.0.0-20220816172711-d0ab2d17673e/website/docs/language/data-sources/index.mdx (about)

     1  ---
     2  page_title: Data Sources - Configuration Language
     3  description: >-
     4    Data sources allow Terraform to use external data, function output, and data
     5    from other configurations. Learn data resource arguments, behavior, and
     6    lifecycle.
     7  ---
     8  
     9  # Data Sources
    10  
    11  _Data sources_ allow Terraform to use information defined outside of Terraform,
    12  defined by another separate Terraform configuration, or modified by functions.
    13  
    14  > **Hands-on:** Try the [Query Data Sources](https://learn.hashicorp.com/tutorials/terraform/data-sources) tutorial on HashiCorp Learn.
    15  
    16  Each [provider](/language/providers) may offer data sources
    17  alongside its set of [resource](/language/resources)
    18  types.
    19  
    20  ## Using Data Sources
    21  
    22  A data source is accessed via a special kind of resource known as a
    23  _data resource_, declared using a `data` block:
    24  
    25  ```hcl
    26  data "aws_ami" "example" {
    27    most_recent = true
    28  
    29    owners = ["self"]
    30    tags = {
    31      Name   = "app-server"
    32      Tested = "true"
    33    }
    34  }
    35  ```
    36  
    37  A `data` block requests that Terraform read from a given data source ("aws_ami")
    38  and export the result under the given local name ("example"). The name is used
    39  to refer to this resource from elsewhere in the same Terraform module, but has
    40  no significance outside of the scope of a module.
    41  
    42  The data source and name together serve as an identifier for a given
    43  resource and so must be unique within a module.
    44  
    45  Within the block body (between `{` and `}`) are query constraints defined by
    46  the data source. Most arguments in this section depend on the
    47  data source, and indeed in this example `most_recent`, `owners` and `tags` are
    48  all arguments defined specifically for [the `aws_ami` data source](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/ami).
    49  
    50  When distinguishing from data resources, the primary kind of resource (as declared
    51  by a `resource` block) is known as a _managed resource_. Both kinds of resources
    52  take arguments and export attributes for use in configuration, but while
    53  managed resources cause Terraform to create, update, and delete infrastructure
    54  objects, data resources cause Terraform only to _read_ objects. For brevity,
    55  managed resources are often referred to just as "resources" when the meaning
    56  is clear from context.
    57  
    58  ## Data Source Arguments
    59  
    60  Each data resource is associated with a single data source, which determines
    61  the kind of object (or objects) it reads and what query constraint arguments
    62  are available.
    63  
    64  Each data source in turn belongs to a [provider](/language/providers),
    65  which is a plugin for Terraform that offers a collection of resource types and
    66  data sources that most often belong to a single cloud or on-premises
    67  infrastructure platform.
    68  
    69  Most of the items within the body of a `data` block are defined by and
    70  specific to the selected data source, and these arguments can make full
    71  use of [expressions](/language/expressions) and other dynamic
    72  Terraform language features.
    73  
    74  However, there are some "meta-arguments" that are defined by Terraform itself
    75  and apply across all data sources. These arguments often have additional
    76  restrictions on what language features can be used with them, and are described
    77  in more detail in the following sections.
    78  
    79  ## Data Resource Behavior
    80  
    81  Terraform reads data resources during the planning phase when possible, but
    82  announces in the plan when it must defer reading resources until the apply
    83  phase to preserve the order of operations. Terraform defers reading data
    84  resources in the following situations:
    85  * At least one of the given arguments is a managed resource attribute or
    86    other value that Terraform cannot predict until the apply step.
    87  * The data resource depends directly on a managed resource that itself has
    88    planned changes in the current plan.
    89  * The data resource has
    90    [custom conditions](#custom-condition-checks)
    91    and it depends directly or indirectly on a managed resource that itself
    92    has planned changes in the current plan.
    93  
    94  Refer to [Data Resource Dependencies](#data-resource-dependencies) for details
    95  on what it means for a data resource to depend on other objects. Any resulting
    96  attribute of such a data resource will be unknown during planning, so it cannot
    97  be used in situations where values must be fully known.
    98  
    99  ## Local-only Data Sources
   100  
   101  While many data sources correspond to an infrastructure object type that
   102  is accessed via a remote network API, some specialized data sources operate
   103  only within Terraform itself, calculating some results and exposing them
   104  for use elsewhere.
   105  
   106  For example, local-only data sources exist for
   107  [rendering templates](https://registry.terraform.io/providers/hashicorp/template/latest/docs/data-sources/file),
   108  [reading local files](https://registry.terraform.io/providers/hashicorp/local/latest/docs/data-sources/file), and
   109  [rendering AWS IAM policies](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document).
   110  
   111  The behavior of local-only data sources is the same as all other data
   112  sources, but their result data exists only temporarily during a Terraform
   113  operation, and is re-calculated each time a new plan is created.
   114  
   115  ## Data Resource Dependencies
   116  
   117  Data resources have the same dependency resolution behavior
   118  [as defined for managed resources](/language/resources/behavior#resource-dependencies).
   119  Setting the `depends_on` meta-argument within `data` blocks defers reading of
   120  the data source until after all changes to the dependencies have been applied.
   121  
   122  In order to ensure that data sources are accessing the most up to date
   123  information possible in a wide variety of use cases, arguments directly
   124  referencing managed resources are treated the same as if the resource was
   125  listed in `depends_on`. This behavior can be avoided when desired by indirectly
   126  referencing the managed resource values through a `local` value, unless the
   127  data resource itself has
   128  [custom conditions](#custom-condition-checks).
   129  
   130  ~> **NOTE:** **In Terraform 0.12 and earlier**, due to the data resource behavior of deferring the read until the apply phase when depending on values that are not yet known, using `depends_on` with `data` resources will force the read to always be deferred to the apply phase, and therefore a configuration that uses `depends_on` with a `data` resource can never converge. Due to this behavior, we do not recommend using `depends_on` with data resources.
   131  
   132  ## Custom Condition Checks
   133  
   134  You can use `precondition` and `postcondition` blocks to specify assumptions and guarantees about how the data source operates. The following examples creates a postcondition that checks whether the AMI has the correct tags.
   135  
   136  ``` hcl
   137  data "aws_ami" "example" {
   138    id = var.aws_ami_id
   139  
   140    lifecycle {
   141      # The AMI ID must refer to an existing AMI that has the tag "nomad-server".
   142      postcondition {
   143        condition     = self.tags["Component"] == "nomad-server"
   144        error_message = "tags[\"Component\"] must be \"nomad-server\"."
   145      }
   146    }
   147  }
   148  ```
   149  
   150  Custom conditions can help capture assumptions, helping future maintainers understand the configuration design and intent. They also return useful information about errors earlier and in context, helping consumers more easily diagnose issues in their configurations.
   151  
   152  Refer to [Custom Condition Checks](/language/expressions/custom-conditions#preconditions-and-postconditions) for more details.
   153  
   154  
   155  ## Multiple Resource Instances
   156  
   157  Data resources support [`count`](/language/meta-arguments/count)
   158  and [`for_each`](/language/meta-arguments/for_each)
   159  meta-arguments as defined for managed resources, with the same syntax and behavior.
   160  
   161  As with managed resources, when `count` or `for_each` is present it is important to
   162  distinguish the resource itself from the multiple resource _instances_ it
   163  creates. Each instance will separately read from its data source with its
   164  own variant of the constraint arguments, producing an indexed result.
   165  
   166  ## Selecting a Non-default Provider Configuration
   167  
   168  Data resources support [the `provider` meta-argument](/language/meta-arguments/resource-provider)
   169  as defined for managed resources, with the same syntax and behavior.
   170  
   171  ## Lifecycle Customizations
   172  
   173  Data resources do not currently have any customization settings available
   174  for their lifecycle, but the `lifecycle` nested block is reserved in case
   175  any are added in future versions.
   176  
   177  ## Example
   178  
   179  A data source configuration looks like the following:
   180  
   181  ```hcl
   182  # Find the latest available AMI that is tagged with Component = web
   183  data "aws_ami" "web" {
   184    filter {
   185      name   = "state"
   186      values = ["available"]
   187    }
   188  
   189    filter {
   190      name   = "tag:Component"
   191      values = ["web"]
   192    }
   193  
   194    most_recent = true
   195  }
   196  ```
   197  
   198  ## Description
   199  
   200  The `data` block creates a data instance of the given _type_ (first
   201  block label) and _name_ (second block label). The combination of the type
   202  and name must be unique.
   203  
   204  Within the block (the `{ }`) is configuration for the data instance. The
   205  configuration is dependent on the type; as with
   206  [resources](/language/resources), each provider on the
   207  [Terraform Registry](https://registry.terraform.io/browse/providers) has its own
   208  documentation for configuring and using the data types it provides.
   209  
   210  Each data instance will export one or more attributes, which can be
   211  used in other resources as reference expressions of the form
   212  `data.<TYPE>.<NAME>.<ATTRIBUTE>`. For example:
   213  
   214  ```hcl
   215  resource "aws_instance" "web" {
   216    ami           = data.aws_ami.web.id
   217    instance_type = "t1.micro"
   218  }
   219  ```
   220  
   221  ## Meta-Arguments
   222  
   223  As data sources are essentially a read only subset of resources, they also
   224  support the same [meta-arguments](/language/resources/syntax#meta-arguments) of resources
   225  with the exception of the
   226  [`lifecycle` configuration block](/language/meta-arguments/lifecycle).
   227  
   228  ### Non-Default Provider Configurations
   229  
   230  Similarly to [resources](/language/resources), when
   231  a module has multiple configurations for the same provider you can specify which
   232  configuration to use with the `provider` meta-argument:
   233  
   234  ```hcl
   235  data "aws_ami" "web" {
   236    provider = aws.west
   237  
   238    # ...
   239  }
   240  ```
   241  
   242  See
   243  [The Resource `provider` Meta-Argument](/language/meta-arguments/resource-provider)
   244  for more information.
   245  
   246  ## Data Source Lifecycle
   247  
   248  If the arguments of a data instance contain no references to computed values,
   249  such as attributes of resources that have not yet been created, then the
   250  data instance will be read and its state updated during Terraform's "refresh"
   251  phase, which by default runs prior to creating a plan. This ensures that the
   252  retrieved data is available for use during planning and the diff will show
   253  the real values obtained.
   254  
   255  Data instance arguments may refer to computed values, in which case the
   256  attributes of the instance itself cannot be resolved until all of its
   257  arguments are defined. In this case, refreshing the data instance will be
   258  deferred until the "apply" phase, and all interpolations of the data instance
   259  attributes will show as "computed" in the plan since the values are not yet
   260  known.