Packer — understand how it works in couple minutes

What exactly is packer ?

is a tool for creating a server image, with this tool we can create an AMI for AWS, arm for Azure or box for Vagrant. Packer is developed a Vagrant HashiCorp company a feeling of use because the tools that have been developed is the same kind of feel that Vagrant. I think it is a common pattern to create a pre-built server image as an arm/ami and launch an instance based on this.

How start with Packer ?

Just write the definition contents in the json file and execute , and the server image will be created automatically according to the definition contents.

Example build command:

packer build -var "version=$VAGRANT_BOX_VERSION.$NOW" -var box-config.json

Packer Installation

Installation is very simple, we can use package manager or download binary file. All information we can find in documentation.

How to use Packer to build Azure ARM Image

At the beginning we will need create Azure service principal which will be used by Packer to connect to MS Azure.

  • Create service principal
az ad sp create-for-rbac --query "{ client_id: appId, client_secret: password, tenant_id: tenant }"

After creating the Service Principal, we should get the following output

{
"client_id": "xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
"client_secret": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
"tenant_id": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
}
  • Now we can define packer template. To build the image, we will need a json file with a complete definition. As an example, let’s take a look at the definition that creates the ARM below.

All information that will be used by the builder is contained in the variables section.

  • If we want to check the syntax we can do this with below command
packer validate ubuntu.json

If our syntax is correct and there are no errors, a message should appear like below:

Template validated successfully.
  • Now it’s time to build our image, we do this with this command
packer build ubuntu.json

It takes a few minutes to build the ARM, it’s depend on the content of the initialization task. After build is complete, we can find our image on the Azure portal.

How to use Packer to build Vagrant Box

  • Create token in VagrantCloud
  • Now we can define the packer template. To build the image, we will need the same json file with full definition as before. As an example, let’s look at the definition that creates the Vagrant Box
  • In this case, the variables section is much smaller than last since it only contains versions and cloud_token. There is no need to define anything else because the image is built using a local Virtualbox.
  • Build our image, we do this with this command
packer build -var "version=1.0.0" -var "cloud_token=xxxxxxxxxxxxxxxxxxxxxx.yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz" box-config.json

Speed of building in this case depends on how powerful the computer is, usually it takes a few minutes. After build is complete, we can find our image on the Vagrant cloud portal

Conclusion

I think this is a good and quick way to create golden images that we can use in our infrastructure.

DevOps Consultant. I’m strongly focused on automation, security, and reliability.