Terraform
data block reference
The data block fetches data about a resource from the provider without provisioning an associated infrastructure object. You can reference data source attributes to configure other resources, keeping your configuration dynamic and preventing hardcoding. Refer to Query infrastructure data for more information.
The source type, arguments, and attributes depend on the provider. Refer to the provider documentation for more information.
Configuration model
The data block supports the following configuration:
- data "<TYPE>" "<LABEL>"block- provider-specific arguments block | refer to your provider documentation
- countnumber | mutually exclusive with- for_each
- depends_onlist of references
- for_eachmap or set of strings | mutually exclusive with- count
- providerreference
- lifecycleblock- preconditionblock- conditionstring
- error_messagestring
 
- postconditionblock- conditionstring
- error_messagestring
 
 
 
Complete configuration
The following data block includes all built-in arguments supported in a data source:
data "<TYPE>" "<LABEL>" {
   <PROVIDER-SPECIFIC ARGUMENTS>
   count = <NUMBER>      # `count` and `for_each` are mutually exclusive
   depends_on = [ <RESOURCE.ADDRESS.EXPRESSION> ]
   for_each = {          # `for_each` and `count` are mutually exclusive
      <KEY> = <VALUE>
   }
   for_each = [       # `for_each` accepts a map or a set of strings
    "<VALUE>",
    "<VALUE>"
   ]
   provider = <REFERENCE.TO.ALIAS>
   lifecycle {
      precondition {
         condition = <EXPRESSION>
         error_message = "<STRING>"
    }
    postcondition {
      condition = <EXPRESSION>
      error_message = "<STRING>"
    }
  }
}
Specification
A data block supports the following configuration.
data "<TYPE>" "<LABEL>"
You must set the following arguments for every data block:
- TYPE: Specifies the data source type. Provider developers define the support data sources. Refer to the provider documentation for details. Terraform also includes the- terraform_remote_statedata source, which lets you access state data from other workspaces.
- LABEL: Specifies a name for the data source. Use the- data.<label>.<attribute>syntax to reference the data. Refer to References to Named Values and Resource naming for expression syntax and label recommendations.
Provider-specific arguments
The provider developer determines which arguments you can define for a data source. Refer to the provider documentation for details.
count
The count meta-argument instructs Terraform to provision multiple instances of the same data source with identical or similar configuration. You cannot use both a count and  for_each argument in the same block.
data "<TYPE>" "<LABEL>" {
  count = <number>
}
count is a meta-argument. Meta-arguments are built into the Terraform language and control how Terraform creates resources. Refer to the count reference for details about how the argument works.
depends_on
The depends_on meta-argument specifies an upstream resource that the data source depends on. Terraform must complete all operations on the upstream resource before performing operations on the data source containing the depends_on argument.
data "<TYPE>" "<LABEL>" {
  depends_on = [ <resource reference> ]
}
depends_on is a meta-argument. Meta-arguments are built into the Terraform language and control how Terraform creates resources. Refer to the depends_on reference for details about how the argument works.
for_each
The for_each meta-argument instructs Terraform to provision similar resources without requiring separate configuration blocks for each resource.
data "<TYPE>" "<LABEL>" {
  for_each = [ "<VALUE>" ]
  # . . .
}
for_each is a meta-argument. Meta-arguments are built into the Terraform language and control how Terraform creates resources. Refer to the for_each reference for details about how the argument works.
provider
The provider argument instructs Terraform to use an alternate provider configuration to provision the nested data source.
data "<TYPE>" "<LABEL>" {
  provider = <provider>.<alias>
}
provider is a meta-argument. Meta-arguments are built into the Terraform language and control how Terraform creates resources. Refer to the provider reference for details about how the argument works.
lifecycle
The lifecycle block defines lifecycle rules for how Terraform operates on your data source.
data "<TYPE>" "<LABEL>" {
  lifecycle {
    <lifecycle>
  }
}
You can specify the following lifecycle rules to manage how Terraform performs operations on the resource:
- precondition: Specifies a condition that Terraform evaluates before creating the data source. Refer to Validate your configuration for more information.
- postcondition: Specifies a condition that Terraform evaluates after creating the data source. Refer to Validate your configuration for more information.
Configurations defined in the lifecycle block affect how Terraform constructs and traverses the dependency graph. You can only use literal values in the lifecycle block because Terraform processes them before it evaluates arbitrary expressions for a run.
lifecycle is a meta-argument. Meta-arguments are built into the Terraform language and control how Terraform creates resources. Refer to the lifecycle reference for details about how the argument works.
precondition
The precondition block specifies a condition that must return true before Terraform evaluates and performs operations on the data source. You can also specify an error message for Terraform to print when the condition returns false.
resource {
  lifecycle {
    precondition {
      condition = <expression>
      error_message = "<message>"
    }
  }
}
precondition is a directive available in the lifecycle meta-argument. Meta-arguments are built into the Terraform language and control how Terraform creates resources. Refer to the lifecycle reference for details about how the argument works.
postcondition
The postcondition block specifies a condition that must return true after Terraform performs operations on the data source. You can also specify an error message for Terraform to print to the console when the condition returns false.
data "<TYPE>" "<LABEL>" {
  lifecycle {
    postcondition {
      condition = <expression>
      error_message = "<message>"
    }
  }
}
postcondition is a directive available in the lifecycle meta-argument. Meta-arguments are built into the Terraform language and control how Terraform creates resources. Refer to the lifecycle reference for details about how the argument works.