Skip to main content
This lesson shows how to use Packer to build a reusable Amazon Machine Image (AMI) that includes the Vault binary and example configuration files. You will learn how to:
  • Upload local Vault artifacts to a temporary EC2 instance that Packer launches.
  • (Optionally) automate installation steps with a shell provisioner.
  • Build and launch instances from the resulting AMI.
A screenshot of a GitHub repository page for a HashiCorp-related project, showing folder entries (consul, general, vault), a README section, and repository controls like Code, Issues, and Pull requests. The right side shows repo metadata such as stars, forks, and an About blurb.
Repository contents
  • The example repository contains materials for Consul, Vault, and a Packer directory with the template and supporting files.
  • The Packer directory (used in this lesson) includes:
    • A Packer HCL template (vault.pkr.hcl)
    • A local Vault ZIP (e.g., vault_1.7.1_linux_amd64.zip)
    • Example Vault HCL configuration files and a systemd unit file under files/
Files referenced in the Packer template Why use Packer for Vault AMIs?
  • Create a reproducible AMI that already contains the Vault binary and example configs.
  • Speed up provisioning by avoiding manual install steps on every instance.
  • Optionally include installation and configuration as part of the Packer build.
Packer HCL template (cleaned example) Below is a consolidated example of the Packer HCL (vault.pkr.hcl) used in this lesson. It uploads local files to the temporary EC2 instance Packer launches. You can extend it with a provisioner "shell" to automate the unzip/install steps.
You can override variables (region, VPC, subnet, vault_zip) by editing the defaults in the HCL file, setting environment variables, or passing variables at runtime with the Packer CLI.
Prerequisites
  • Packer installed locally (https://www.packer.io/).
  • AWS credentials configured (AWS CLI or environment variables).
  • The Vault ZIP downloaded locally from HashiCorp releases.
  • Target VPC and Subnet IDs where Packer should launch the temporary instance.
Preparing variables
  • Set vpc_id and subnet_id to the target network where Packer will launch the temporary instance. You can get these IDs from the AWS VPC and Subnets console.
  • Either set values directly in vault.pkr.hcl, export environment variables, or pass them with -var flags when running packer build.
Validate and build
  • Validate the template:
  • Build the AMI:
Sample Packer build output This cleaned sample output highlights the important steps Packer performs during the build.
Launching an instance from the new AMI
  • After the build completes, go to the EC2 -> AMIs console and find the new AMI.
  • Launch a new instance from that AMI (for testing, a t2.micro or similar is fine).
  • Attach an SSH key pair, choose a security group that permits your SSH source IP, and enable public IP if you want direct SSH access.
A screenshot of the AWS EC2 Launch Instance wizard showing "Step 3: Configure Instance Details" with fields for network, subnet, auto-assign public IP, IAM role, and other instance settings. The page is open in a browser window over a code editor UI.
Rename the instance in the EC2 console for clarity.
A screenshot of the AWS EC2 Management Console showing a list of EC2 instances with one instance selected and an "Edit Name" dialog open. The instance details pane is visible at the bottom, showing status and networking information.
Verify uploaded files and install Vault on the running instance
  1. SSH into the instance (example user: ec2-user).
  2. Check /tmp to confirm Packer uploaded the Vault ZIP and files:
  1. Unzip the Vault binary and move it into a system path:
  1. Verify the binary and version:
Install configuration and service unit (manual example)
  • If you uploaded vault.service and configuration files, move them to the proper directories and enable the systemd service:
Automating installation inside Packer (optional)
  • To automate unzip + install during the Packer build, add a provisioner "shell" block to the build section that runs the commands above. This produces an AMI that is ready-to-run without manual post-launch steps.
Wrap-up and best practices
  • Building a Vault AMI with Packer creates a reproducible, versioned image that speeds up deployments.
  • Consider automating configuration templating and secrets injection at launch (for example, using cloud-init, user data, or a configuration management tool) rather than baking secrets into AMIs.
  • Keep the Vault binary up to date in your build pipeline and version AMIs to make rollbacks easier.
Links and references Further reading
  • Automating Packer builds in CI/CD to publish Vault AMIs on each release.
  • Using Packer post-processors to share AMIs across regions or accounts.

Watch Video