github.com/iaas-resource-provision/iaas-rpc@v1.0.7-0.20211021023331-ed21f798c408/website/docs/language/data-sources/index.html.md (about) 1 --- 2 layout: "language" 3 page_title: "Data Sources - Configuration Language" 4 sidebar_current: "docs-config-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 _Data sources_ allow Terraform 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/iaas-rpc.dirata-sources?in=terraform/configuration-language&utm_source=WEBSITE&utm_medium=WEB_IO&utm_offer=ARTICLE_PAGE&utm_content=DOCS) tutorial on HashiCorp Learn. 15 16 Each [provider](/docs/language/providers/index.html) may offer data sources 17 alongside its set of [resource](/docs/language/resources/index.html) 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](/docs/language/providers/index.html), 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](/docs/language/expressions/index.html) 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 If the query constraint arguments for a data resource refer only to constant 82 values or values that are already known, the data resource will be read and its 83 state updated during Terraform's "refresh" phase, which runs prior to creating a plan. 84 This ensures that the retrieved data is available for use during planning and 85 so Terraform's plan will show the actual values obtained. 86 87 Query constraint arguments may refer to values that cannot be determined until 88 after configuration is applied, such as the id of a managed resource that has 89 not been created yet. In this case, reading from the data source is deferred 90 until the apply phase, and any references to the results of the data resource 91 elsewhere in configuration will themselves be unknown until after the 92 configuration has been applied. 93 94 ## Local-only Data Sources 95 96 While many data sources correspond to an infrastructure object type that 97 is accessed via a remote network API, some specialized data sources operate 98 only within Terraform itself, calculating some results and exposing them 99 for use elsewhere. 100 101 For example, local-only data sources exist for 102 [rendering templates](https://registry.terraform.io/providers/hashicorp/template/latest/docs/data-sources/file), 103 [reading local files](https://registry.terraform.io/providers/hashicorp/local/latest/docs/data-sources/file), and 104 [rendering AWS IAM policies](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document). 105 106 The behavior of local-only data sources is the same as all other data 107 sources, but their result data exists only temporarily during a Terraform 108 operation, and is re-calculated each time a new plan is created. 109 110 ## Data Resource Dependencies 111 112 Data resources have the same dependency resolution behavior 113 [as defined for managed resources](/docs/language/resources/behavior.html#resource-dependencies). 114 Setting the `depends_on` meta-argument within `data` blocks defers reading of 115 the data source until after all changes to the dependencies have been applied. 116 117 In order to ensure that data sources are accessing the most up to date 118 information possible in a wide variety of use cases, arguments directly 119 referencing managed resources are treated the same as if the resource was 120 listed in `depends_on`. This behavior can be avoided when desired by indirectly 121 referencing the managed resource values through a `local` value. 122 123 ~> **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. 124 125 126 ## Multiple Resource Instances 127 128 Data resources support [`count`](/docs/language/meta-arguments/count.html) 129 and [`for_each`](/docs/language/meta-arguments/for_each.html) 130 meta-arguments as defined for managed resources, with the same syntax and behavior. 131 132 As with managed resources, when `count` or `for_each` is present it is important to 133 distinguish the resource itself from the multiple resource _instances_ it 134 creates. Each instance will separately read from its data source with its 135 own variant of the constraint arguments, producing an indexed result. 136 137 ## Selecting a Non-default Provider Configuration 138 139 Data resources support [the `provider` meta-argument](/docs/language/meta-arguments/resource-provider.html) 140 as defined for managed resources, with the same syntax and behavior. 141 142 ## Lifecycle Customizations 143 144 Data resources do not currently have any customization settings available 145 for their lifecycle, but the `lifecycle` nested block is reserved in case 146 any are added in future versions. 147 148 ## Example 149 150 A data source configuration looks like the following: 151 152 ```hcl 153 # Find the latest available AMI that is tagged with Component = web 154 data "aws_ami" "web" { 155 filter { 156 name = "state" 157 values = ["available"] 158 } 159 160 filter { 161 name = "tag:Component" 162 values = ["web"] 163 } 164 165 most_recent = true 166 } 167 ``` 168 169 ## Description 170 171 The `data` block creates a data instance of the given _type_ (first 172 block label) and _name_ (second block label). The combination of the type 173 and name must be unique. 174 175 Within the block (the `{ }`) is configuration for the data instance. The 176 configuration is dependent on the type; as with 177 [resources](/docs/language/resources/index.html), each provider on the 178 [Terraform Registry](https://registry.terraform.io/browse/providers) has its own 179 documentation for configuring and using the data types it provides. 180 181 Each data instance will export one or more attributes, which can be 182 used in other resources as reference expressions of the form 183 `data.<TYPE>.<NAME>.<ATTRIBUTE>`. For example: 184 185 ```hcl 186 resource "aws_instance" "web" { 187 ami = data.aws_ami.web.id 188 instance_type = "t1.micro" 189 } 190 ``` 191 192 ## Meta-Arguments 193 194 As data sources are essentially a read only subset of resources, they also 195 support the same [meta-arguments](/docs/language/resources/syntax.html#meta-arguments) of resources 196 with the exception of the 197 [`lifecycle` configuration block](/docs/language/meta-arguments/lifecycle.html). 198 199 ### Non-Default Provider Configurations 200 201 Similarly to [resources](/docs/language/resources/index.html), when 202 a module has multiple configurations for the same provider you can specify which 203 configuration to use with the `provider` meta-argument: 204 205 ```hcl 206 data "aws_ami" "web" { 207 provider = aws.west 208 209 # ... 210 } 211 ``` 212 213 See 214 [The Resource `provider` Meta-Argument](/docs/language/meta-arguments/resource-provider.html) 215 for more information. 216 217 ## Data Source Lifecycle 218 219 If the arguments of a data instance contain no references to computed values, 220 such as attributes of resources that have not yet been created, then the 221 data instance will be read and its state updated during Terraform's "refresh" 222 phase, which by default runs prior to creating a plan. This ensures that the 223 retrieved data is available for use during planning and the diff will show 224 the real values obtained. 225 226 Data instance arguments may refer to computed values, in which case the 227 attributes of the instance itself cannot be resolved until all of its 228 arguments are defined. In this case, refreshing the data instance will be 229 deferred until the "apply" phase, and all interpolations of the data instance 230 attributes will show as "computed" in the plan since the values are not yet 231 known.