# hashicorp/packer architecture

> Architecture diagram of the GitHub repository hashicorp/packer, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-01). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/hashicorp/packer
- Repository: https://github.com/hashicorp/packer

## Overview

##### Main Components
a. CLI Frontend – Represented primarily by commands in the “cmd”, “command”, and “main.go” files. This is the entry point for users and orchestrates input parsing, configuration loading, and command dispatch.
b. Core Build Engine – Central logic found in files “packer/core.go”, “packer/build.go”, and related modules handling the overall build lifecycle (e.g., build orchestration, progress bar, logging, telemetry, etc.).
c. Plugin Architecture – A key element of Packer’s design. Organize the different plugin types as separate components/subsystems:
- Builder Plugins (in directories like “builder” and “packer/cmd_builder.go”): They generate machine images based on selected targets.
- Provisioners (in the “provisioner” directory): They carry out configuration and setup tasks on the images.
- Datasources (found in the “datasource” directory): They are used to fetch remote data or verify external resources.
- Post-processors (under “post-processor”): They handle post-build tasks, such as compressing images or generating manifests.
- Plugin Getter – Modules under “packer/plugin-getter” manage installation and version-checking for plugins.
d. HCP Integration – Modules under “internal/hcp” and corresponding datasources (like “datasource/hcp-packer-artifact”) support integration with HashiCorp Cloud Platform services.
e. Utility and Helper Layers – Several directories (“internal/dag”, “hcl2template”, “helper”) implement supporting functionality such as dependency graph resolution, HCL parsing/templating, and common utilities for the CLI.
f. Testing & Examples – The “acctest”, “packer_test”, and “examples” directories show a strong emphasis on automated integration tests and sample configurations; these represent development and validation layers.

##### Relationships and Interactions
– The CLI receives user commands and passes them to the core engine.
– The core engine reads configuration files (using HCL templates from “hcl2template”) and determines which plugins to activate based on the configuration.
– Through a well-defined plugin interface, the engine invokes builder, provisioner, datasource, and post-processor plugins. Each plugin type works independently yet cooperates through shared protocols.
– The plugin getter subsystem communicates with remote repositories (for example, GitHub integrations) to download or update plugins.
– External integrations such as HCP Packer use APIs defined under “internal/hcp” to connect and manage image lifecycle metadata.
– Underlying flows (like dependency resolution or build ordering) use DAG algorithms (see “internal/dag”) to guarantee proper execution order.

##### Architectural Patterns and Design Principles
– Modular and extensible design: Each subsystem is an independently maintained module that adheres to a defined plugin interface.
– Plugin-based architecture: Multiple “builders”, “provisioners”, “datasources”, and “post-processors” are designed to be easily integrated and swapped.
– Separation of concerns: The CLI, core engine, and various plugins are well‐separated to isolate responsibilities.
– Use of configuration-as-code: The project makes extensive use of HCL (HashiCorp Configuration Language), whose parsing and templating is managed in its own modules.
– Testing and integration: An extensive set of tests (both unit and acceptance) ensures that each component is correctly interacting with others.

## Diagram (Mermaid)

```mermaid
flowchart TD
    CLI["CLI Frontend (cmd, main.go)"]:::cli
    Engine["Core Build Engine (packer/core.go, packer/build.go)"]:::engine

    subgraph "Plugin Architecture"
        Builder["Builder Plugins (builder/, packer/cmd_builder.go)"]:::plugin
        Provisioner["Provisioner Plugins (provisioner/)"]:::plugin
        Datasource["Datasource Plugins (datasource/)"]:::plugin
        PostProcessor["Post-processor Plugins (post-processor/)"]:::plugin
        PluginGetter["Plugin Getter (packer/plugin-getter/)"]:::plugin
    end

    HCP["HCP Integration (internal/hcp/)"]:::external
    Utility["Utility & Helper Layers (internal/dag/, hcl2template/, helper/)"]:::utility
    Testing["Testing & Examples (acctest/, packer_test/, examples/)"]:::testing

    CLI -->|"dispatches"| Engine
    Engine -->|"invokes"| Builder
    Engine -->|"invokes"| Provisioner
    Engine -->|"invokes"| Datasource
    Engine -->|"invokes"| PostProcessor
    Engine -->|"invokes"| PluginGetter
    Engine -->|"uses"| Utility
    Engine -->|"integratesWith"| HCP
    CLI -->|"validatesWith"| Testing

    click CLI "https://github.com/hashicorp/packer/tree/main/cmd/"
    click Engine "https://github.com/hashicorp/packer/blob/main/packer/core.go"
    click Builder "https://github.com/hashicorp/packer/tree/main/builder/"
    click Provisioner "https://github.com/hashicorp/packer/tree/main/provisioner/"
    click Datasource "https://github.com/hashicorp/packer/tree/main/datasource/"
    click PostProcessor "https://github.com/hashicorp/packer/tree/main/post-processor/"
    click PluginGetter "https://github.com/hashicorp/packer/tree/main/packer/plugin-getter/"
    click HCP "https://github.com/hashicorp/packer/tree/main/internal/hcp/"
    click Utility "https://github.com/hashicorp/packer/tree/main/internal/dag/"
    click Testing "https://github.com/hashicorp/packer/tree/main/acctest/"

    classDef cli fill:#add8e6,stroke:#000,stroke-width:2px;
    classDef engine fill:#90ee90,stroke:#000,stroke-width:2px;
    classDef plugin fill:#fffacd,stroke:#000,stroke-width:2px;
    classDef external fill:#ffb6c1,stroke:#000,stroke-width:2px;
    classDef utility fill:#d3d3d3,stroke:#000,stroke-width:2px;
    classDef testing fill:#f08080,stroke:#000,stroke-width:2px;
```
