|
| 1 | +--- |
| 2 | +title: "Level up your Packer templates: A best practices guide" |
| 3 | +date: 2024-09-04 |
| 4 | +description: Looking to improve your Packer templates? This collection of tips will make your Packer templates faster, easier to use and easier to maintain. |
| 5 | +avatar: baseball-blue |
| 6 | +images: |
| 7 | + ogPath: 1x1.png |
| 8 | + ldPaths: |
| 9 | + - 1x1.png |
| 10 | + - 4x3.png |
| 11 | + - 16x9.png |
| 12 | +--- |
| 13 | + |
| 14 | +Over the past few years I've been using Packer a lot. |
| 15 | +When it works, it works. |
| 16 | +But most of the time the experience is not very smooth. |
| 17 | +Over time I made many mistakes, and slowly figured out how to create Packer templates that are easier to use and maintain. |
| 18 | + |
| 19 | +I collected my experiences with Packer into practices that will help you learn from my mistakes and create Packer templates that are easier to use, test and maintain. |
| 20 | + |
| 21 | +## Test the images |
| 22 | + |
| 23 | +> Test images automatically. |
| 24 | +
|
| 25 | +Automated testing is hands down the best productivity boost I found for Packer. |
| 26 | +Manually testing images is a long and tedious process. |
| 27 | +Automated testing makes the development feedback loop much faster, making it much easier to maintain templates. |
| 28 | + |
| 29 | +Testing Packer templates means building the template, cloning it and performing checks on the cloned virtual machine. |
| 30 | +[Terratest has utilities for running Packer ](https://pkg.go.dev/github.com/gruntwork-io/[email protected]/modules/packer), |
| 31 | +and I find it very useful because it also has utilities for running Terraform and running commands over SSH. |
| 32 | + |
| 33 | +[Check out my Kubernetes image builder to see an example of testing with Terratest.](https://github.com/SneakyBugs/Kubernetes-Image-Builder/tree/main/test) |
| 34 | + |
| 35 | +## Minimize the boot command |
| 36 | + |
| 37 | +> Do as little as possible in the boot command. |
| 38 | +
|
| 39 | +Boot commands are slow and inconsistent. |
| 40 | +Automating an interactive process with one-way communication tends to do that. |
| 41 | + |
| 42 | +Avoid performing unnecessary actions in the boot command. |
| 43 | +Only perform actions required for Packer to connect with SSH, everything else will be easier, faster and more consistent with provisioners. |
| 44 | + |
| 45 | +[Alpine Linux cloud images are a great example](https://gitlab.alpinelinux.org/alpine/cloud/alpine-cloud-images/-/blob/main/alpine.pkr.hcl) of doing the bare minimum, connecting to SSH first and installing the OS with provisioners. |
| 46 | + |
| 47 | +If possible, it is best to use a disk image of an OS rather than an installer `iso`. |
| 48 | +Using a disk image removes the need for boot commands, and is much faster since you don't need to install the OS itself. |
| 49 | + |
| 50 | +## Reduce usage of Variables |
| 51 | + |
| 52 | +> Only use variables for what users need to change. |
| 53 | +
|
| 54 | +Only use variables for options users need to customize on a per-build basis. |
| 55 | +Use locals to avoid repeating yourself if needed. |
| 56 | +Use multiple `source` and `build` blocks for building images of multiple operating systems and versions. |
| 57 | + |
| 58 | +Variables are the interface between the template and it's users. |
| 59 | +Reducing the amount of variables makes templates easier to use, develop, and test. |
| 60 | + |
| 61 | +The effects of using too many variables |
| 62 | +[can be seen in the official Kubernetes image builder.](https://github.com/kubernetes-sigs/image-builder/tree/main/images/capi/packer/qemu) |
| 63 | +It makes the template very hard to understand, and does not reduce code repetition much compared to multiple `source` and `build` blocks. |
| 64 | + |
| 65 | +## Avoid Packer's HTTP server |
| 66 | + |
| 67 | +> Avoid Packer's HTTP server if you plan on running in CI. |
| 68 | +
|
| 69 | +Using Packer's HTTP server limits you to run Packer only where you can run an HTTP server reachable by the virtual machine. |
| 70 | +This is problematic for running Packer inside CI runners which are not configured to accept incoming HTTP traffic. |
| 71 | +An example of working around this limitation |
| 72 | +[can be seen in my Debian PVE template.](https://github.com/LKummer/packer-debian) |
| 73 | +Where I had to work around it by uploading the needed files as GitLab pipeline artifacts in the `upload-preseed` job. |
| 74 | + |
| 75 | +When not utilizing Packer's HTTP server, you can build templates everywhere. |
| 76 | +No need for messy workarounds. |
| 77 | + |
| 78 | +By avoiding the HTTP server you also avoid Windows related issues. |
| 79 | +Specifically CRLF line endings causing issues when cloning on Windows and networking problems when running inside WSL. |
0 commit comments