0

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?

JM Gelilio
  • 3,482
  • 1
  • 11
  • 23
Adam Briers
  • 101
  • 3
  • 14
  • Because the variable `instance_schedule_policy` is an object with different attributes, in order to reference the `time_zone` you need to do it like this: `var.instance_schedule_policy.time_zone` – javierlga Jun 16 '22 at 16:08
  • I made the amendment: ```resource "google_compute_resource_policy" "hourly" { #description = "Start and stop instances" instance_schedule_policy { name = var.name region = var.region #time_zone = var.time_zone vm_start_schedule { schedule = var.vm_start_schedule } vm_stop_schedule { schedule = var.vm_stop_schedule } time_zone = var.instance_schedule_policy.time_zone } }``` – Adam Briers Jun 17 '22 at 08:30
  • `on main.tf line 112, in resource "google_compute_resource_policy" "hourly": 112: schedule = var.vm_start_schedule An input variable with the name "vm_start_schedule" has not been declared. This variable can be declared with a variable "vm_start_schedule" {} block.` Same for the stop_schedule. If i try and rename the schedule = var.vm_stop_schedule to vm_stop_schedule = var.vm_stop_schedule It errors advising it expects `schedule =` – Adam Briers Jun 17 '22 at 08:38

1 Answers1

0

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"
}
}
Adam Briers
  • 101
  • 3
  • 14