Definition:

  • fetches data about a resource from the provider without provisioning an associated infrastructure object

data block

  •     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>"
          }
        }
      }

terraform_remote_state:

  • get data from remote/local Terraform State
  • only root-level output values from remote state are exposed
  • Better to use other data block if possible
  •     data "terraform_remote_state" "vpc" {
        backend = "remote"
      
        config = {
          organization = "hashicorp"
          workspaces = {
            name = "vpc-prod"
          }
        }
      }
      
      # Terraform >= 0.12
      resource "aws_instance" "foo" {
        # ...
        subnet_id = data.terraform_remote_state.vpc.outputs.subnet_id
      }