Reputation: 1256
I am working on using Point In Time Recovery(PITR) for AWS DynamoDB.
DynamoDB Table A was provisioned using Terraform. After PITR is enabled on table A, I managed to restore it to a new table A-Backup using CLI based on the instruction from AWS Documentation.
After the restore is done, I have updated lambda to use the new table name A-Backup as the new value for the table environment variables.
But now the question is how to sync this change in Terraform to make sure Terraform reflects the change that is made, which is a new table that has been created from PITR?
What's the best practice here?
Upvotes: 8
Views: 8397
Reputation: 424
Here is the process I have used to keep Terraform up-to-date when restoring a DynamoDB table.
Example Terraform table
# dynamo.tf
resource "aws_dynamodb_table" "bird_sightings" {
name = "bird_sightings_original"
point_in_time_recovery {
enabled = true
}
}
Restore backup with AWS CLI
aws dynamodb restore-table-to-point-in-time \
--source-table-name "bird_sightings_original" \
--target-table-name "bird_sightings_restored" \
--restore-date-time `date -v -7d +"%s"` # timestamp for 7 days ago
aws dynamodb wait table-exists --table-name bird_sightings_restored
Update Terraform state and apply missing settings
# Remove original table from Terraform state (does not delete table)
terraform state rm aws_dynamodb_table.bird_sightings
# Update table name in terraform config file
sed -i "s/bird_sightings_original/bird_sightings_restored/g" dynamo.tf
# Import new table into the Terraform state as the original resource
terraform import aws_dynamodb_table.bird_sightings bird_sightings_restored
# Apply settings that AWS does not copy to backups (tags, point in time recovery, stream settings, etc)
terraform apply
Terraform is a great compliment to the AWS recovery process because AWS does not copy all table settings to the backups. Terraform will tell you what's missing and update your tables.
Upvotes: 12
Reputation: 1256
The only solution I found is to just use terraform import
to import the new table change to Terraform. That should be sufficient.
https://www.terraform.io/docs/commands/import.html
Upvotes: 0