github.com/hugorut/terraform@v1.1.3/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 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](/language/resources/behavior#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 ## Multiple Resource Instances 126 127 Data resources support [`count`](/language/meta-arguments/count) 128 and [`for_each`](/language/meta-arguments/for_each) 129 meta-arguments as defined for managed resources, with the same syntax and behavior. 130 131 As with managed resources, when `count` or `for_each` is present it is important to 132 distinguish the resource itself from the multiple resource _instances_ it 133 creates. Each instance will separately read from its data source with its 134 own variant of the constraint arguments, producing an indexed result. 135 136 ## Selecting a Non-default Provider Configuration 137 138 Data resources support [the `provider` meta-argument](/language/meta-arguments/resource-provider) 139 as defined for managed resources, with the same syntax and behavior. 140 141 ## Lifecycle Customizations 142 143 Data resources do not currently have any customization settings available 144 for their lifecycle, but the `lifecycle` nested block is reserved in case 145 any are added in future versions. 146 147 ## Example 148 149 A data source configuration looks like the following: 150 151 ```hcl 152 # Find the latest available AMI that is tagged with Component = web 153 data "aws_ami" "web" { 154 filter { 155 name = "state" 156 values = ["available"] 157 } 158 159 filter { 160 name = "tag:Component" 161 values = ["web"] 162 } 163 164 most_recent = true 165 } 166 ``` 167 168 ## Description 169 170 The `data` block creates a data instance of the given _type_ (first 171 block label) and _name_ (second block label). The combination of the type 172 and name must be unique. 173 174 Within the block (the `{ }`) is configuration for the data instance. The 175 configuration is dependent on the type; as with 176 [resources](/language/resources), each provider on the 177 [Terraform Registry](https://registry.terraform.io/browse/providers) has its own 178 documentation for configuring and using the data types it provides. 179 180 Each data instance will export one or more attributes, which can be 181 used in other resources as reference expressions of the form 182 `data.<TYPE>.<NAME>.<ATTRIBUTE>`. For example: 183 184 ```hcl 185 resource "aws_instance" "web" { 186 ami = data.aws_ami.web.id 187 instance_type = "t1.micro" 188 } 189 ``` 190 191 ## Meta-Arguments 192 193 As data sources are essentially a read only subset of resources, they also 194 support the same [meta-arguments](/language/resources/syntax#meta-arguments) of resources 195 with the exception of the 196 [`lifecycle` configuration block](/language/meta-arguments/lifecycle). 197 198 ### Non-Default Provider Configurations 199 200 Similarly to [resources](/language/resources), when 201 a module has multiple configurations for the same provider you can specify which 202 configuration to use with the `provider` meta-argument: 203 204 ```hcl 205 data "aws_ami" "web" { 206 provider = aws.west 207 208 # ... 209 } 210 ``` 211 212 See 213 [The Resource `provider` Meta-Argument](/language/meta-arguments/resource-provider) 214 for more information. 215 216 ## Data Source Lifecycle 217 218 If the arguments of a data instance contain no references to computed values, 219 such as attributes of resources that have not yet been created, then the 220 data instance will be read and its state updated during Terraform's "refresh" 221 phase, which by default runs prior to creating a plan. This ensures that the 222 retrieved data is available for use during planning and the diff will show 223 the real values obtained. 224 225 Data instance arguments may refer to computed values, in which case the 226 attributes of the instance itself cannot be resolved until all of its 227 arguments are defined. In this case, refreshing the data instance will be 228 deferred until the "apply" phase, and all interpolations of the data instance 229 attributes will show as "computed" in the plan since the values are not yet 230 known.