繁体   English   中英

如何使用for循环连接Terraform输出中的字符串?

[英]How to concatenate strings in Terraform output with for loop?

我有多个aws_glue_catalog_table资源,我想创建一个循环所有资源的output ,以显示每个资源的 S3 存储桶位置。 这样做的目的是测试我是否为 Terratest 中的每个资源使用了正确的location (因为它是变量的串联)。 我不能使用aws_glue_catalog_table.*aws_glue_catalog_table.[]因为 Terraform 不允许在不指定名称的情况下引用资源。

所以我用r1r2rx创建了一个variable "table_names" 然后,我可以遍历名称。 我想动态创建字符串aws_glue_catalog_table.r1.storage_descriptor[0].location ,以便我可以检查location是否正确。

resource "aws_glue_catalog_table" "r1" {
  name          = "r1"
  database_name = var.db_name
  storage_descriptor {
    location      = "s3://${var.bucket_name}/${var.environment}-config/r1"
  }
...
}
resource "aws_glue_catalog_table" "rX" {
  name          = "rX"
  database_name = var.db_name
  storage_descriptor {
    location      = "s3://${var.bucket_name}/${var.environment}-config/rX"
  }
}

variable "table_names" {
  description = "The list of Athena table names"
  type        = list(string)
  default     = ["r1", "r2", "r3", "rx"]
}
output "athena_tables" {
  description = "Athena tables"
  value = [for n in var.table_names : n]
}

第一次尝试:我尝试使用语法aws_glue_catalog_table.${table}创建一个output "athena_tables_location" ,但确实如此。

output "athena_tables_location" {
  // HOW DO I ITERATE OVER ALL TABLES?
  value = [for t in var.table_names : aws_glue_catalog_table.${t}.storage_descriptor[0].location"]
}

第二次尝试:我尝试创建一个variable "table_name_locations"但 IntelliJ 已经在 for 循环中显示错误${t} [for t in var.table_names : "aws_glue_catalog_table.${t}.storage_descriptor[0].location"] .

variable "table_name_locations" {
  description = "The list of Athena table locations"
  type        = list(string)
  // THIS ALSO DOES NOT WORK
  default     = [for t in var.table_names : "aws_glue_catalog_table.${t}.storage_descriptor[0].location"]
}

如何列出output中的所有表位置,然后使用 Terratest 对其进行测试? 一旦我可以遍历表并收集 S3 位置,我就可以使用 Terratest 进行以下测试:

athenaTablesLocation := terraform.Output(t, terraformOpts, "athena_tables_location")
assert.Contains(t, athenaTablesLocation, "s3://rX/test-config/rX",)

似乎您在这里有一个不寻常的静态和动态混合:您已经静态定义了固定数量的aws_glue_catalog_table资源,但您想根据输入变量的值动态使用它们。

Terraform 不允许动态引用资源,因为它的执行模型需要在所有对象之间构建依赖关系图,因此它需要知道特定表达式中涉及哪些确切资源。 但是,原则上您可以构建自己的包含所有这些对象的单个值,然后从中动态选择:

locals {
  tables = {
    r1 = aws_glue_catalog_table.r1
    r2 = aws_glue_catalog_table.r2
    r3 = aws_glue_catalog_table.r3
    # etc
  }
}

output "table_locations" {
  value = {
    for t in var.table_names : t => local.tables[t].storage_descriptor[0].location
  }
}

通过这个结构,Terraform 可以看到output "table_locations"取决于local.tableslocal.tables取决于所有相关资源,因此评估顺序将是正确的。


但是,您的表定义似乎也是基于var.table_names系统化的,因此可能会从动态本身中受益。 您可以使用资源for_each功能来声明单个资源的多个实例:

variable "table_names" {
  description = "Athena table names to create"
  type        = set(string)
  default     = ["r1", "r2", "r3", "rx"]
}

resource "aws_glue_catalog_table" "all" {
  for_each = var.table_names

  name          = each.key
  database_name = var.db_name
  storage_descriptor {
    location      = "s3://${var.bucket_name}/${var.environment}-config/${each.key}"
  }
  ...
}

output "table_locations" {
  value = {
    for k, t in aws_glue_catalog_table.all : k => t.storage_descriptor[0].location
  }
}

在这种情况下, aws_glue_catalog_table.all将所有表一起表示为具有多个实例的单个资源,每个实例由表名称标识。 for_each资源作为映射出现在表达式中,因此这将使用如下地址声明资源实例:

  • aws_glue_catalog_table.all["r1"]
  • aws_glue_catalog_table.all["r2"]
  • aws_glue_catalog_table.all["r3"]
  • ...

因为这已经是一个map了,这次我们不需要在本地值中构建map的额外步骤,而是可以直接访问这个map来构建输出值,这将是一个从表名到存储的映射地点:

{
  r1 = "s3://BUCKETNAME/ENVNAME-config/r1"
  r2 = "s3://BUCKETNAME/ENVNAME-config/r2"
  r3 = "s3://BUCKETNAME/ENVNAME-config/r3"
  # ...
}

在这个例子中,我假设所有的表除了它们的名字外都是相同的,我希望这在实践中不是真的,但我只是按照你在问题中包含的内容进行。 如果表确实需要具有不同的设置,那么您可以将var.table_names更改为variable "tables"其类型是对象类型的映射,其中值描述表之间的差异,但这是一个不同的主题,有点超出这个问题的范围,所以我不会在这里详细介绍。

暂无
暂无

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

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