From Christoph's Personal Wiki
Revision as of 18:01, 29 April 2020 by Christoph (Talk | contribs) (External links)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

HashiCorp Packer is a tool to automate the creation of any type of machine image. It embraces modern configuration management by encouraging you to use automated scripts to install and configure the software within your Packer-made images. Packer brings machine images into the modern age, unlocking untapped potential and opening new opportunities.


Create an AWS AMI using Ubuntu 16.04 as a base image with Docker 17.03 pre-installed
  • Packer template:
$ cat << EOF > packer-docker-17.03-ubuntu-16.04.json
  "variables": {
    "aws_access_key": "",
    "aws_secret_key": "",
    "region": "us-west-2"
  "builders": [
      "type": "amazon-ebs",
      "access_key": "{{user `aws_access_key`}}",
      "secret_key": "{{user `aws_secret_key`}}",
      "region": "us-west-2",
      "source_ami_filter": {
        "filters": {
          "virtualization-type": "hvm",
          "name": "ubuntu/images/*ubuntu-xenial-16.04-amd64-server-*",
          "root-device-type": "ebs"
        "owners": [
        "most_recent": true
      "instance_type": "t2.micro",
      "ssh_username": "ubuntu",
      "ami_name": "packer-docker-17.03-ubuntu-16.04"
  "provisioners": [
      "type": "shell",
      "script": "./"
  • Docker install script:
$ cat << EOF >
# Install Docker 17.03.2 on Ubuntu 16.04
sudo apt-get install -y apt-transport-https ca-certificates curl software-properties-common
curl -fsSL | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] xenial stable"
sudo apt-get update -y
apt-cache madison docker-ce
sudo apt-get install -y docker-ce=17.03.2~ce-0~ubuntu-xenial
echo "docker-ce hold" | sudo dpkg --set-selections
sudo apt-get dselect-upgrade
sudo usermod -aG docker ubuntu
sudo systemctl enable docker
sudo systemctl restart docker
  • Create Packer run script:
$ cat << EOF >

packer build \
  -var "aws_access_key=${AWS_ACCESS_KEY}" \
  -var "aws_secret_key=${AWS_SECRET_KEY}" \
  • Run Packer script (example):
$ bash "<your_aws_access_key>" "<your_aws_secret_key>" packer-docker-17.03-ubuntu-16.04.json

If the above packer run completes successfully, you should see something like the following:

==> Builds finished. The artifacts of successful builds are:
--> amazon-ebs: AMIs were created:
us-west-2: ami-000000000000000

You can now use this Packer-created AMI to launch EC2 instances in AWS.

See also

External links