Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DynamoDB restore to a new table and update Terraform

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?

like image 507
codigube Avatar asked Jan 30 '20 09:01

codigube


People also ask

How do you update multiple items in a DynamoDB table?

The only way to update multiple items at the same time is use TransactionWrite operation provided by DynamoDB. But it comes with a limitation (25 at most for example). So keep in mind with that, you probable should do some limitation in your application as well.

What are the two ways of retrieving data from a DynamoDB table?

To read data from a DynamoDB table, there are 3 ways: get-item – This is used in AWS Command Line Interface (CLI). To retrieve an item you must specify a table name and keys that you want to retrieve. Query – The items in the table can be retrieved by querying on the table.

Does DynamoDB overwrite?

By default, the DynamoDB write operations ( PutItem , UpdateItem , DeleteItem ) are unconditional: Each operation overwrites an existing item that has the specified primary key.

How do I update a file in DynamoDB?

To update an existing item in an Amazon DynamoDB table, you use the UpdateItem operation. You must provide the key of the item that you want to update. You must also provide an update expression, indicating the attributes that you want to modify and the values that you want to assign to them.


1 Answers

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.

like image 83
mihow Avatar answered Sep 21 '22 15:09

mihow