gpt4 book ai didi

Terraform for_each for nested value(嵌套值的TerraForm For_Each)

转载 作者:bug小助手 更新时间:2023-10-25 19:12:40 34 4
gpt4 key购买 nike



The main idea is to create several env_var for one environment (production and development) in a GitHub repo.

主要思想是在GitHub存储库中为一个环境(生产和开发)创建多个env_var。


Using these variables, I can pass through each.key Keys are the environment names.
However, I need to realize how I iterate over nested values.
What do I need to use instead local.env_vars_git[each.key][0]["env_var_name"]?

使用这些变量,我可以遍历每个变量。Key键是环境名称。但是,我需要认识到如何迭代嵌套的值。我需要使用什么来代替local.env_vars_git[each.key][0][“env_var_name”]?


locals {
env_vars_git = {
production = [
{
env_var_name = "hostname_01"
env_var_value = "%s"
},
{
env_var_name = "hostname_02"
env_var_value = "%s"
}
],
development = [
{
env_var_name = "hostname_01"
env_var_value = "%s"
},
{
env_var_name = "hostname_02"
env_var_value = "%s"
}
]
}
}

resource "github_actions_environment_variable" "aws" {
for_each = local.env_vars_git
repository = github_repository.aws.name
environment = each.key
variable_name = local.env_vars_git[each.key][0]["env_var_name"]
value = local.env_vars_git[each.key][0]["env_var_value"]
}

enter image description here


更多回答

The first step is to transform your variable into a more optimal structure. Is that option available to you in this situation, or are you forced to work with the current structure?

第一步是将变量转换为更优化的结构。在这种情况下,您是否可以选择该选项,或者您是否被迫使用当前的结构?

@MatthewSchuchard - thank you. I'm looking for a better structure. This structure works, but trying to create environment twice.

@MatthewSchuchard-谢谢。我在找一种更好的结构。这种结构奏效了,但试图两次创造环境。

优秀答案推荐

One way I have done nested loops in terraform is using modules...

我在Terraform中实现嵌套循环的一种方法是使用模块...



  • Outside we loop over the outer elements (environments)

  • Pass the inner elements list as a parameter

  • Inside the module we loop over the inner elements (env variables)


Below is an example:

下面是一个示例:


I'm simplifying your locals but once you get the concept you can recomplicate that later if you like.

我正在简化你的本地人,但一旦你理解了概念,如果你愿意,你可以稍后再把它复杂化。


locals {
env_vars_git = {
production = {
hostname_01 = "foo_prod"
hostname_02 = "bar_prod"
},
development = {
hostname_01 = "foo_dev"
hostname_02 = "bar_dev"
}
}
}

module "test" {
source = "./env_vars"

for_each = local.env_vars_git
repository = "github_repository.aws.name"
environment = each.key
variables = each.value
}

then the module code is:

则模块代码为:


variable "repository" {
type = string
}

variable "environment" {
type = string
}

variable "variables" {
type = map(string)
}

resource "null_resource" "sh_test" {
for_each = var.variables
provisioner "local-exec" {
when = create
command = "echo '${each.value}' "
interpreter = ["/bin/sh", "-c"]
}
}

You can see that I'm using a null_resource to just to output the values, but the same can be done with any other resource

您可以看到,我使用的是NULL_RESOURCE来输出值,但对任何其他资源也可以这样做


...and terraform apply looks like

...和Terraform应用看起来像


Do you want to perform these actions?
Terraform will perform the actions described above.
Only 'yes' will be accepted to approve.

Enter a value: yes

module.test["development"].null_resource.sh_test["hostname_01"]: Creating...
module.test["development"].null_resource.sh_test["hostname_01"]: Provisioning with 'local-exec'...
module.test["development"].null_resource.sh_test["hostname_01"] (local-exec): Executing: ["/bin/sh" "-c" "echo 'foo_dev' "]
module.test["development"].null_resource.sh_test["hostname_01"] (local-exec): foo_dev
module.test["development"].null_resource.sh_test["hostname_01"]: Creation complete after 0s [id=7355436237029689557]

module.test["production"].null_resource.sh_test["hostname_01"]: Creating...
module.test["production"].null_resource.sh_test["hostname_01"]: Provisioning with 'local-exec'...
module.test["production"].null_resource.sh_test["hostname_01"] (local-exec): Executing: ["/bin/sh" "-c" "echo 'foo_prod' "]
module.test["production"].null_resource.sh_test["hostname_01"] (local-exec): foo_prod
module.test["production"].null_resource.sh_test["hostname_01"]: Creation complete after 0s [id=4455254562097512830]

module.test["development"].null_resource.sh_test["hostname_02"]: Creating...
module.test["development"].null_resource.sh_test["hostname_02"]: Provisioning with 'local-exec'...
module.test["development"].null_resource.sh_test["hostname_02"] (local-exec): Executing: ["/bin/sh" "-c" "echo 'bar_dev' "]
module.test["development"].null_resource.sh_test["hostname_02"] (local-exec): bar_dev
module.test["development"].null_resource.sh_test["hostname_02"]: Creation complete after 0s [id=2018334306873294541]

module.test["production"].null_resource.sh_test["hostname_02"]: Creating...
module.test["production"].null_resource.sh_test["hostname_02"]: Provisioning with 'local-exec'...
module.test["production"].null_resource.sh_test["hostname_02"] (local-exec): Executing: ["/bin/sh" "-c" "echo 'bar_prod' "]
module.test["production"].null_resource.sh_test["hostname_02"] (local-exec): bar_prod
module.test["production"].null_resource.sh_test["hostname_02"]: Creation complete after 0s [id=5766017297644124093]


I also have the code here:

https://github.com/heldersepu/hs-scripts/tree/master/TerraForm/module_loop

我这里也有代码:https://github.com/heldersepu/hs-scripts/tree/master/TerraForm/module_loop


更多回答

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com