Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding multiple DynamoDB items through terraform

How to add multiple items in dynamoDB table?

  table_name = "${var.environment}-kaleidos-dynamodb-MappingConfig"
  hash_key   = "eventType"
  item = <<EOF
   json
EOF
}````

DynamoDB always expects one item. Is there a way to provide multiple items?
like image 867
Vijaya Senthil Kumar Avatar asked Aug 08 '26 00:08

Vijaya Senthil Kumar


1 Answers

There's no way to add multiple items using a single resource "aws_dynamodb_table_item". You can have multiple resource statements in the same file, as long as you give them different names, for example:

resource "aws_dynamodb_table_item" "item1" {
  ...
}

resource "aws_dynamodb_table_item" "item2" {
  ...
}

If you are trying to create items based on an array or map or a specific number, you can use count or for_each (for_each was introduced in 0.12.6)

count example:

resource "aws_dynamodb_table_item" "items" {
  count = 4
  item <<EOF
{
  "pk": {"S": "${count.index}"}
}
EOF

for_each example:

resource "aws_dynamodb_table_item" "items" {
  for_each = {
    item1 = {
      something = "hello"
    }
    item2 = {
      something = "hello2"
    }
  }
  item = <<EOF
{
  "pk": {"S": "${each.key}"},
  "something": {"S": "${each.value.something}"}
}
EOF
}
like image 170
sleepy_keita Avatar answered Aug 09 '26 20:08

sleepy_keita