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

     1  ---
     2  page_title: HCL templates overview
     3  description: |-
     4    HCL templates are configuration files that describe infrastructure and set variables using HashiCorp configuration language (HCL). Learn about HCL templates for Packer.
     5  ---
     6  
     7  # HCL templates overview
     8  
     9  This topic provides overview information about HashiCorp configuration language (HCL) templates for Packer.
    10  
    11  ## Introduction
    12  
    13  Packer reads and applies configurations defined in HCL template files. HCL templates provide concise descriptions of the required steps to get to a build file. You can add arguments, blocks, and expressions to your HCL templates to define your build as code. Refer to [Introduction to Packer HCL2](/packer/guides/hcl) for additional information.
    14  
    15  ## Builds
    16  
    17  The main purpose of the HCL language is defining builds and sources. All other
    18  language features exist only to make the definition of builds more flexible and
    19  convenient.
    20  
    21  `packer build` takes one argument. When a directory is passed, all files in the
    22  folder with a name ending with `.pkr.hcl` or `.pkr.json` will be parsed using
    23  the HCL2 format. When a file ending with `.pkr.hcl` or `.pkr.json` is passed it
    24  will be parsed using the HCL2 schema. For every other case; the _JSON only_ old
    25  packer schema will be used.
    26  
    27  ## Arguments, blocks, and expressions
    28  
    29  The syntax of the HCL language consists of only a few basic elements:
    30  
    31  ```hcl
    32  source "amazon-ebs" "main" {
    33    ami_name = "main-ami"
    34  }
    35  
    36  <BLOCK TYPE> "<BLOCK LABEL>" "<BLOCK LABEL>" {
    37    # Block body
    38    <IDENTIFIER> = <EXPRESSION> # Argument
    39  }
    40  ```
    41  
    42  - _Blocks_ are containers for other content and usually represent the
    43    configuration of some kind of object, like a source. Blocks have a
    44    _block type,_ can have zero or more _labels,_ and have a _body_ that contains
    45    any number of arguments and nested blocks. Most of Packer's features are
    46    controlled by top-level blocks in a configuration file.
    47  - _Arguments_ assign a value to a name. They appear within blocks.
    48  - _Expressions_ represent a value, either literally or by referencing and
    49    combining other values. They appear as values for arguments, or within other
    50    expressions.
    51  
    52  For full details about Packer's syntax, see:
    53  
    54  - [Configuration Syntax](/packer/docs/templates/hcl_templates/syntax)
    55  - [Expressions](/packer/docs/templates/hcl_templates/expressions)
    56  
    57  ## Code organization
    58  
    59  The HCL language uses configuration files that are named with the `.pkr.hcl`
    60  file extension. There is also [a JSON-based variant of the
    61  language](/packer/docs/templates/hcl_templates/syntax-json) that is named with the `.pkr.json` file
    62  extension.
    63  
    64  Configuration files must always use UTF-8 encoding, and by convention are
    65  usually maintained with Unix-style line endings (LF) rather than Windows-style
    66  line endings (CRLF), though both are accepted.
    67  
    68  ## Configuration ordering
    69  
    70  The ordering of root blocks is not significant. The order of `provisioner` or
    71  `post-processor` blocks within a `build` is the only major feature where block
    72  order matters.