Kaguei Nakueka
Kaguei Nakueka

Reputation: 1128

Terraform: Add item to a DynamoDB Table

What is the correct way to add tuple and key-pair values items to a DynamoDB database via Terraform?

I am trying like this:

resource "aws_dynamodb_table_item" "item" {
    table_name = aws_dynamodb_table.dynamodb-table.name
    hash_key = aws_dynamodb_table.dynamodb-table.hash_key

    for_each = {
        "0" = {
            location = "Madrid"
            coordinates = [["lat", "40.49"], ["lng", "-3.56"]]
            visible = false
            destinations = [0, 4]
        }
    }

    item = <<ITEM
    {
        "id": { "N": "${each.key}"},
        "location": {"S" : "${each.value.location}"},
        "visible": {"B" : "${each.value.visible}"},
        "destinations": {"L" : [{"N": "${each.value.destinations}"}]
    }
    ITEM
}

And I am getting the message:

each.value.destinations is tuple with 2 elements │ │ Cannot include the given value in a string template: string required.

I also have no clue on how to add the coordinates variable.

Thanks!

Upvotes: 2

Views: 3570

Answers (3)

Kaguei Nakueka
Kaguei Nakueka

Reputation: 1128

"destinations": {"NS": ${jsonencode(each.value.destinations)}}

Did the trick!

Upvotes: 1

bembas
bembas

Reputation: 771

List should be something like that :

"destinations": {"L": [{ "N" : 1 }, { "N" : 2 }]}

You are trying to pass

"destinations": {"L": [{ "N" : [0,4] }]}

Also you are missing the last } in destinations key

Upvotes: 3

Grzegorz Oledzki
Grzegorz Oledzki

Reputation: 24311

TLDR: I think the problem here is that you are trying to put L(N) - i.e. a list of numeric values, while your current Terraform code tries to put all the destinations into one N/number.

Instead of:

[{"N": "${each.value.destinations}"}]

you need some iteration over destinations and building a {"N": ...} of them.

Upvotes: 2

Related Questions