Inside a build

What happens during packer build on a cloud, step by step, from a temporary machine through communicators, provisioners and the saved image, to post-processors and clean-up.

GazeekTERRAFORM AND PACKER: INFRASTRUCTURE AS CODE·LESSON 18
LESSON 18

Inside a build

What happens during packer build on a cloud, step by step, from a temporary machine through communicators, provisioners and the saved image, to post-processors and clean-up.

0:00 / 3:48

Diagrams in this video · 2Free in archhy

Copy and edit in archhy: One build, six steps (new tab)
Copy and edit in archhy: Inside packer build on AWS (new tab)

After this lesson you can

  • Describe the steps a cloud builder takes during packer build
  • Explain what a communicator is and name SSH and WinRM
  • Describe what provisioners and post-processors do
  • Explain why Packer cleans up its temporary resources
Transcript

Inside a build

Space Llama here. Last time, we read a template. Today we press the button. Let's follow one packer build on AWS, from the first API call to the finished image. Grab a cup of tea. A real build takes a few minutes, and we'll use every one of them.

One build, six steps

Every cloud build follows the same shape. The builder launches a temporary machine. Then Packer connects to it, using a communicator. Provisioners run inside the machine to install and configure software. When they're done, the machine is saved as an image, and Packer cleans up everything temporary. Finally, post-processors can do something with the result, like writing a list of what was built.

Inside packer build on AWS

Here's that build on AWS, using the amazon E B S builder. A data source finds the base image to start from. Say, the newest official Debian or Ubuntu AMI, an Amazon Machine Image. Packer creates a temporary key pair and security group, then boots a temporary build instance from that base image. It connects over SSH, the Secure Shell, and runs the provisioners. Shell scripts, uploaded files, or a configuration tool like Ansible. Then Packer stops the instance and saves it as a new AMI. That's your golden image. The build instance, the key pair and the security group are all deleted. Only the image stays. And a manifest post-processor can write a small JSON file recording the new image ID. Every one of these steps shows up in the build log, so you can watch it happen, live.

Communicators

A communicator is how Packer uploads files and runs scripts on the machine it's building. Usually that's SSH. For Windows, it's WinRM. You can also say none, when no provisioners need to connect. Some builders bring their own. The Docker builder, for example, just uses docker exec. Packer waits for the communicator to answer before it runs anything. So a slow boot just means a slightly longer wait.

Provisioners vs post-processors

Two words that sound alike, so let's split them. Provisioners run inside the machine, before it becomes an image. They change what's in the image. Post-processors run afterwards. They work on the output, like writing a manifest, compressing a file, or tagging a container image. Inside the oven, or after it comes out. Baking metaphors, all the way down.

Where builds run

You can run packer build on your laptop, and that's fine while you're learning. For a team, run it in CI, your continuous integration, so every image comes from a reviewed commit, built the same way every time. Either way, Packer needs credentials for the cloud, because it launches real machines. So a build costs a little money while the temporary machine runs. Clean-up matters for your bill, too.

Recap

Let's recap. A builder launches a temporary machine from a base image. A communicator, usually SSH or WinRM, connects to it. Provisioners install and configure, then the machine is saved as an image. Post-processors handle the output, and Packer deletes everything temporary. Only the image stays.

Practice and what's next

That's a build, from the inside. No servers were harmed. Well, one temporary one. Next lesson, A registry for image metadata, where we meet HCP Packer, on the HashiCorp Cloud Platform. See you there. More videos on the channel. Alpaca Mango. I'll be there. I'm always there. Mate.

Gazeek Flash · free

Exam questions on your phone

Free questions on Packer and golden images: quick-fire batches and timed exams, on the web or Android.