[英]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.我已经在 my.tf 配置中设置了多个 github 存储库,并在“github”存储库资源上使用了一个简单的 for_each。 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我的第一个意图是使用一个模块(./modules/github_repo/repo.tf),其中包括
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从 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不幸的是,我为每个分支和回购组合得到一个 404,就像这样
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":
错误:查询 GitHub 分支参考 /orgName/service1 (refs/heads/master) 时出错:GET https://api.github.com/repos//orgName/service1/git/ref/heads/master : 404 Not Found []使用 module.branches_per_microservice["service1"].github_branch.branches["test"] 在 modules/github_repo/repo.tf 第 23 行,在资源“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.我想这是一个“提供者”的东西,因为如果我尝试直接在 main.tf 中创建一个分支,它就会起作用。 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)
(我已经知道在带有 count 或 for_each 循环的模块中提供程序是不可能的,如 terraform 文档中所写)
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...对于这里的任何提示、想法或方法,请提前致谢......
Nested loop can be replaced with a single loop over the setproduct
of two sets.嵌套循环可以用两个集合的
setproduct
上的单个循环代替。 The documentation for setproduct
can be found here https://www.terraform.io/language/functions/setproduct setproduct
的文档可以在这里找到https://www.terraform.io/language/functions/setproduct
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.