Home / AWS

AWSRunbookAdvanced

Sev1: DynamoDB table deleted by mistake, restore from PITR or AWS Backup

A production DynamoDB table was deleted and the app returns ResourceNotFoundException. Find the newest backup, restore under the original name, then rebuild the settings a restore does not carry over.

Deepak Bhardwaj6 min read

Applies toAmazon DynamoDB (on-demand and provisioned), point-in-time recovery, on-demand backups, AWS Backup, AWS CLI v2

Symptoms

  • Application errors: ResourceNotFoundException: Requested resource not found on every read and write to one table.
  • aws dynamodb describe-table returns ResourceNotFoundException.
  • Lambda functions triggered by the table's stream stop receiving events.
  • CloudTrail shows a DeleteTable call, often from a teardown script, a Terraform destroy against the wrong workspace, or a CloudFormation stack deletion.

There is no undelete. You restore into a new table. If you restore with the same name, clients reconnect without a config change.

Step 1: confirm the deletion and who did it (2 minutes)

TABLE=orders
aws dynamodb describe-table --table-name $TABLE 2>&1 | head -2

aws cloudtrail lookup-events --lookup-attributes AttributeKey=EventName,AttributeValue=DeleteTable \
  --max-results 10 --query "Events[].[EventTime,Username,Resources[0].ResourceName]" --output table

If the deletion came from IaC, stop that pipeline now. A second apply or destroy can delete the restored table too.

Step 2: find the newest restore source

Check in this order.

System backup from PITR. If point-in-time recovery was enabled when the table was deleted, DynamoDB keeps a system backup of the table as it was at deletion:

aws dynamodb list-backups --table-name $TABLE --backup-type SYSTEM \
  --query "BackupSummaries[].[BackupName,BackupCreationDateTime,BackupStatus,BackupArn]" --output table

On-demand backups you created:

aws dynamodb list-backups --table-name $TABLE --backup-type USER \
  --query "BackupSummaries[].[BackupName,BackupCreationDateTime,BackupArn]" --output table

AWS Backup recovery points:

aws backup list-recovery-points-by-resource \
  --resource-arn arn:aws:dynamodb:eu-west-1:111122223333:table/$TABLE \
  --query "RecoveryPoints[].[CreationDate,Status,RecoveryPointArn,BackupVaultName]" --output table
What you find Data you get back Use
SYSTEM backup exists Everything up to the moment of deletion Step 3A
Only USER backups Up to the last on-demand backup Step 3A, then replay newer data if you can
Only AWS Backup recovery points Up to the last scheduled job Step 3B
Nothing Nothing from DynamoDB Rebuild from the source of truth (event log, S3 export, upstream system)

Also check for exports to S3 (aws dynamodb list-exports) and for a global table replica in another Region. If a replica survived, it holds the current data and you can point clients at that Region while you rebuild.

Step 3: restore under the original name

Before you start, capture what you remember about the table from IaC or the last describe-table output in logs: key schema, GSIs, LSIs, billing mode, encryption key. A restore lets you override some of these.

3A: restore from a DynamoDB backup (SYSTEM or USER)

aws dynamodb restore-table-from-backup \
  --target-table-name $TABLE \
  --backup-arn arn:aws:dynamodb:eu-west-1:111122223333:table/orders/backup/01700000000000-abcdef12 \
  --billing-mode-override PAY_PER_REQUEST

aws dynamodb wait table-exists --table-name $TABLE
aws dynamodb describe-table --table-name $TABLE --query "Table.[TableStatus,ItemCount,TableArn]"

Add --sse-specification-override Enabled=true,SSEType=KMS,KMSMasterKeyId=<key-arn> if the table used a customer managed key. To restore only some secondary indexes, use --global-secondary-index-override.

3B: restore from AWS Backup

Get the metadata the restore job expects, then start the job:

RP=arn:aws:backup:eu-west-1:111122223333:recovery-point:abcd-1234
aws backup get-recovery-point-restore-metadata --backup-vault-name prod-vault --recovery-point-arn $RP

aws backup start-restore-job --recovery-point-arn $RP \
  --iam-role-arn arn:aws:iam::111122223333:role/service-role/AWSBackupDefaultServiceRole \
  --metadata TargetTableName=$TABLE
aws backup describe-restore-job --restore-job-id <id> --query "[Status,PercentDone,StatusMessage]"

Restore time depends on table size, not on the backup age. Large tables can take hours. Tell stakeholders early and do not promise a time you have not measured.

Warning

ItemCount in describe-table is updated periodically, not in real time. Verify with application-level checks or a Select=COUNT scan on a small table, not by watching that number.

Step 4: rebuild what a restore does not bring back

A restored table gets data, keys, indexes and encryption. It does not get these, so reapply them before declaring recovery:

Setting Command
Point-in-time recovery aws dynamodb update-continuous-backups --table-name $TABLE --point-in-time-recovery-specification PointInTimeRecoveryEnabled=true
TTL aws dynamodb update-time-to-live --table-name $TABLE --time-to-live-specification "Enabled=true,AttributeName=expiresAt"
Streams aws dynamodb update-table --table-name $TABLE --stream-specification StreamEnabled=true,StreamViewType=NEW_AND_OLD_IMAGES
Auto scaling (provisioned mode) Re-register Application Auto Scaling targets and policies
Tags aws dynamodb tag-resource --resource-arn <arn> --tags Key=app,Value=orders
CloudWatch alarms Recreate or confirm they point at the table name
Resource-based policy aws dynamodb put-resource-policy if you used one

Streams need extra care. The new stream has a new ARN, so existing Lambda event source mappings point at nothing:

NEW_STREAM=$(aws dynamodb describe-table --table-name $TABLE --query "Table.LatestStreamArn" --output text)
aws lambda list-event-source-mappings --function-name orders-stream-handler \
  --query "EventSourceMappings[].[UUID,EventSourceArn,State]" --output table
aws lambda create-event-source-mapping --function-name orders-stream-handler \
  --event-source-arn $NEW_STREAM --starting-position LATEST

Delete the old mapping once the new one is Enabled. If you use TRIM_HORIZON instead of LATEST, downstream consumers will reprocess every restored item as a new insert; make sure they are idempotent first.

For a former global table, the restore creates a single-Region table. Add replicas back with aws dynamodb update-table --replica-updates after the table is ACTIVE.

Step 5: reconcile the data gap

  • With a SYSTEM backup the gap is only writes that failed after deletion. Callers got errors, so check whether they retried, queued to a DLQ or dropped data.
  • With an older backup, list the window between the backup time and the deletion and replay from upstream sources, application logs or an S3 export.

Prevent it next time

Turn on deletion protection for every production table. A DeleteTable call then fails until someone turns it off deliberately:

aws dynamodb update-table --table-name $TABLE --deletion-protection-enabled
  • Enable PITR on all production tables; it is what gives you a backup at the moment of deletion.
  • In Terraform, add deletion_protection_enabled = true and lifecycle { prevent_destroy = true }. In CloudFormation, set DeletionProtectionEnabled: true and DeletionPolicy: Retain.
  • Deny dynamodb:DeleteTable on production tables in an SCP, with an exemption for a break-glass role.
  • Copy AWS Backup recovery points to a vault in another account with Vault Lock, so a compromised account cannot delete both table and backups.
  • Alert on DeleteTable and UpdateTable events that disable deletion protection.
  • Test a full restore each quarter and record how long it took for your largest table.