google-compute-engineterraform-provider-gcppackerhcl

How can I get access to the exact image used by packer when I've specified an image family for it use


I'm building GCE images using packer. My hcl contains something like this

source "googlecompute" "my-image" {
  project_id          = var.project_id
  ...
  source_image_family = local.source_image_family
  image_labels = {
    "my_label" : "my_value",
    "label2" : var.label2_value
  }
  ...
}

I would like to include the exact source image name resolved from the specified family as a label. Something like "base_image" but I can't seem to find a way of doing this.

What I tried so far was to try and resolve the image name from the family in a data block and then I can specify that resolved image name to my source as source_image and also use it in labels and anywhere else I want. E.G.

data "google_compute_image" "base_image" {
  family  = var.source_image_family 
  project = var.project_id
}

source "googlecompute" "my-image" {
  project_id          = var.project_id
  ...
  source_image = data.google_compute_image.base_image.self_link
  image_labels = {
    "my_label" : "my_value",
    "label2" : var.label2_value,
    "base_image" : data.google_compute_image.base_image.name
  }
  ...
}

However that appeared to not be the right approach, it seems the googe_compute_image plugin isn't for use in packer and I get this error executing packer validate Error: Unknown data type google_compute_image

For packer I found https://developer.hashicorp.com/packer/integrations/hashicorp/googlecompute but this doesn't seem to provide any data components.

I did the obligatory asking of AI tooling but it suggested

data "googlecompute" "base_image" {
  family  = local.source_image_family
  project = var.project_id
}

which when ran just gives me this error executing packer validate Error: Unknown data type googlecompute.

It suggested I hadn't imported the plugin... but I am successfully using that plugin's builder component (my source block) so the plugin is clearly available.

Am I missing some obvious step/approach or is this just something that isn't possible in packer?

Thanks


Solution

  • The correct name for the data block is googlecompute-image as indicated in the documentation.

    According to the content in the question, your data block should appear like:

    data "googlecompute-image" "basic-example" {
      project_id  = var.project_id
      filters     = "family=${var.source_image_family}"
      most_recent = true
    }
    

    Note: you may be under the assumption that separators are generally _ instead of -, and this may be due to experience with Terraform. The Packer ecosystem generally uses - instead of _ for their nomenclature.