github.com/hashicorp/packer@v1.14.3/website/content/docs/templates/hcl_templates/datasources.mdx (about)

     1  ---
     2  page_title: Data Sources
     3  description: >-
     4    A `data` block defines a data source that instructs Packer to query data defined outside of Packer for use in builds and sources. Learn how to configure `data` blocks to define data sources.
     5  ---
     6  
     7  # Data sources reference
     8  
     9  This topic describes how to use the `data` block to configure data sources in your HCL Packer templates. The `data` block instructs Packer to fetch or compute data for use in [`locals` blocks](/packer/docs/templates/hcl_templates/blocks/locals) and
    10  [`source` blocks](/packer/docs/templates/hcl_templates/blocks/source) so that builders can use of information defined outside of Packer.
    11  
    12  ## Using data sources
    13  
    14  A data source is declared using a data block, and the configuration looks like the following:
    15  
    16  ```hcl
    17  data "amazon-ami" "example" {
    18      filters = {
    19          virtualization-type = "hvm"
    20          name = "ubuntu/images/*ubuntu-xenial-16.04-amd64-server-*"
    21          root-device-type = "ebs"
    22      }
    23      owners = ["099720109477"]
    24      most_recent = true
    25  }
    26  ```
    27  
    28  A `data` block requests that Packer read from a given data source (`"amazon-ami"`) and export the result under the given
    29  local name (`"example"`). The name is used to refer to this data source from elsewhere in the same Packer configuration.
    30  
    31  The data block creates a data instance of the given _type_ (first block label) and _name_ (second block label).
    32  The combination of the type and name must be unique within a configuration.
    33  
    34  Within the block (the `{ }`) is the configuration for the data instance. The configuration is dependent on the data source type,
    35  and is documented for each data source. For example the configuration of the `amazon-ami` data source can be found at [plugins/datasources/amazon/ami](/packer/plugins/datasources/amazon/ami#configuration-reference).
    36  
    37  A data source can output one or more attributes, which can be used by adding their key name to the data source unique
    38  identifier, like `data.<TYPE>.<NAME>.<ATTRIBUTE>`.
    39  
    40  The output from the `amazon-ami.example` above can be accessed as follows:
    41  
    42  Output data:
    43  
    44  ```
    45  "data.amazon-ami.example" {
    46    id = "ami12345"
    47    name = "MyAMI"
    48    creation_date = "01/01/2021"
    49    owner = "123456789"
    50    owner_name = "Some Name"
    51    tags = {"tag1": "value"}
    52  }
    53  ```
    54  
    55  Usage:
    56  
    57  ```hcl
    58  // in a local
    59  locals {
    60    source_ami_id = data.amazon-ami.example.id
    61    source_ami_name = data.amazon-ami.example.name
    62   }
    63  ```
    64  
    65  ```hcl
    66  // in a source
    67  source "amazon-ebs" "basic-example" {
    68    source_ami = local.source_ami
    69    // ...
    70  }
    71  ```
    72  
    73  ## Known limitations
    74  `@include 'datasources/local-dependency-limitation.mdx'`
    75  
    76  
    77  ## Related
    78  
    79  - The list of available data sources can be found in the [data sources](/packer/docs/datasources)
    80    section.
    81  - Create your own [custom data source](/packer/docs/plugins/creation/custom-datasources) !