Control artifacts with channels
Just like software teams version and tag applications to track changes, you should version and tag parent artifacts. HCP Packer channels let you to label artifact versions to describe the quality and stability of the build. This gives you control over the artifact delivery, helping other teams dynamically query artifacts for their requirements. 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 artifact to deploy an EC2 instance with Terraform. Next, you will update the channel to point to another version, 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 with Contributor access to HCP.
- Set your client ID and secret as environment variables.
- Configured your AWS credentials as environment variables.
- Built an artifact and push its metadata to HCP Packer.
In addition, you will need:
- Terraform 1.1.7+ installed locally.
Create another artifact version
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 artifact. Continue with the tutorial while Packer builds the artifact.
Add channels to the bucket
Visit the HCP Packer dashboard and select the learn-packer-ubuntu
artifact you created in the previous tutorial. Your bucket has one complete version 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 v1
version. Leave the Channel access as Unrestricted.
Query and deploy artifact
Wait until Packer finishes building the second version 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_version
retrieves the channel's current version. Notice that this resource references the bucket name (learn-packer-ubuntu
) and channel (production
) you created in the previous steps.The
hcp_packer_artifact
retrieves a specific artifact from the version data source above. Remember that a version may contain multiple artifacts from different cloud providers and regions.
In addition, this configuration deploys an EC2 instance with the artifact you queried.
The Terraform provider for HCP uses the HCP_CLIENT_ID
, HCP_CLIENT_SECRET
, and HCP_PROJECT_ID
environment variables to authenticate to HCP. You configured these values in the previous tutorial.
Note
This tutorial uses the same service principal you configured previously with Contributor access to all HCP resources. HCP Packer lets you control access to individual buckets, and we recommend that you create a service principal that only has access to the buckets you need during your Terraform runs. Refer to HCP Packer permissions for more information.
Initialize your Terraform configuration.
Apply your configuration. Enter yes
when prompted to confirm the run.
The ubuntu_version
output returns all metadata associated with the version. It shows that this is the first version (name = "v1"
) and that it is assigned to the production
channel.
The ubuntu_us_east_2
output returns the build for us_east_2
region. Notice that the external_identifier
maps to the AMI ID.
Update a channel to point to another version
Return to the Channels page in the HCP Packer portal.
Edit the production channel by clicking on the ... and selecting Changed assigned version. Then, update the channel to the second version.
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 version to the channel.
Re-query channel artifact version
Now, re-apply your Terraform configuration. Enter yes
when prompted to confirm the run.
Since you updated the version the channel uses, the name
changed from v1
to v2
,
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 version, 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 artifact metadata in the HCP Packer documentation
- Visit the Terraform
hcp
provider for a full list of arguments and attributes for thehcp_packer_version
andhcp_packer_artifact
data resources - Visit the HCP Packer Glossary for additional descriptions of the terms covered in this tutorial