Introduction
Packer has two data sources that work together to retrieve information from the HCP Packer registry:
- hcp-packer-iteration - Retrieves information about an iteration.
- hcp-packer-image - Retrieves information about a specific image.
This guide will show you how to use the HCP Packer Registry Data sources in your Terraform configuration to specify the exact build image when multiple images exist in the same provider and region for a given iteration.
Prerequisites
- Install Packer v1.7.7+ to use the latest version of the HCP Packer registry data sources.
- An HCP account with an HCP Packer Registry and Service Principal key.
- A Cloud Provider account with sufficient access credentials.
- Terraform HCP Provider version 0.36.0+.
Use Case
The hcp_packer_image data source only returns the first found image's metadata filtered by the given schema values, from the returned list of images associated with the specified iteration. Therefore, if multiple images exist in the same region, it will only pick one of them. In this case, you can filter images by a source build name using the component_type
optional argument.
Procedure
This example assumes multiple images were built in parallel from a single Packer template, and those images exist in the same cloud provider and region for a given iteration. We will use the HCP Packer Registry Data sources in our Terraform configuration to specify the exact build image we're looking for.
AWS
- Configure the hcp_packer_iteration data source to retrieve information about the HCP Packer iteration.
data "hcp_packer_iteration" "aws-images" {
bucket_name = "aws-images"
channel = "latest"
}
-
Configure the hcp_packer_image data source with the
component_type
argument referencing the name of the source builder that built the image, and output the cloud_image_id to display the AMI of the image being retrieved.
data "hcp_packer_image" "aws-ubuntu" {
bucket_name = "aws-images"
cloud_provider = "aws"
component_type = "amazon-ebs.ubuntu-focal-west"
iteration_id = data.hcp_packer_iteration.aws-images.ulid
region = "us-west-2"
}
output "aws-image-id" {
value = data.hcp_packer_image.aws-ubuntu.cloud_image_id
}
- Run a
terraform plan
to verify the correct AMI is being retrieved by Packer. If correct, you can reference the image usingami = data.hcp_packer_image.aws-ubuntu.cloud_image_id
when provisioning AWS resources.
$ terraform plan
data.hcp_packer_iteration.aws-images: Reading...
data.hcp_packer_iteration.aws-images: Read complete after 0s [id=01GT01GG3HKNBFD48MP1TMM7V7]
data.hcp_packer_image.aws-ubuntu: Reading...
data.hcp_packer_image.aws-ubuntu: Read complete after 0s [id=01GT01SD97F1JVGNYB8ND95M2T]
Changes to Outputs:
+ packer-image-id = "ami-0457b715e3acf189d"
You can apply this plan to save these new output values to the Terraform state, without changing any real infrastructure.
You can use the same process described above to provision GCE instances, however, instead of retrieving the AMI ID, we need to reference the self_link
URL attached to the Packer image. See google_compute_instance doc for more information.
- This can be achieved by referencing the
labels
of the Packer image usingimage = data.hcp_packer_image.gce-ubuntu.labels["self_link"]
.
Full example can be seen below:
data "hcp_packer_iteration" "gce-images" {
bucket_name = "gce-images"
channel = "latest"
}
data "hcp_packer_image" "gce-ubuntu" {
bucket_name = "gce-images"
cloud_provider = "gce"
component_type = "googlecompute.ubuntu-2004-lts"
iteration_id = data.hcp_packer_iteration.gce-images.ulid
region = "us-central1-a"
}
##Build google instance from packer data sources.
resource "google_compute_instance" "gce_instance" {
name = "gce-instance"
machine_type = "f1-micro"
boot_disk {
initialize_params {
image = data.hcp_packer_image.gce-ubuntu.labels["self_link"]
}
}
network_interface {
network = google_compute_network.vpc_network.self_link
access_config {
nat_ip = google_compute_address.static.address
}
}
}
Additional Information & References
For additional questions or support, please open a Support ticket.