Google notifications

Hello guys!

I’m trying to add budget, cost monitoring and budget to Terraform. I have a folder modules which has the module monitoring. In variables.tf I have defined the needed variables and in main.tf I have the resources.

This is the code that I have in main.tf:

resource "google_monitoring_notification_channel" "email" {
  # project      = var.project
  count        = length(var.notified_mails)
  display_name = "cloud function error email notification"
  type         = "email"
  labels = {
    email_address = var.notified_mails[count.index]
  }
  enabled = true
}

resource "google_billing_budget" "monthly_budget" {
  billing_account = var.billing_account
  display_name = "Monthly Expense Control"

  budget_amount {
    specified_amount {
      currency_code = var.budget_amount.currency_code
      units         = var.budget_amount.units
    }
  }
  calendar_period = var.calendar_period

  threshold_rules {
    threshold_percent = var.threshold_rules.threshold_percent
    spend_basis       = var.threshold_rules.spend_basis
  }

  notifications {
    monitoring_notification_channels = [google_monitoring_notification_channel.email[*].name]
    pubsub_topic = null
  }
}

Then I have folders for each environments which have main.tf, variables.tf and config.auto.tfvars files. The notifications in each environment are defined like:

variable "notifications" {
  type = object({
    monitoring_notification_channels = list(string)
    pubsub_topic = string
  })
}

I get those errors:

image

Can someone help me please? Thank you in advance!

Hi @c0nfusi0n9822,

Input variables are for sending data into the Terraform module from the outside, and so you can’t define an input variable in terms of something that’s inside the module.

The module source code you shared doesn’t seem to refer to var.notifications.monitoring_notification_channels anyway, so it seems like you could safely remove that attribute and remove its definition in the config.auto.tfvars file.

The module is written to refer directly to google_monitoring_notification_channel.email[*].name when configuring the monitoring notification channels anyway, so it seems like this input variable isn’t really doing anything.

I can’t set up the all_updates_rule:

all_updates_rule {
monitoring_notification_channels = [
google_monitoring_notification_channel.email[count.index],
]
disable_default_iam_recipients = false
}
}

This is the error:

This is how the resource is defined:
image