Adam Briers
Adam Briers

Reputation: 101

GCP Terraform resource policy in compute module issues

I am trying to add a start-stop schedule to our vm instances in our cloud repository (it is a terraform/terragrunt setup)

The example presented on the official site is this:

enter image description here

So since we use Terragrunt as a wrapper my module looks like this:

enter image description here

And for reference my variable block is this:

enter image description here

When i push the code it errors on step 0 in CloudBuild with the following error:

Error: Reference to undeclared input variable on main.tf line 116, in resource "google_compute_resource_policy" "hourly": 116: time_zone = var.time_zone An input variable with the name "time_zone" has not been declared. This variable can be declared with a variable "time_zone" {}block.

enter image description here

I have tried placing this variable in different positions of the block but i keep getting the same error. Has anyone got any ideas?

Upvotes: 0

Views: 951

Answers (1)

Adam Briers
Adam Briers

Reputation: 101

This is now resolved. I want to thank @kornshell93 for pointing me in the right direction.

I ended up using the block as suggested but creating a new module and hitting that from a separate section within my vm instance block. I linked to the project as a dependency this way. The previous method via the main compute instance module kept failing on all other vm instances, almost like it was expecting this block on all of them.

resource "google_compute_resource_policy" "hourly" {
name        = var.instance_schedule_policy.name
region      = var.region
project     = var.project
description = "Start and stop instances"

instance_schedule_policy {
  vm_start_schedule {
    schedule = var.instance_schedule_policy.vm_start_schedule
  }
  vm_stop_schedule {
    schedule = var.instance_schedule_policy.vm_stop_schedule
  }
  time_zone = var.instance_schedule_policy.time_zone
}
}

And the vm instance block

inputs = {
#instance start/stop schedules
project               = dependency.project.outputs.project_id
region                = "europe-west2"
instance_schedule_policy = {
name                      = "start-stop"
vm_start_schedule         = "30 07 * * *"
vm_stop_schedule          = "00 18 * * *"
time_zone                 = "GMT"
}
}

Upvotes: 0

Related Questions