github.com/hugorut/terraform@v1.1.3/website/docs/language/configuration-0-11/data-sources.mdx (about) 1 --- 2 page_title: Data Sources - 0.11 Configuration Language 3 description: >- 4 Data sources allow data to be fetched or computed for use elsewhere in 5 Terraform configuration. 6 --- 7 8 # Data Sources 9 10 -> **Note:** This page is about Terraform 0.11 and earlier. For Terraform 0.12 11 and later, see 12 [Configuration Language: Configuring Data Sources](/language/data-sources). 13 14 _Data sources_ allow data to be fetched or computed for use elsewhere 15 in Terraform configuration. Use of data sources allows a Terraform 16 configuration to build on information defined outside of Terraform, 17 or defined by another separate Terraform configuration. 18 19 [Providers](/language/configuration-0-11/providers) are responsible in 20 Terraform for defining and implementing data sources. Whereas 21 a [resource](/language/configuration-0-11/resources) causes Terraform 22 to create and manage a new infrastructure component, data sources 23 present read-only views into pre-existing data, or they compute 24 new values on the fly within Terraform itself. 25 26 For example, a data source may retrieve remote state data from a 27 Terraform Cloud workspace, configuration information from Consul, or look up a pre-existing 28 AWS resource by filtering on its attributes and tags. 29 30 Every data source in Terraform is mapped to a provider based 31 on longest-prefix matching. For example the `aws_ami` 32 data source would map to the `aws` provider (if that exists). 33 34 This page assumes you're familiar with the 35 [configuration syntax](/language/configuration-0-11/syntax) 36 already. 37 38 ## Example 39 40 A data source configuration looks like the following: 41 42 ```hcl 43 # Find the latest available AMI that is tagged with Component = web 44 data "aws_ami" "web" { 45 filter { 46 name = "state" 47 values = ["available"] 48 } 49 50 filter { 51 name = "tag:Component" 52 values = ["web"] 53 } 54 55 most_recent = true 56 } 57 ``` 58 59 ## Description 60 61 The `data` block creates a data instance of the given `TYPE` (first 62 parameter) and `NAME` (second parameter). The combination of the type 63 and name must be unique. 64 65 Within the block (the `{ }`) is configuration for the data instance. The 66 configuration is dependent on the type; consult the [provider's documentation](https://registry.terraform.io/browse/providers) for 67 details. 68 69 Each data instance will export one or more attributes, which can be 70 interpolated into other resources using variables of the form 71 `data.TYPE.NAME.ATTR`. For example: 72 73 ```hcl 74 resource "aws_instance" "web" { 75 ami = "${data.aws_ami.web.id}" 76 instance_type = "t1.micro" 77 } 78 ``` 79 80 ### Meta-parameters 81 82 As data sources are essentially a read only subset of resources they also support the same [meta-parameters](/language/configuration-0-11/resources#meta-parameters) of resources except for the [`lifecycle` configuration block](/language/configuration-0-11/resources#lifecycle). 83 84 ## Multiple Provider Instances 85 86 Similarly to [resources](/language/configuration-0-11/resources), the 87 `provider` meta-parameter can be used where a configuration has 88 multiple aliased instances of the same provider: 89 90 ```hcl 91 data "aws_ami" "web" { 92 provider = "aws.west" 93 94 # ... 95 } 96 ``` 97 98 See the ["Multiple Provider Instances"](/language/configuration-0-11/resources#multiple-provider-instances) documentation for resources 99 for more information. 100 101 ## Data Source Lifecycle 102 103 If the arguments of a data instance contain no references to computed values, 104 such as attributes of resources that have not yet been created, then the 105 data instance will be read and its state updated during Terraform's "refresh" 106 phase, which by default runs prior to creating a plan. This ensures that the 107 retrieved data is available for use during planning and the diff will show 108 the real values obtained. 109 110 Data instance arguments may refer to computed values, in which case the 111 attributes of the instance itself cannot be resolved until all of its 112 arguments are defined. In this case, refreshing the data instance will be 113 deferred until the "apply" phase, and all interpolations of the data instance 114 attributes will show as "computed" in the plan since the values are not yet 115 known.