»Built-in Blocks

The Packer - HCL2 language includes a number of built-in blocks that you can use. A block is a container for configuration.

Blocks can be defined in multiple files and packer build folder will build using solely the files from a directory named folder.

Packer does not support user-defined blocks and so only the blocks built in to the language are available for use. The navigation for this section includes a list of all of the available built-in HCL2 blocks.

»Config example:

# variables.pkr.hcl
variable "foo" {
    type = string
    default = "the default value of the `foo` variable"
    description = "description of the `foo` variable"
}
# locals.pkr.hcl
locals {
    # locals can be bare values like:
    wee = local.baz
    # locals can also be set with other variables :
    baz = "Foo is '${var.foo}' but not '${local.wee}'"
}
# sources.pkr.hcl
source "amazon-ebs" "example-1" {
    // ...
}
# build.pkr.hcl
build {
    # use the `name` field to name a build in the logs.
    # For example this present config will display 
    # "buildname.amazon-ebs.example-1" and "buildname.amazon-ebs.example-2"
    name = "buildname"

    sources = [
        # use the optional plural `sources` list to simply use a `source`
        # without changing any field.
        "source.amazon-ebs.example-1",
    ]

    source "source.amazon-ebs.example-2" {
        # Use the singular `source` block set specific fields.
        # Note that fields cannot be overwritten, in other words, you cannot 
        # set the 'output' field from the top-level source block and here.
        output = "different value"
        name = var.foo
    }

    provisioner "shell" {
        scripts = fileset(".", "scripts/{install,secure}.sh")
    }

    post-processor "shell-local" {
        inline = ["echo Hello World"]
    }
}