github.com/ns1/terraform@v0.7.10-0.20161109153551-8949419bef40/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/resources.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 filter { 43 name = "state" 44 values = ["available"] 45 } 46 filter { 47 name = "tag:Component" 48 values = ["web"] 49 } 50 most_recent = true 51 } 52 ``` 53 54 ## Description 55 56 The `data` block creates a data instance of the given `TYPE` (first 57 parameter) and `NAME` (second parameter). The combination of the type 58 and name must be unique. 59 60 Within the block (the `{ }`) is configuration for the data instance. The 61 configuration is dependent on the type, and is documented for each 62 data source in the [providers section](/docs/providers/index.html). 63 64 Each data instance will export one or more attributes, which can be 65 interpolated into other resources using variables of the form 66 `data.TYPE.NAME.ATTR`. For example: 67 68 ``` 69 resource "aws_instance" "web" { 70 ami = "${data.aws_ami.web.id}" 71 instance_type = "t1.micro" 72 } 73 ``` 74 75 ## Multiple Provider Instances 76 77 Similarly to [resources](/docs/configuration/resources.html), the 78 `provider` meta-parameter can be used where a configuration has 79 multiple aliased instances of the same provider: 80 81 ``` 82 data "aws_ami" "web" { 83 provider = "aws.west" 84 85 // etc... 86 } 87 88 ``` 89 90 See the "Multiple Provider Instances" documentation for resources 91 for more information. 92 93 ## Data Source Lifecycle 94 95 If the arguments of a data instance contain no references to computed values, 96 such as attributes of resources that have not yet been created, then the 97 data instance will be read and its state updated during Terraform's "refresh" 98 phase, which by default runs prior to creating a plan. This ensures that the 99 retrieved data is available for use during planning and the diff will show 100 the real values obtained. 101 102 Data instance arguments may refer to computed values, in which case the 103 attributes of the instance itself cannot be resolved until all of its 104 arguments are defined. In this case, refreshing the data instance will be 105 deferred until the "apply" phase, and all interpolations of the data instance 106 attributes will show as "computed" in the plan since the values are not yet 107 known.