Reduce Your Virtual Machine Build Time Using Hashicorp Packer: [Part 1 - Packer Overview and Install]
Dec 14, 2023Introduction:
If you're someone tasked with server provisioning, OS installations/configurations, or setting up a home virtual sandbox, this blog series is for you. In it, you'll learn about Hashicorp Packer and how it can make your job easier. We'll end the series with a set of scripts and an HCL template you can use to build a Windows Server 2022 golden image.
Server provisioning is a cumbersome process involving numerous steps. It can be highly time-consuming and error-prone when done manually. With the advent of the cloud and how easy it is to create whole environments, development teams and end users expect platforms to be built instantly. Issues commonly encountered with a manual approach include:
- Time-consuming - Even if virtual, server builds involve operating system installation, patching, and configuring it to specifications (firewall rules, date/time settings, additional package/software installations, power settings, disk formatting, etc.). This ties you, the system/platform engineer, to a computer and limits your productivity. Most environments consist of multiple machines, which is exacerbated when whole environments must be built for projects or migrations.
- Inconsistent and unreliable results - Manual processes are error-prone. Each systems admin or platform engineer may follow their process when building new servers. It's usually too late to reconfigure the machine once in use and requires an impact on availability to correct (which then impacts customers).
- Security and compliance risks.
Hashicorp Packer can help address each one of the issues above. It is an open-source tool that can help you avoid manual provisioning steps. In this blog series, we'll cover the following;
- Part 1 - Packer Overview and Install
- Part 2 - HCL template components
- Part 3 - Environment Setup
- Part 4 - Windows Server 2022 - HCL template
- Part 5 - Configuration Scripts
- Part 6 - Creating the golden image (Win2022_gui.box)
What is Hashicorp Packer?
Packer is an open-source tool that enables you to create identical machine images for multiple platforms from a single source template. I use it to create golden images, which I then use to make my development/learning environment. One of the most common use cases is creating golden images that teams across an organization can use in cloud infrastructure and on-premises virtual environments. You can implement it along with your overall Infrastructure as Code strategy. Define the template once, and deploy the golden image to multiple environments (in the cloud or on-premises). Here are just some of the benefits of using Packer;
- Faster and easier image creation and delivery
- Higher quality and performance of images
- Greater flexibility and compatibility of images
- Enhanced security and compliance of images
- Environment Parity - Avoid the "works on my machine" scenarios. With Packer, you can ensure that any new environments you build start with the same image.
Let's take a look at how simple the installation is.
Installation:
Packer can be installed on MacOS, Windows, and Linux. Installation steps are below. In the upcoming posts, I'll use Windows with VMware Workstation Pro installed on the same machine.
- OS X - Use Homebrew.
brew tap hashicorp/tap
- brew install hashicorp/tap/packer
- Windows (using Chocolatey). If you already have Chocolatey installed, skip step 1. If not, open Windows Powershell as an admin and install it using the command below.
Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))
choco install packer
- Linux - Check this link for details. It depends on which distribution you're using.
Next, run the following command to check the Packer version.
packer --version
That's it. Simple installation.
Conclusion:
Packer can simplify your server build process considerably and, if you're new to Infrastructure as Code concepts, get you started down the path of platform engineering. If you want to follow along with this series and don't have an environment to test with, I'd encourage you to download the guide below to begin setting up your sandbox environment.
Thanks for stopping by.