Packer — understand how it works in couple minutes

Photo by Greg Rosenke on Unsplash

What exactly is packer ?

How start with Packer ?

Example build command:

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

Packer Installation

How to use Packer to build Azure ARM Image

  • 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

  • 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

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Maciej

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