skip to Main Content

I want to return items which are being held in DynamoDB using Terraform. This data source will then be used to construct a Tagging Policy

I want to hold cost centre numbers in DynamoDB, to the help construct Tagging Policies

resource "aws_dynamodb_table" "poc" {
  name         = "poc"
  billing_mode = "PAY_PER_REQUEST"
  hash_key     = "tag_key"
  range_key    = "tag_value"

  attribute {
    name = "tag_key"
    type = "S"
  }

  attribute {
    name = "tag_value"
    type = "S"
  }


  point_in_time_recovery {
    enabled = false
  }
}

resource "aws_dynamodb_table_item" "poc1" {
  table_name = aws_dynamodb_table.poc.name
  hash_key   = aws_dynamodb_table.poc.hash_key
  range_key  = aws_dynamodb_table.poc.range_key  
  depends_on = [aws_dynamodb_table.poc]

  item = <<ITEM
{
  "tag_key": {
    "S": "CostCenter"
  },
  "tag_value": {
    "S": "1111"
  }
}
ITEM
}

I can build the dynamoDB and upload some data to it.

data "aws_dynamodb_table_item" "poc" {
  table_name = aws_dynamodb_table.poc.name
  key                   = <<KEY
{
    "tag_key": {"S": "CostCenter"}
}
KEY
  depends_on            = [aws_dynamodb_table_item.poc]
}

But i am struggling to get the "key" right for the data source. For the Primary key i am using both Partition key and sort key. But i am struggling with the syntax needed in the data source.

Is anyone able to help point out my error?

│ Error: reading Amazon DynamoDB Table Item Data Source (poc|tag_key||CostCenter|): ValidationException: The provided key element does not match the schema

I basically want to list all the items which have "CostCenter" as Partition key as the data source

Thanks

2

Answers


  1. You must provide the sort key as well:

    For a composite primary key, you must provide values for both the partition key and the sort key.

    If you want a list of items maybe you could use a for_each.

    Login or Signup to reply.
  2. Per the documentation:

    key – (Required) A map of attribute names to AttributeValue objects, representing the primary key of the item to retrieve. For the primary key, you must provide all of the attributes. For example, with a simple primary key, you only need to provide a value for the partition key. For a composite primary key, you must provide values for both the partition key and the sort key.

    You’ve defined a range (sort) key on your table, so your table’s primary key is a composite of the hash key and the range (sort) key. You will have to provide both values in order to the aws_dynamodb_table_item datasource in order to retrieve the item.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search