Terraform - Foreach - Default value
04:58 28 Jul 2023

I'm a newbie in the Terraform world. Excuse me in advance if this is a dumb question.

This is my conf file:

terraform {
  required_providers {
    google = {
      source  = "hashicorp/google"
      version = "~> 4.75.0"
    }
  }
}

provider "google" {
  credentials = file(var.credentials_file)
  project = var.project
  region  = var.region
  zone    = var.zone
}

resource "google_cloudfunctions_function" "all" { 

    for_each = {
        fct1 = {
            name = "bob"
            git_folder = "bob_folder"
            var1 = "pikachu"
            var2 = "red"
            available_memory_mb = 256
        },
        fct2 = {
            name = "bill"
            git_folder = "bill_folder"
            var1 = "pidget"
            // var2 <-- no need here
            // available_memory_mb <-- default value will be ok here
        }
    }

  name = each.value.name
  environment_variables = {
    VAR1 = var.var1 // OR each.value.var1 IF DEFINED ?
    VAR2 = each.value.var2 // OR DEFAULT VALUE IF UNDEFINED ?
  } 

  trigger_http = true
  available_memory_mb = // "default value if not defined on fct
  [...]

  source_repository {
    url = "https://source.developers.google.com/projects/[...]/moveable-aliases/${var.git_branch}/paths/${each.value.git_folder}"
  }
}

Like you've seen, I'm just trying to write all my google functions with their specific params. I don't succeed to specify default params than can be overwrite by specific params.

How can I tell to Terraform : "For this function, takes default param if undefined" ? Or maybe : "For this function, don't takes var.* param but the one defined here" ?

google-cloud-platform terraform terraform-provider-gcp