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