What is Packer?
Packer is a server imaging tool developed by HashiCorp. Server imaging; or alternatively, immutable infrastructure; is a popular alternative to runtime configuration tools, such as Ansible or Puppet. It is based on the idea that once a server is deployed, you never change it; instead, you deploy a new server with your changes and then tear down the old one. This makes server maintenance easier, and allows you to more efficiently scale across a large number of machines.
While Packer doesn't support Vultr out of the box, there is a plugin that adds that support. Instructions for installing Packer with Vultr support can be found on the plugin's README. The rest of this article assumes that you have Packer installed, as well as the Vultr plugin.
How it works
The purpose of Packer is to produce server images, or artifacts that can be used to create a new server exactly as it was at imaging time. For Vultr, those artifacts are called Snapshots. The plugin works by deploying a new VPS, running your defined provisioners on it, creating a Snapshot of it, and then tearing down the VPS. When you want to create a new server from your image, simply call Vultr's API to deploy a new VPS with the desired Snapshot as its OS.
Using Packer
Packer takes a JSON file defining the build that it should run as input. Here is a sample file defining a Vultr build:
{
"variables": {
"vultr_api_key": "{{env `VULTR_API_KEY`}}"
},
"builders": [
{
"type": "vultr",
"api_key": "{{user `vultr_api_key`}}",
"snapshot_description": "My Awesome App",
"region_id": "2",
"plan_id": "201",
"os_id": "167",
"ssh_username": "root"
}
]
}
This file has two top-level keys, variables
and builders
. The variables
key is used to define custom variables, and in this case is used to incorporate the VULTR_API_KEY
environment variable in a way that can be used later. The builders
key contains a list of builds to run, in this case only our Vultr build. Let's break that build down a little:
type
: This defines the builder to actually use, and for Vultr builds this should always be vultr
.
api_key
: In order to run the build, you need an API key, which is defined here. This example defines it as the value of the vultr_api_key
user variable, which further up is set to the value of the VULTR_API_KEY
environment variable.
snapshot_description
: The description of the resulting snapshot.
region_id
: The region to deploy a VPS to. This doesn't affect the final snapshot, only the server that the snapshot is created from.
plan_id
: The plan to use for the VPS. Future uses of this snapshot must define a plan that is at least this large.
os_id
: The Operating System to base the snapshot on.
ssh_username
: The username that will be used when SSH'ing into the server for provisioning. For Linux servers, this will usually be root
.
To use it, save this file with a name like server.json
, and then run the command packer build server.json
. You will see some output indicating the status of the build, which will take a few minutes.
When you run this build, a new server named Snapshotting: My Awesome App
will be created, snapshotted, and then destroyed. With no provisioners defined, that is all that happens. For more information on defining provisioners, refer to Packer's documentation.
Once the build is finished, Packer will output the ID of the resulting snapshot.
Defining region, plan, and OS
These three values are required for every Vultr build, but can be defined in one of several ways. For each one, you should specify exactly one of its variations in order to avoid ambiguity:
Region
Region information can be queried via the API: https://www.vultr.com/api/#regions_region_list
region_id
: The ID of the region, e.g. 2
region_code
: The code of the region, e.g. ORD
Plan
Plan information can be queried via the API: https://www.vultr.com/api/#plans_plan_list
plan_id
: The ID of the plan, e.g. 201
OS
Operating System information can be queried via the API: https://www.vultr.com/api/#os_os_list
os_id
: The ID of the OS, e.g. 167