Packer meets Terraform

How Terraform reads an image ID from an HCP Packer channel with the hcp_packer_artifact data source, so promoting a channel is all it takes for the next apply to use the new image, and how revoked images are kept out of plans.

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

Packer meets Terraform

How Terraform reads an image ID from an HCP Packer channel with the hcp_packer_artifact data source, so promoting a channel is all it takes for the next apply to use the new image, and how revoked images are kept out of plans.

0:00 / 3:34

Diagrams in this video · 2Free in archhy

Copy and edit in archhy: Hard-coded, or from a channel (new tab)
Copy and edit in archhy: Terraform reads the image from a channel (new tab)

After this lesson you can

  • Explain why hard-coded image IDs cause trouble
  • Use the hcp_packer_artifact data source to read an image from a channel
  • Describe what happens on the next apply after a channel is promoted
  • Describe two ways to keep revoked images out of a plan
Transcript

Packer meets Terraform

G'day. Space Llama here, for the last part of the course. We've got Terraform on one side, and on the other, HCP Packer, the HashiCorp Cloud Platform registry full of golden images. Today, we join them up.

Hard-coded, or from a channel

The old way is to paste an Amazon Machine Image ID straight into Terraform. A M I equals, then a long string. It works, until the next image. Then somebody edits the code, in every configuration, in every region. The better way is a data source. Terraform asks HCP Packer which image the production channel points at, every time it plans. So the image changes when the channel moves, and the code doesn't change at all.

The data source

The data source is called H C P packer artifact, and it comes from HashiCorp's H C P provider. You give it four things. The bucket name, the channel name, the platform, such as A W S, and the region. It returns the matching artifact. Its external identifier attribute is the image ID, ready to drop into a launch template. Because it's a data source, it's read fresh on every plan. Nothing to remember, and nothing to paste.

Terraform reads the image from a channel

Here's the whole join. On the left, HCP Packer, with a production channel. In the middle, an HCP Terraform workspace. Its configuration uses the data source to read the AMI ID for u s east one. On apply, Terraform updates the launch template on the right with the new image ID. New servers in the Auto Scaling group launch from it. To replace the servers already running, you'd add an instance refresh. Terraform can manage that too. Meanwhile, a run task checks the plan with HCP Packer. Is any image in it revoked?

What a promotion looks like

Let's follow one promotion through. Someone moves the production channel to version four. Nothing happens yet. Moving a channel doesn't start a run by itself. At the next plan, the data source reads the channel again, and gets a new image ID. The plan shows the launch template changing, from the old image to the new one. You review it, like any other change, then apply.

Keeping revoked images out

Remember revoke at, from the revocation lesson? You can check it yourself, for free. Add a precondition to the resource. If the image's revoke at time has passed, the plan fails with a clear message. Or use HCP Terraform's artifact validation run task. It fails when new or replaced resources use a revoked version. Set to mandatory, it stops the run. Set to advisory, it warns. HCP Packer's paid Standard edition also checks hard-coded image IDs.

Recap

Let's recap. Hard-coded image IDs go stale. Read images from the registry instead. The H C P packer artifact data source reads a channel, and returns the image ID. Promote the channel, and the next apply uses the new image. And a precondition, or the artifact validation run task, keeps revoked images out of your plans.

Practice and what's next

That's Packer meeting Terraform. They get on very well, as it turns out. Next lesson, The image lifecycle, end to end, where we automate the whole loop. See you there.

Gazeek Flash · free

Exam questions on your phone

Free questions on The image lifecycle, and what's next: quick-fire batches and timed exams, on the web or Android.