Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages
Databases and Redis

terraform apply fails to write an aws_dynamodb_table_item

Your error

creating DynamoDB Table (example-name) Item: operation error DynamoDB: PutItem, https response error StatusCode: 400, RequestID: ...3 more lines it can show

Fix it fast

Most common cause, 1 of 4

The item JSON lacks a key attribute or uses the wrong key type

Write each key in the item with the same name and type descriptor as the table, and set range_key on the resource when the table has a sort key.

Fix
resource "aws_dynamodb_table_item" "example" {
  table_name = aws_dynamodb_table.example.name
  hash_key   = aws_dynamodb_table.example.hash_key
  item = jsonencode({
    exampleHashKey = { S = "something" }
    one            = { N = "11111" }
  })
}
Did this fix it?
Full steps for this cause: check, fix, verify

Short answer

operation error DynamoDB: PutItem, https response error StatusCode: 400 from aws_dynamodb_table_item is a rejected write, and the exception name tells why. ValidationException: a key is missing, empty or the wrong type. ConditionalCheckFailedException: the item already exists, so import it. ResourceNotFoundException: wrong table name or Region. Applies to AWS provider 6.x.

  • Applies to Terraform 1.17.2, hashicorp/aws provider 6.73.0, Amazon DynamoDB PutItem API
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Which cause do you have?

Run the apply again and pull out the exception name that follows StatusCode: 400. ValidationException points to cause 1 or 2, ConditionalCheckFailedException to cause 3, and ResourceNotFoundException to cause 4.

Run this first
terraform apply 2>&1 | grep -oE "[A-Za-z]+Exception[^,]*"

Then find the line you see in this list and open its fix:

  1. 1The item JSON lacks a key attribute or uses the wrong key typeValidationException, and the key names or S/N/B types in item differ from the table's KeySchema and AttributeDefinitions
  2. 2A key attribute is an empty stringValidationException, and the plan shows the key value as "", often from a variable or local that was never set
  3. 3An item with the same key already exists in the tableConditionalCheckFailedException on create, for an item that was written by an app, the console or a lost Terraform state
  4. 4The table name, Region or table status is wrongResourceNotFoundException, often when table_name is a hard-coded string or the provider points at another Region

What does the error look like?

  • creating DynamoDB Table (example-name) Item: operation error DynamoDB: PutItem, https response error StatusCode: 400, RequestID: ...
  • api error ValidationException
  • ConditionalCheckFailedException
  • ResourceNotFoundException

Cause 1 of 4

The item JSON lacks a key attribute or uses the wrong key type

You seeValidationException, and the key names or S/N/B types in item differ from the table's KeySchema and AttributeDefinitions

Check

DynamoDB only accepts a put that carries every primary key attribute, both the partition key and, for a composite key, the sort key. Key attributes must also use the data type set in the table definition. A string key written as {"N": ...} is refused.

Check
aws dynamodb describe-table --table-name example-name --query "Table.[KeySchema,AttributeDefinitions]"

Fix

Write each key in the item with the same name and type descriptor as the table, and set range_key on the resource when the table has a sort key. Referencing the table's own attributes keeps the names in step.

Fix
resource "aws_dynamodb_table_item" "example" {
  table_name = aws_dynamodb_table.example.name
  hash_key   = aws_dynamodb_table.example.hash_key
  item = jsonencode({
    exampleHashKey = { S = "something" }
    one            = { N = "11111" }
  })
}

Verify

terraform apply creates the item, and get-item returns it by its key.

Verify
aws dynamodb get-item --table-name example-name --key '{"exampleHashKey": {"S": "something"}}'

Confirmed by docs.aws.amazon.com

Did this fix it?

Cause 2 of 4

A key attribute is an empty string

You seeValidationException, and the plan shows the key value as "", often from a variable or local that was never set

Check

Empty strings are allowed for ordinary attributes, but a String or Binary value used as a table or index key must be longer than zero. An unset Terraform variable that defaults to an empty string produces exactly this rejected put.

Check
terraform plan | grep -n -A12 aws_dynamodb_table_item

Fix

Pass a real value for the key, and stop empty input early with a validation rule on the variable that feeds it.

Fix
variable "user_id" {
  type = string
  validation {
    condition     = var.user_id != ""
    error_message = "user_id is the table key and cannot be empty."
  }
}

Verify

terraform plan now fails fast with the validation message when the value is missing, and applies when it is set.

Verify
terraform plan -var=user_id=u-123

Confirmed by docs.aws.amazon.com

Did this fix it?

Cause 3 of 4

An item with the same key already exists in the table

You seeConditionalCheckFailedException on create, for an item that was written by an app, the console or a lost Terraform state

Check

The AWS provider sends its create with the condition attribute_not_exists on the hash key, so it never overwrites an item it did not create. If that key is already in the table, DynamoDB fails the condition and returns 400.

Check
aws dynamodb get-item --table-name example-name --key '{"exampleHashKey": {"S": "something"}}'

Fix

Bring the existing item under Terraform with an import block that names the table and the key value, then plan again. Add the range key value after a second comma when the table has a sort key.

Fix
import {
  to = aws_dynamodb_table_item.example
  id = "example-name,something"
}

Verify

terraform plan shows the item as imported, with no create step.

Verify
terraform plan

Confirmed by raw.githubusercontent.com

Did this fix it?

Cause 4 of 4

The table name, Region or table status is wrong

You seeResourceNotFoundException, often when table_name is a hard-coded string or the provider points at another Region

Check

DynamoDB answers ResourceNotFoundException with HTTP 400 when the table does not exist where the request went, or is not yet ACTIVE. A typed-in table name is not a reference, so Terraform cannot infer that the table must exist before the item.

Check
aws dynamodb describe-table --table-name example-name --query Table.TableStatus

Fix

Point table_name at the table resource instead of a string, so Terraform orders the two and uses the same provider Region. Check the Region on the provider block if the table lives elsewhere.

Fix
table_name = aws_dynamodb_table.example.name

Verify

describe-table prints ACTIVE in the provider's Region and the item applies.

Verify
terraform apply

Confirmed by raw.githubusercontent.com

Did this fix it?

How do I stop it coming back?

  • Build item JSON with jsonencode and the table's own hash_key and range_key attributes, never hand-typed key names.
  • Give every variable that becomes a key a validation block, so empty input fails at plan time.
  • Keep aws_dynamodb_table_item for small seed or config rows; the provider docs say it is not designed for large amounts of data.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine on the same stack: Dispatch Pro, Raheeb, Loade Driver.

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.