amazon-web-services - terraform 计划 'string required' dynamodb_table_item

标签 amazon-web-services terraform infrastructure-as-code

我需要向 dynamodb_table_item 资源添加一组字符串。

# my.tfvars

client_days = [
  "2021-05-08",               # May
  "2021-06-12", "2021-06-26", # June
]

# main.tf
variable "client_days" {
  type        = set(string)
  description = "Client days."
}

resource "aws_dynamodb_table_item" "client_days" {
  table_name = aws_dynamodb_table.periods.name
  hash_key   = "name"

  item = <<EOF
{
  "name": { "S": "client-days" },
  "days": {
      "SS" : "${client_days}"
  }
}
EOF
}

结果如下所示:

 32:   item = <<EOF
  33: {
  34:   "name": { "S": "client-days" },
  35:   "days": {
  36:       "SS" : "${var.client_days}"
  37:   }
  38: }
  39: EOF
    |----------------
    | var.client_days is set of string with 11 elements

Cannot include the given value in a string template: string required.

我不知道如何解决这个问题。 我还尝试使用 join() 将该列表转换为字符串。

最佳答案

您必须使用jsonencode :

resource "aws_dynamodb_table_item" "client_days" {
  table_name = "testdb"
  hash_key   = "name"

  item = <<EOF
{
  "name": { "S": "client-days" },
  "days": {
      "SS" : ${jsonencode(var.client_days)}
  }
}
EOF
}

关于amazon-web-services - terraform 计划 'string required' dynamodb_table_item,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67315751/

相关文章:

amazon-web-services - 如何将现有堆栈转换为嵌套堆栈

azure - Terraform:如何将 PS 模块从库安装到 Azure 自动化中?

amazon-web-services - map 变量中的局部变量

typescript - 如何使用 AWS CDK 检测控制台/堆栈偏差

amazon-web-services - Terraform启用vpc流日志到亚马逊s3

amazon-web-services - 将多个私有(private)子网附加到每个 terraform 的路由表

java - 在 aws 服务器上启动 tomcat 8 的问题

amazon-web-services - 如何更改从 AWS Step Function Map 并行运行的 Glue 作业的最大并发运行次数?

Azure 可用区参数语法

node.js - 使用 Node js aws-sdk 检查 S3 上是否存在文件夹