unable to change source_image for GCP mig template

Viewed 51

I have a seemingly simple task and it's giving me lots of problems. I have a GCP infrastructure project that looks like this:

.
├── files
│   ├── instance-template-custom.json
│   └── instance-template-default.json
├── modules
│   ├── cloud-vpc
│   │   ├── main.tf
│   │   ├── outputs.tf
│   │   └── variables.tf
│   ├── compute
│   │   ├── main.tf
│   │   ├── outputs.tf
│   │   └── variables.tf
│   ├── firewall
│   │   ├── main.tf
│   │   ├── outputs.tf
│   │   └── variables.tf
│   ├── glb
│   │   ├── main.tf
│   │   ├── outputs.tf
│   │   └── variables.tf
│   └── storage
│       ├── main.tf
│       ├── outputs.tf
│       └── variables.tf
└── src
    ├── gceme.sh.tpl
    ├── main.tf
    ├── outputs.tf
    ├── terraform.tfvars
    ├── variables.tf
    └── versions.tf

I am using the GCP "gce-lb-http" module, and specifically the example code for https-redirect: https://github.com/terraform-google-modules/terraform-google-lb-http/tree/master/examples/https-redirect

The "mig_template" module will default to deploying Centos7 VMs. I have a requirement to deploy Debian 10 vms. This should be very straightforward for me to do, using the source_image setting in the module. My main.tf in the "glb" module (from my code tree) looks like this: (additonal code removed for clarity)

...
// get Debian 10 image self-link
data "google_compute_image" "debian_image" {
  family  = "debian-10"
  project = "debian-cloud"
}


// create MIG template and mig here
module "gateway_template" {
  source            = "terraform-google-modules/vm/google//modules/instance_template"
  version           = "6.2.0"
  machine_type      = var.app_machine_type
  disk_size_gb      = 20
  source_image      = data.google_compute_image.debian_image.self_link
  labels            = {
    costing   = "app",
    inventory = "demo1",
    component = "gateway"
  }
  network           = var.network
  subnetwork        = var.subnetwork
  access_config     = []
  service_account   = {
    email  = var.service_account_email
    scopes = ["cloud-platform"]
  }
  name_prefix       = var.network_name
  tags           = ["compute"]
}
...

However, the TF plan throws the following error:

Error: error retrieving image information: googleapi: Error 400: Invalid value for field 'image': 'https://www.googleapis.com/compute/v1/projects/debian-cloud/global/images/debian-10-buster-v20220719'. Must be a match of regex '[a-z](?:[-a-z0-9]{0,61}[a-z0-9])?|[1-9][0-9]{0,19}', invalid
│ 
│   with module.glb.module.gateway_template.data.google_compute_image.image,
│   on .terraform/modules/glb.gateway_template/modules/instance_template/main.tf line 20, in data "google_compute_image" "image":
│   20: data "google_compute_image" "image" {

Can anyone suggest what I might be doing wrong?

The source code ((https://github.com/terraform-google-modules/terraform-google-vm/blob/v7.8.0/modules/instance_template/main.tf)) for the instance_template submodule has locals defined in its main.tf:

locals {
  source_image         = var.source_image != "" ? var.source_image : "centos-7-v20201112"
  source_image_family  = var.source_image_family != "" ? var.source_image_family : "centos-7"
  source_image_project = var.source_image_project != "" ? var.source_image_project : "centos-cloud"
...}

This is definitely what is tripping me up (the Centos defaults) but I don't know how to code around this. I thought by simply referring to the data lookup for the debian 10 image, this would all be sorted. Thanks in advance for any pointers!

1 Answers

I was able to get this working by hard-coding the three characteristics of an image they are looking for in the source code: image family project

// create MIG template and mig here
module "gateway_template" {
  source               = "terraform-google-modules/vm/google//modules/instance_template"
  version              = "6.2.0"
  machine_type         = var.app_machine_type
  disk_size_gb         = 20
  source_image         = "debian-10-buster-v20220719"
  source_image_family  = "debian-10"
  source_image_project = "debian-cloud"
  labels = {
    costing   = "app",
    inventory = "demo1",
    component = "gateway"
  }
  network       = var.network
  subnetwork    = var.subnetwork
  access_config = []
  service_account = {
    email  = var.service_account_email
    scopes = ["cloud-platform"]
  }
  name_prefix = var.network_name
  tags        = ["compute"]
}
Related