简体   繁体   English

Terraform 中 for_each 循环的动态输出

[英]Dynamic outputs from for_each loop in Terraform

I'm using 0.14.2 Terraform version and I have a task which I create dynamically a resource.我正在使用 0.14.2 Terraform 版本,我有一个动态创建资源的任务。 The task is the next:任务是下一个:

resource "aws_db_instance" "api-mariaDB" {
  for_each             = local.ob

  identifier           = "api-mariadb-${each.key}"
  allocated_storage    = "20"
  storage_type         = "gp2"
  engine               = "mariadb"
  engine_version       = "10.4.8"
  allow_major_version_upgrade = true
  auto_minor_version_upgrade = false
  instance_class       = "db.t2.micro"
  name                 = "ssapi"
  username             = "admin"
  password             = "Temporal123"
  db_subnet_group_name = aws_db_subnet_group.subnet-mariadb[each.value].name
  skip_final_snapshot  = true
  vpc_security_group_ids = [aws_security_group.rds_SG.id]
  tags = {
    Name        = "api-db-${each.key}"
  }
}

Using this local vars:使用这个本地变量:

locals {
    ob = toset([
     "es",
     "uk",
    ])
}

This resource create 2 RDS instance's with the next output:此资源使用下一个 output 创建 2 个 RDS 实例:

aws_db_instance.api-mariaDB["es"] will be created
  + resource "aws_db_instance" "api-mariaDB" {
      + address                               = (known after apply)
      + allocated_storage                     = 20
      + allow_major_version_upgrade           = true
      + apply_immediately                     = (known after apply)
      + arn                                   = (known after apply)
      .
      .
      .

I would like to access to the arn in both iteration to use after in a helm chart.. I tried with the next syntax but with no success:我想在两次迭代中访问 arn 以在 helm 图表中使用 after .. 我尝试使用下一个语法但没有成功:

output "rds" {
   value = {
     endpoint = "aws_db_instance.api-mariaDB[each.value].arn"
}

Any idea?任何想法?

Thanks!谢谢!

EDIT编辑

I tried this option also:我也试过这个选项:

output "rds" {
  value = {
    endpoint = aws_db_instance.api-mariaDB[*].arn
  }
}

But I got this error:但我得到了这个错误:

Error: Unsupported attribute

  on rds.tf line 49, in output "rds":
  49:     endpoint = aws_db_instance.api-mariaDB[*].arn

This object does not have an attribute named "arn".
The `*` operator will let you get a value from every item in a list. `*` 运算符将让您从列表中的每个项目中获取一个值。

For each creates a map, so you need to iterate over the keys:为每个创建一个 map,因此您需要遍历键:

 output "rds" { value = { endpoint = [for o in local.ob: aws_db_instance.api-mariaDB[o]].arn } }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM