Difference between revisions of "Packer"

From Christoph's Personal Wiki
Jump to: navigation, search
(Created page with "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...")
(No difference)

Revision as of 23:53, 29 August 2018

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.

Example

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": [
          "099720109477"
        ],
        "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-17.03-ubuntu-16.04.sh"
    }
  ]
}
EOF
  • Docker install script:
$ cat << EOF >docker-install-17.03-ubuntu-16.04.sh
#!/bin/bash
# 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 https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu 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
EOF
  • Create Packer run script:
$ cat << EOF > packer-run.sh
#!/bin/bash

AWS_ACCESS_KEY="$1"
AWS_SECRET_KEY="$2"
JSON=$3
packer build \
  -var "aws_access_key=${AWS_ACCESS_KEY}" \
  -var "aws_secret_key=${AWS_SECRET_KEY}" \
  $3
  • Run Packer script (example):
$ bash packer-run.sh "<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.

External links