Search for a command to run...
Detailed instructions for installing and using HashiCorp Packer, an open-source tool for creating identical machine images for multiple platforms from a single source configuration.
Packer is an open-source tool by HashiCorp that enables you to create identical machine images for multiple platforms from a single source configuration. It automates the creation of any type of machine image.
# Add HashiCorp GPG key
wget -O- https://apt.releases.hashicorp.com/gpg | sudo gpg --dearmor -o /usr/share/keyrings/hashicorp-archive-keyring.gpg
# Add HashiCorp repository
echo "deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] https://apt.releases.hashicorp.com $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/hashicorp.list
# Update and install Packer
sudo apt update
sudo apt install packer
# Check Packer version
packer version
# View available commands
packer -help
Create example.pkr.hcl
:
packer {
required_plugins {
amazon = {
version = ">= 1.0.0"
source = "github.com/hashicorp/amazon"
}
}
}
source "amazon-ebs" "ubuntu" {
ami_name = "learn-packer-linux-aws"
instance_type = "t2.micro"
region = "us-west-2"
source_ami_filter {
filters = {
name = "ubuntu/images/*ubuntu-focal-20.04-amd64-server-*"
root-device-type = "ebs"
virtualization-type = "hvm"
}
most_recent = true
owners = ["099720109477"]
}
ssh_username = "ubuntu"
}
build {
sources = [
"source.amazon-ebs.ubuntu"
]
}
# Initialize Packer configuration
packer init .
# Format configuration files
packer fmt .
# Validate template
packer validate .
# Build image
packer build .
# Build with variables
packer build -var 'aws_access_key=YOUR_KEY' .
# AWS credentials
export AWS_ACCESS_KEY_ID="your_access_key"
export AWS_SECRET_ACCESS_KEY="your_secret_key"
export AWS_DEFAULT_REGION="your_region"
# Azure credentials
export ARM_SUBSCRIPTION_ID="your_subscription_id"
export ARM_CLIENT_ID="your_client_id"
export ARM_CLIENT_SECRET="your_client_secret"
export ARM_TENANT_ID="your_tenant_id"
# Enable detailed logging
export PACKER_LOG=1
export PACKER_LOG_PATH="packer.log"
Template Organization
Security
Build Optimization
Authentication Errors
Build Failures
# Debug mode
packer build -debug .
# On error
export PACKER_LOG=1
packer build .
Network Issues
Plugin Issues
# Clean plugin cache
rm -rf ~/.packer.d/plugins
packer init -upgrade .
SSH Problems