2016-05-02 01:41:16 +02:00
|
|
|
---
|
|
|
|
layout: "docs"
|
|
|
|
page_title: "Configuring Data Sources"
|
|
|
|
sidebar_current: "docs-config-data-sources"
|
|
|
|
description: |-
|
|
|
|
Data sources allow data to be fetched or computed for use elsewhere in Terraform configuration.
|
|
|
|
---
|
|
|
|
|
2018-05-06 06:27:07 +02:00
|
|
|
# Data Sources
|
2016-05-02 01:41:16 +02:00
|
|
|
|
2018-05-06 06:27:07 +02:00
|
|
|
_Data sources_ allow data to be fetched or computed for use elsewhere
|
2016-05-02 01:41:16 +02:00
|
|
|
in Terraform configuration. Use of data sources allows a Terraform
|
2018-05-06 06:27:07 +02:00
|
|
|
configuration to make use of information defined outside of Terraform,
|
2016-05-02 01:41:16 +02:00
|
|
|
or defined by another separate Terraform configuration.
|
|
|
|
|
2018-12-11 01:14:33 +01:00
|
|
|
Each [provider](./providers.html) may offer data sources
|
|
|
|
alongside its set of [resource types](./resources.html#resource-types-and-arguments).
|
2016-05-02 01:41:16 +02:00
|
|
|
|
2018-05-06 06:27:07 +02:00
|
|
|
## Using Data Sources
|
2016-05-02 01:41:16 +02:00
|
|
|
|
2018-05-06 06:27:07 +02:00
|
|
|
A data source is accessed via a special kind of resource known as a
|
|
|
|
_data resource_, declared using a `data` block:
|
2016-05-02 01:41:16 +02:00
|
|
|
|
2018-05-06 06:27:07 +02:00
|
|
|
```hcl
|
|
|
|
data "aws_ami" "example" {
|
|
|
|
most_recent = true
|
|
|
|
|
|
|
|
owners = ["self"]
|
|
|
|
tags = {
|
|
|
|
Name = "app-server"
|
|
|
|
Tested = "true"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
A `data` block requests that Terraform read from a given data source ("aws_ami")
|
|
|
|
and export the result under the given local name ("example"). The name is used
|
|
|
|
to refer to this resource from elsewhere in the same Terraform module, but has
|
|
|
|
no significance outside of the scope of a module.
|
|
|
|
|
|
|
|
The data source and name together serve as an identifier for a given
|
|
|
|
resource and so must be unique within a module.
|
|
|
|
|
|
|
|
Within the block body (between `{` and `}`) are query constraints defined by
|
|
|
|
the data source. Most arguments in this section depend on the
|
|
|
|
data source, and indeed in this example `most_recent`, `owners` and `tags` are
|
|
|
|
all arguments defined specifically for [the `aws_ami` data source](/docs/providers/aws/d/ami.html).
|
|
|
|
|
|
|
|
When distinguishing from data resources, the primary kind of resource (as declared
|
|
|
|
by a `resource` block) is known as a _managed resource_. Both kinds of resources
|
|
|
|
take arguments and export attributes for use in configuration, but while
|
|
|
|
managed resources cause Terraform to create, update, and delete infrastructure
|
|
|
|
objects, data resources cause Terraform only to _read_ objects. For brevity,
|
|
|
|
managed resources are often referred to just as "resources" when the meaning
|
|
|
|
is clear from context.
|
|
|
|
|
|
|
|
## Data Source Arguments
|
|
|
|
|
|
|
|
Each data resource is associated with a single data source, which determines
|
|
|
|
the kind of object (or objects) it reads and what query constraint arguments
|
|
|
|
are available.
|
|
|
|
|
2018-12-11 01:14:33 +01:00
|
|
|
Each data source in turn belongs to a [provider](./providers.html),
|
2018-05-06 06:27:07 +02:00
|
|
|
which is a plugin for Terraform that offers a collection of resource types and
|
|
|
|
data sources that most often belong to a single cloud or on-premises
|
|
|
|
infrastructure platform.
|
|
|
|
|
|
|
|
Most of the items within the body of a `data` block are defined by and
|
|
|
|
specific to the selected data source, and these arguments can make full
|
2018-12-11 01:14:33 +01:00
|
|
|
use of [expressions](./expressions.html) and other dynamic
|
2018-05-06 06:27:07 +02:00
|
|
|
Terraform language features.
|
|
|
|
|
|
|
|
However, there are some "meta-arguments" that are defined by Terraform itself
|
|
|
|
and apply across all data sources. These arguments often have additional
|
|
|
|
restrictions on what language features can be used with them, and are described
|
|
|
|
in more detail in the following sections.
|
|
|
|
|
|
|
|
## Data Resource Behavior
|
|
|
|
|
|
|
|
If the query constraint arguments for a data resource refer only to constant
|
|
|
|
values or values that are already known, the data resource will be read and its
|
|
|
|
state updated during Terraform's "refresh" phase, which runs prior to creating a plan.
|
|
|
|
This ensures that the retrieved data is available for use during planning and
|
|
|
|
so Terraform's plan will show the actual values obtained.
|
|
|
|
|
|
|
|
Query constraint arguments may refer to values that cannot be determined until
|
|
|
|
after configuration is applied, such as the id of a managed resource that has
|
|
|
|
not been created yet. In this case, reading from the data source is deferred
|
|
|
|
until the apply phase, and any references to the results of the data resource
|
|
|
|
elsewhere in configuration will themselves be unknown until after the
|
|
|
|
configuration has been applied.
|
|
|
|
|
|
|
|
## Local-only Data Sources
|
|
|
|
|
|
|
|
While many data sources correspond to an infrastructure object type that
|
|
|
|
is accessed via a remote network API, some specialized data sources operate
|
|
|
|
only within Terraform itself, calculating some results and exposing them
|
|
|
|
for use elsewhere.
|
|
|
|
|
|
|
|
For example, local-only data sources exist for
|
|
|
|
[rendering templates](/docs/providers/template/d/template_file.html),
|
|
|
|
[reading local files](/docs/providers/local/d/file.html), and
|
|
|
|
[rendering AWS IAM policies](/docs/providers/aws/d/iam_policy_document.html).
|
|
|
|
|
|
|
|
The behavior of local-only data sources is the same as all other data
|
|
|
|
sources, but their result data exists only temporarily during a Terraform
|
|
|
|
operation, and is re-calulated each time a new plan is created.
|
|
|
|
|
|
|
|
## Data Resource Dependencies
|
|
|
|
|
|
|
|
Data resources have the same dependency resolution behavior
|
2018-12-11 01:14:33 +01:00
|
|
|
[as defined for managed resources](./resources.html#resource-dependencies).
|
2018-05-06 06:27:07 +02:00
|
|
|
|
|
|
|
In particular, the `depends_on` meta-argument is also available within `data`
|
|
|
|
blocks, with the same meaning and syntax as in `resource` blocks.
|
|
|
|
|
|
|
|
However, 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.
|
|
|
|
|
|
|
|
## Multiple Resource Instances
|
|
|
|
|
2018-12-11 01:14:33 +01:00
|
|
|
Data resources support [the `count` meta-argument](./resources.html#count-multiple-resource-instances)
|
2018-05-06 06:27:07 +02:00
|
|
|
as defined for managed resources, with the same syntax and behavior.
|
|
|
|
|
|
|
|
As with managed resources, when `count` is present it is important to
|
|
|
|
distinguish the resource itself from the multiple resource _instances_ it
|
|
|
|
creates. Each instance will separately read from its data source with its
|
|
|
|
own variant of the constraint arguments, producing an indexed result.
|
|
|
|
|
|
|
|
## Selecting a Non-default Provider Configuration
|
|
|
|
|
2018-12-11 01:14:33 +01:00
|
|
|
Data resources support [the `providers` meta-argument](./resources.html#provider-selecting-a-non-default-provider-configuration)
|
2018-05-06 06:27:07 +02:00
|
|
|
as defined for managed resources, with the same syntax and behavior.
|
|
|
|
|
|
|
|
## Lifecycle Customizations
|
|
|
|
|
|
|
|
Data resources do not currently have any customization settings available
|
|
|
|
for their lifecycle, but the `lifecycle` nested block is reserved in case
|
|
|
|
any are added in future versions.
|
2016-05-02 01:41:16 +02:00
|
|
|
|
|
|
|
## Example
|
|
|
|
|
|
|
|
A data source configuration looks like the following:
|
|
|
|
|
2017-04-05 17:29:27 +02:00
|
|
|
```hcl
|
|
|
|
# Find the latest available AMI that is tagged with Component = web
|
2016-05-02 01:41:16 +02:00
|
|
|
data "aws_ami" "web" {
|
2016-09-21 10:31:44 +02:00
|
|
|
filter {
|
2017-04-05 17:29:27 +02:00
|
|
|
name = "state"
|
2016-09-21 10:31:44 +02:00
|
|
|
values = ["available"]
|
2016-05-02 01:41:16 +02:00
|
|
|
}
|
2017-04-05 17:29:27 +02:00
|
|
|
|
2016-09-21 10:31:44 +02:00
|
|
|
filter {
|
2017-04-05 17:29:27 +02:00
|
|
|
name = "tag:Component"
|
2016-09-21 10:31:44 +02:00
|
|
|
values = ["web"]
|
|
|
|
}
|
2017-04-05 17:29:27 +02:00
|
|
|
|
2016-09-21 10:31:44 +02:00
|
|
|
most_recent = true
|
2016-05-02 01:41:16 +02:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
## Description
|
|
|
|
|
2018-12-11 01:14:33 +01:00
|
|
|
The `data` block creates a data instance of the given _type_ (first
|
|
|
|
block label) and _name_ (second block label). The combination of the type
|
2016-05-02 01:41:16 +02:00
|
|
|
and name must be unique.
|
|
|
|
|
|
|
|
Within the block (the `{ }`) is configuration for the data instance. The
|
|
|
|
configuration is dependent on the type, and is documented for each
|
|
|
|
data source in the [providers section](/docs/providers/index.html).
|
|
|
|
|
|
|
|
Each data instance will export one or more attributes, which can be
|
2018-12-11 01:14:33 +01:00
|
|
|
used in other resources as reference expressions of the form
|
|
|
|
`data.<TYPE>.<NAME>.<ATTRIBUTE>`. For example:
|
2016-05-02 01:41:16 +02:00
|
|
|
|
2017-04-05 17:29:27 +02:00
|
|
|
```hcl
|
2016-05-02 01:41:16 +02:00
|
|
|
resource "aws_instance" "web" {
|
2018-12-11 01:14:33 +01:00
|
|
|
ami = data.aws_ami.web.id
|
2017-04-05 17:29:27 +02:00
|
|
|
instance_type = "t1.micro"
|
2016-05-02 01:41:16 +02:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2018-12-11 01:14:33 +01:00
|
|
|
## Meta-Arguments
|
2017-05-02 16:05:50 +02:00
|
|
|
|
2018-12-11 01:14:33 +01:00
|
|
|
As data sources are essentially a read only subset of resources, they also
|
|
|
|
support the same [meta-arguments](./resources.html#meta-arguments) of resources
|
|
|
|
with the exception of the
|
|
|
|
[`lifecycle` configuration block](./resources.html#lifecycle-lifecycle-customizations).
|
2017-05-02 16:05:50 +02:00
|
|
|
|
2018-12-11 01:14:33 +01:00
|
|
|
### Multiple Provider Instances
|
2016-05-02 01:41:16 +02:00
|
|
|
|
2018-12-11 01:14:33 +01:00
|
|
|
Similarly to [resources](./resources.html), the
|
|
|
|
`provider` meta-argument can be used where a configuration has
|
2016-05-02 01:41:16 +02:00
|
|
|
multiple aliased instances of the same provider:
|
|
|
|
|
2017-04-05 17:29:27 +02:00
|
|
|
```hcl
|
2016-05-02 01:41:16 +02:00
|
|
|
data "aws_ami" "web" {
|
|
|
|
provider = "aws.west"
|
|
|
|
|
2017-04-05 17:29:27 +02:00
|
|
|
# ...
|
2016-05-02 01:41:16 +02:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2018-12-11 01:14:33 +01:00
|
|
|
See [Resources: Multiple Provider Instances](./resources.html#provider-selecting-a-non-default-provider-configuration)
|
2016-05-02 01:41:16 +02:00
|
|
|
for more information.
|
|
|
|
|
2018-12-11 01:14:33 +01:00
|
|
|
### Data Source Lifecycle
|
2016-05-02 01:41:16 +02:00
|
|
|
|
|
|
|
If the arguments of a data instance contain no references to computed values,
|
|
|
|
such as attributes of resources that have not yet been created, then the
|
|
|
|
data instance will be read and its state updated during Terraform's "refresh"
|
|
|
|
phase, which by default runs prior to creating a plan. This ensures that the
|
|
|
|
retrieved data is available for use during planning and the diff will show
|
|
|
|
the real values obtained.
|
|
|
|
|
|
|
|
Data instance arguments may refer to computed values, in which case the
|
|
|
|
attributes of the instance itself cannot be resolved until all of its
|
|
|
|
arguments are defined. In this case, refreshing the data instance will be
|
|
|
|
deferred until the "apply" phase, and all interpolations of the data instance
|
|
|
|
attributes will show as "computed" in the plan since the values are not yet
|
|
|
|
known.
|