github.com/muratcelep/terraform@v1.1.0-beta2-not-internal-4/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: "Data sources allow Terraform to use external data, function output, and data from other configurations. Learn data resource arguments, behavior, and lifecycle." 6 --- 7 8 # Data Sources 9 10 _Data sources_ allow Terraform to use information defined outside of Terraform, 11 defined by another separate Terraform configuration, or modified by functions. 12 13 > **Hands-on:** Try the [Query Data Sources](https://learn.hashicorp.com/tutorials/terraform/data-sources) tutorial on HashiCorp Learn. 14 15 Each [provider](/docs/language/providers/index.html) may offer data sources 16 alongside its set of [resource](/docs/language/resources/index.html) 17 types. 18 19 ## Using Data Sources 20 21 A data source is accessed via a special kind of resource known as a 22 _data resource_, declared using a `data` block: 23 24 ```hcl 25 data "aws_ami" "example" { 26 most_recent = true 27 28 owners = ["self"] 29 tags = { 30 Name = "app-server" 31 Tested = "true" 32 } 33 } 34 ``` 35 36 A `data` block requests that Terraform read from a given data source ("aws_ami") 37 and export the result under the given local name ("example"). The name is used 38 to refer to this resource from elsewhere in the same Terraform module, but has 39 no significance outside of the scope of a module. 40 41 The data source and name together serve as an identifier for a given 42 resource and so must be unique within a module. 43 44 Within the block body (between `{` and `}`) are query constraints defined by 45 the data source. Most arguments in this section depend on the 46 data source, and indeed in this example `most_recent`, `owners` and `tags` are 47 all arguments defined specifically for [the `aws_ami` data source](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/ami). 48 49 When distinguishing from data resources, the primary kind of resource (as declared 50 by a `resource` block) is known as a _managed resource_. Both kinds of resources 51 take arguments and export attributes for use in configuration, but while 52 managed resources cause Terraform to create, update, and delete infrastructure 53 objects, data resources cause Terraform only to _read_ objects. For brevity, 54 managed resources are often referred to just as "resources" when the meaning 55 is clear from context. 56 57 ## Data Source Arguments 58 59 Each data resource is associated with a single data source, which determines 60 the kind of object (or objects) it reads and what query constraint arguments 61 are available. 62 63 Each data source in turn belongs to a [provider](/docs/language/providers/index.html), 64 which is a plugin for Terraform that offers a collection of resource types and 65 data sources that most often belong to a single cloud or on-premises 66 infrastructure platform. 67 68 Most of the items within the body of a `data` block are defined by and 69 specific to the selected data source, and these arguments can make full 70 use of [expressions](/docs/language/expressions/index.html) and other dynamic 71 Terraform language features. 72 73 However, there are some "meta-arguments" that are defined by Terraform itself 74 and apply across all data sources. These arguments often have additional 75 restrictions on what language features can be used with them, and are described 76 in more detail in the following sections. 77 78 ## Data Resource Behavior 79 80 If the query constraint arguments for a data resource refer only to constant 81 values or values that are already known, the data resource will be read and its 82 state updated during Terraform's "refresh" phase, which runs prior to creating a plan. 83 This ensures that the retrieved data is available for use during planning and 84 so Terraform's plan will show the actual values obtained. 85 86 Query constraint arguments may refer to values that cannot be determined until 87 after configuration is applied, such as the id of a managed resource that has 88 not been created yet. In this case, reading from the data source is deferred 89 until the apply phase, and any references to the results of the data resource 90 elsewhere in configuration will themselves be unknown until after the 91 configuration has been applied. 92 93 ## Local-only Data Sources 94 95 While many data sources correspond to an infrastructure object type that 96 is accessed via a remote network API, some specialized data sources operate 97 only within Terraform itself, calculating some results and exposing them 98 for use elsewhere. 99 100 For example, local-only data sources exist for 101 [rendering templates](https://registry.terraform.io/providers/hashicorp/template/latest/docs/data-sources/file), 102 [reading local files](https://registry.terraform.io/providers/hashicorp/local/latest/docs/data-sources/file), and 103 [rendering AWS IAM policies](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document). 104 105 The behavior of local-only data sources is the same as all other data 106 sources, but their result data exists only temporarily during a Terraform 107 operation, and is re-calculated each time a new plan is created. 108 109 ## Data Resource Dependencies 110 111 Data resources have the same dependency resolution behavior 112 [as defined for managed resources](/docs/language/resources/behavior.html#resource-dependencies). 113 Setting the `depends_on` meta-argument within `data` blocks defers reading of 114 the data source until after all changes to the dependencies have been applied. 115 116 In order to ensure that data sources are accessing the most up to date 117 information possible in a wide variety of use cases, arguments directly 118 referencing managed resources are treated the same as if the resource was 119 listed in `depends_on`. This behavior can be avoided when desired by indirectly 120 referencing the managed resource values through a `local` value. 121 122 ~> **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. 123 124 125 ## Multiple Resource Instances 126 127 Data resources support [`count`](/docs/language/meta-arguments/count.html) 128 and [`for_each`](/docs/language/meta-arguments/for_each.html) 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](/docs/language/meta-arguments/resource-provider.html) 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](/docs/language/resources/index.html), 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](/docs/language/resources/syntax.html#meta-arguments) of resources 195 with the exception of the 196 [`lifecycle` configuration block](/docs/language/meta-arguments/lifecycle.html). 197 198 ### Non-Default Provider Configurations 199 200 Similarly to [resources](/docs/language/resources/index.html), 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](/docs/language/meta-arguments/resource-provider.html) 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.