【问题标题】:Adding multiple DynamoDB set items through terraform通过 terraform 添加多个 DynamoDB 集合项
【发布时间】:2021-05-18 13:44:05
【问题描述】:

terraform 版本:0.12.20

我想在 terraform 中添加多个具有 set 数据类型的项目,我浏览了 link,其中一个示例展示了如何将简单数据类型添加为 String,但它无法使用 Set 添加

下面是我正在测试的代码

resource "aws_dynamodb_table_item" "items" {
  hash_key   = "key"
  table_name = "test"
  for_each = {
    "72" = {
      test = ["114717","2"],
      test1 = []
    },
    "25" = {
      test = ["114717"],
      test1 = []
    }
  }
  item = <<EOF
{
  "key": {"S": "${each.key}"},
  "test": {"SS": "${each.value.test}"},
  "test1": {"SS": "${each.value.test1}"}
}
EOF
}

但是,Cannot include the given value in a string template: string required. 失败

我尝试了类似的东西

resource "aws_dynamodb_table_item" "items" {
  hash_key   = "key"
  table_name = "test"
  for_each = {
    "72" = {
      test = "114717,2",
      test1 = ""
    },
    "25" = {
      test = "114717",
      test1 = ""
    }
  }
  item = <<EOF
{
  "key": {"S": "${each.key}"},
  "test": {"SS": ["${each.value.test}"]},
  "test1": {"SS": ["${each.value.test1}"]}
}
EOF
}

这无法将 "114717,2" 区分为两个不同的项目

在第二个示例中,我什至也尝试了以下部分

{
  "key": {"S": "${each.key}"},
  "test": {"SS": "${split(",",each.value.test)}"},
  "test1": {"SS": "${split(",",each.value.test1)}"}
}

Cannot include the given value in a string template: string required. 也会失败

我希望能够将值拆分为数组["114717","2"]。这将帮助我在 DynamoDB 中将值存储为 Set

【问题讨论】:

  • “这无法将“114717,2”区分为两个不同的项目”是什么意思?实际结果是什么,为什么会出错,预期的结果应该是什么?
  • @Marcin 我已经更新了我的问题以便更好地理解
  • whiteListedCustomersblackListedCustomers 是什么?您的代码中没有定义此类变量。
  • @Marcin 对不起,我做了代码混淆,错过了删除那些,更新了问题

标签: amazon-web-services terraform terraform-provider-aws terraform0.12+


【解决方案1】:

您的item 应该是有效的 json。为此,您可以使用jsonencode:

resource "aws_dynamodb_table_item" "items" {
  hash_key   = "key"
  table_name = "GameScores"
  for_each = {
    "72" = {
      test = ["114717","2"],
      test1 = []
    },
    "25" = {
      test = ["114717"],
      test1 = []
    }
  }
  item = <<EOF
{
  "key": {"S": "${each.key}"},
  "test": {"SS": ${jsonencode(each.value.test)}},
  "test1": {"SS": ${length(each.value.test1) > 0 ? jsonencode(each.value.test1) : jsonencode([""])}}
}
EOF
}

还有SS 不能为空,所以你必须考虑到这一点。因此,您必须检查并使用[""] 数组。或者你必须重新考虑如果你的test1[] 怎么办。

【讨论】:

  • 我也试过了,terraform计划由于SS作为数组的约束而失败,将在位中发布确切的错误
  • @Sach 代码有效。我运行它并验证了它。
猜你喜欢
  • 1970-01-01
  • 2022-01-14
  • 1970-01-01
  • 2020-01-11
  • 1970-01-01
  • 2019-02-08
  • 1970-01-01
  • 1970-01-01
  • 2011-12-03
相关资源
最近更新 更多