【问题标题】:Dynamically importing secondary indexes in terraform for dynamodb在 terraform 中为 dynamodb 动态导入二级索引
【发布时间】:2019-12-29 04:03:23
【问题描述】:

我正在尝试将几个 dynamodb 表导入到 terraform。我被困在如何动态处理环境之间的全局二级索引上。 每个环境都有一个模块和两个状态文件。

如何使用 count 动态输入这些变量,这些变量会在环境之间变化,

例如,在下面的示例中,有 4 个索引,但对于 prod 帐户中的特定索引,读取容量和写入容量会发生变化,而所有其他变量保持不变。

即last-index对prod和nonprod的读写容量值不同

如何在 terraform 中实现?

模块:

locals {
  name           = ["xxx-index","xxx-index","xxx-index","xxx-index","last-index"]
  write_capacity = [ 5,5,5,5,5]
  read_capacity  = [ 5,5,5,5,5]
  range_key      = ["xxx","xxx","xxx","xxx","xxx"]

}

  global_secondary_index {
    count           = "${length(local.name)}"
    name            = "${element(local.name, count.index)}"
    write_capacity  = "${element(local.write_capacity, count.index)"
    read_capacity   = "${element(local.read_capacity, count.index)"
    hash_key        = "userId"
    range_key       = "${element(local.range_key,count.index)}"
    projection_type = "ALL"
  }

Terraform -版本 Terraform v0.11.13 + provider.aws v2.25.0

【问题讨论】:

    标签: amazon-dynamodb terraform terraform-provider-aws


    【解决方案1】:

    对于 Terraform 0.11,这个问题没有合理的答案。它缺少描述您正在寻找的转换所需的原语,并且不支持动态生成嵌套块。

    Terraform 0.11 中最受支持的事情是将索引的数量固定为常数,但仍会改变各个部分,如下所示:

    resource "aws_dynamodb_table" "example" {
      # ...
    
      global_secondary_index {
        name            = "${local.name[0]}"
        write_capacity  = "${local.write_capacity[0]}"
        read_capacity   = "${local.read_capacity[0]}"
        range_key       = "${local.range_key[0]}"
        hash_key        = "userId"
        projection_type = "ALL"
      }
    
      global_secondary_index {
        name            = "${local.name[1]}"
        write_capacity  = "${local.write_capacity[1]}"
        read_capacity   = "${local.read_capacity[1]}"
        range_key       = "${local.range_key[1]}"
        hash_key        = "userId"
        projection_type = "ALL"
      }
    
      global_secondary_index {
        name            = "${local.name[2]}"
        write_capacity  = "${local.write_capacity[2]}"
        read_capacity   = "${local.read_capacity[2]}"
        range_key       = "${local.range_key[2]}"
        hash_key        = "userId"
        projection_type = "ALL"
      }
    
      global_secondary_index {
        name            = "${local.name[3]}"
        write_capacity  = "${local.write_capacity[3]}"
        read_capacity   = "${local.read_capacity[3]}"
        range_key       = "${local.range_key[3]}"
        hash_key        = "userId"
        projection_type = "ALL"
      }
    
      global_secondary_index {
        name            = "${local.name[4]}"
        write_capacity  = "${local.write_capacity[4]}"
        read_capacity   = "${local.read_capacity[4]}"
        range_key       = "${local.range_key[4]}"
        hash_key        = "userId"
        projection_type = "ALL"
      }
    }
    

    为处理此用例而添加的新 Terraform 0.12 功能是 dynamic blocks,它允许根据集合值生成零个或多个特定类型的块。

    例如:

    locals {
      indices = {
        "xxx-index" = {
          write_capacity = 5
          read_capacity  = 5
          range_key      = "xxx"
        },
        "last-index" = {
          write_capacity = 5
          read_capacity  = 5
          range_key      = "xxx"
        },
      }
    }
    
    resource "aws_dynamodb_table" "example" {
      # ...
    
      dynamic "global_secondary_index" {
        for_each = local.indices
        content {
          name            = global_secondary_index.key
          write_capacity  = global_secondary_index.value.write_capacity
          read_capacity   = global_secondary_index.value.read_capacity
          range_key       = global_secondary_index.value.range_key
          hash_key        = "userId"
          projection_type = "ALL"
        }
      }
    }
    

    【讨论】:

    • 我尝试使用这种 terraform 0.12 动态方法,但 global_secondary_index.value.write_capacity 与除 global_secondary_index.key 之外的其他方法一起出现错误。
    猜你喜欢
    • 1970-01-01
    • 2019-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-15
    • 1970-01-01
    相关资源
    最近更新 更多