Martin Januschke
Martin Januschke

Reputation: 1

how to add an additional loop over a stringlist within a for_each

i've setted up multiple github repos within my .tf configuration with a simple for_each on "github" repository resource. In addition i tried to create several branches (a branch per env) for each newly created repo.

My first intention was to use a module (./modules/github_repo/repo.tf) which includes

    locals {
      environments = var.environments
    }

    resource "github_branch" "branches" {
      for_each      = toset(setsubtract(local.environments, ["prod"]))
      repository    = "orgName/${var.REPO_NAME}"
      branch        = lookup(var.environment_to_branch_map, each.key, each.key)
      source_branch = "master" 
    }

with following variables

    variable "REPO_NAME" {
      type = string
    }
    
    variable "environments" {
      type    = list(string)
    }

    variable "environment_to_branch_map" {
      type = map(any)
      default = {
        "prod" = "master"
        "dev"  = "develop"
      }

calling like this from main.tf


    provider "github" {
      token = var.GITHUB_TOKEN
      owner = "orgName"
    }    

    locals {
      environments = ["dev", "prod", "staging", "test"]
      microServices  = tomap({ "service1" : "fnApp", "service2" : "fnApp" })
      default_branch = "master"
    }

    module "branches_per_microservice" {
      for_each     = local.microServices
      source       = "./modules/github_repo"
      REPO_NAME    = each.key
      environments = local.environments
      depends_on   = [github_repository.microservices]
    } 

unfortunately i get an 404 for each branch and repo combination like this

Error: Error querying GitHub branch reference /orgName/service1 (refs/heads/master): GET https://api.github.com/repos//orgName/service1/git/ref/heads/master: 404 Not Found [] with module.branches_per_microservice["service1"].github_branch.branches["test"] on modules/github_repo/repo.tf line 23, in resource "github_branch" "branches":

i guess it's a "provider" thing, cause if i try to create a branch directly in the main.tf, it will work. but the problem is, that i only can use one loop within a resource. (i already know that providers are not possible in modules with count or for_each loops, as written in terraform docs)

resource "github_branch" "branches" {
  for_each      = toset(setsubtract(local.environments, ["prod"]))
  repository    = github_repository.microservices["service1"].name 
  branch        = lookup(var.environment_to_branch_map, each.key, each.key)
  source_branch = "master" 
}

in this case, i have to create a resource for each "MicroService" manually, which i want to avoid heavily... Are there any ideas how i could "nest" the second loop over the environments to create my branches for each Micorservice repos?

Many thx in advance for any hint, idea or approach here...

Upvotes: 0

Views: 290

Answers (1)

Fedor  Petrov
Fedor Petrov

Reputation: 1050

Nested loop can be replaced with a single loop over the setproduct of two sets. The documentation for setproduct can be found here https://www.terraform.io/language/functions/setproduct

Upvotes: 0

Related Questions