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 showFix it fast
Most common cause, 1 of 4The 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.
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" }
})
}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.
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.
terraform apply 2>&1 | grep -oE "[A-Za-z]+Exception[^,]*"Then find the line you see in this list and open its fix:
- 1The item JSON lacks a key attribute or uses the wrong key type
ValidationException, and the key names or S/N/B types in item differ from the table's KeySchema and AttributeDefinitions - 2A key attribute is an empty string
ValidationException, and the plan shows the key value as "", often from a variable or local that was never set - 3An item with the same key already exists in the table
ConditionalCheckFailedException on create, for an item that was written by an app, the console or a lost Terraform state - 4The table name, Region or table status is wrong
ResourceNotFoundException, 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 ValidationExceptionConditionalCheckFailedExceptionResourceNotFoundException
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.
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.
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.
aws dynamodb get-item --table-name example-name --key '{"exampleHashKey": {"S": "something"}}'Confirmed by docs.aws.amazon.com
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.
terraform plan | grep -n -A12 aws_dynamodb_table_itemFix
Pass a real value for the key, and stop empty input early with a validation rule on the variable that feeds it.
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.
terraform plan -var=user_id=u-123Confirmed by docs.aws.amazon.com
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.
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.
import {
to = aws_dynamodb_table_item.example
id = "example-name,something"
}Verify
terraform plan shows the item as imported, with no create step.
terraform planConfirmed by raw.githubusercontent.com
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.
aws dynamodb describe-table --table-name example-name --query Table.TableStatusFix
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.
table_name = aws_dynamodb_table.example.nameVerify
describe-table prints ACTIVE in the provider's Region and the item applies.
terraform applyConfirmed by raw.githubusercontent.com
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.