Control images with channels
Just like software teams version and tag applications to track changes, you should version and tag parent images. HCP Packer channels let you to label image iterations to describe the quality and stability of the build. Channels give you control over the delivery of your images. Other teams can dynamically query images based on the quality and stability they want. This workflow helps your infrastructure stay up to date with minimal manual effort and is less error-prone than manually referencing AMIs or using AWS tags.
In this tutorial, you will create a new channel in the HCP Packer registry. Next, you will query the channel and its build artifact metadata, then use the image to deploy an EC2 instance with Terraform. Next, you will update the channel to point to another iteration, and query the updated channel.
Prerequisites
To complete this tutorial, you must have completed the previous tutorials. In the previous tutorials, you:
- Created a service principal.
- Set your client ID and secret as environment variables.
- Configured your AWS credentials as environment variables.
- Built an image and push its metadata to HCP Packer.
In addition, you will need:
- Terraform 1.1.7+ installed locally.
Create another image iteration
In ubuntu-focal.pkr.hcl
, update the version number to 1.0.1
.
Tip
If you do not already have this file, complete the previous tutorial.
The amazon-ebs
source blocks use the version
variable to generate the AMI name. Since you changed the version number, Packer will generate a new AMI in each region.
In your terminal, format the Packer template.
Stage your template file.
Then, commit your changes.
Finally, build your image. Continue with the tutorial while Packer builds the image.
Add channels to the image bucket
Visit the HCP Packer dashboard and select the learn-packer-ubuntu
image you created in the previous tutorial. Your bucket has one complete iteration and another that Packer is currently building.
Go to Channels in the left navigation menu and click New Channel. Create a new channel named production
and select the first iteration.
Query and deploy image
Wait until Packer finishes building the second iteration before continuing.
Once Packer completes the build, change into the tf-channel
directory.
Open tf-channel/main.tf
to review the Terraform configuration.
This Terraform configuration contains two data sources:
The
hcp_packer_iteration
retrieves the channel's current iteration. Notice that this resource references the bucket name (learn-packer-ubuntu
) and channel (production
) you created in the previous steps.The
hcp_packer_image
retrieves a specific image from the iteration data source above. Remember that an iteration may contain multiple machine images from different cloud providers and regions.
In addition, this configuration deploys an EC2 instance with the image you queried.
The Terraform provider for HCP uses the HCP_CLIENT_ID
and HCP_CLIENT_SECRET
environment variables to authenticate to HCP. You configured these values in the previous tutorial.
Initialize your Terraform configuration.
Apply your configuration. Enter yes
when prompted to confirm the run.
The ubuntu_iteration
output returns all metadata associated with the iteration. It shows that this is the first iteration (incremental_version
) and assigned to the production
channel.
The ubuntu_us_east_2
output returns the build for us_east_2
region. Notice that the cloud_image_id
maps to the AMI ID.
Update a channel to point to another iteration
Return to the Channels page in the HCP Packer portal.
Edit the production channel by clicking on the ... and selecting Changed assigned iteration. Then, update the channel to the second iteration.
View channel assignment history
Click on the production
channel name to view the channel's overview page.
Review the Assignment history section, which lists when you assigned each iteration to the channel.
Re-query channel image iteration
Now, re-apply your Terraform configuration. Enter yes
when prompted to confirm the run.
Since you updated the iteration the channel uses, the incremental_version
changed from 1
to 2
,
Destroy infrastructure
Run terraform destroy
to clean up your provisioned infrastructure. Respond
yes
to the prompt to confirm the operation.
Next steps
In this tutorial, you created a new channel in the HCP Packer registry. Next, you used Terraform to query the channel and its build artifacts metadata, then used the AMI ID to deploy an EC2 instance . Finally, you updated the channel to point to another iteration, and then queried the updated channel and build artifacts.
For more information on topics covered in this tutorial, check out the following resources:
- Read more about referencing image metadata in the HCP Packer documentation
- Visit the Terraform
hcp
provider for a full list of arguments and attributes for thehcp_packer_iteration
andhcp_packer_image
data resources - Visit the HCP Packer Glossary for additional descriptions of the terms covered in this tutorial