Search code examples
google-cloud-platformterraformterraform-provider-gcp

Terraform - Foreach - Default value


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" ?


Solution

  • This is working as expected :

    variables.tf

    variable "function_env" {
        default = {
            var1 = "A"
            var2 = "B"
        }
    }
    
    variable "function_params" {
        default = {
            runtime = "python311"
            available_memory_mb = 512
        }
    }
    

    main.tf

    resource "google_cloudfunctions_function" "all" { 
        for_each = {
            funct1 = {
                name = "Billy"
                env = {
                    var1 = "C"
                }
                runtime = "python310"
            },
            funct2 = {
                name = "John"
                env = {
                    var2 = "D"
                }
            }
        }
    
        // Mandatory variables that needs to be defined for EACH item :
        name = each.value.name
        // Optionnal variables that can be defined for each item. If not, default in variables.tf :
        environment_variables = merge(var.function_env, each.value.env)
        runtime = lookup(each.value,"runtime", var.function_params.runtime)
        // Variables that can only be change by variables.tf :
        available_memory_mb = var.function_params.available_memory_mb
        // Constant variables that can't be change for each item. Always the same :
        timeout = 60
    }
    

    Tell me if this is not best pratice !