Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS DynamoDB Backup using CDK

I want to create a DynamoDB table and backup using AWS Typescript CDK. Creating DynamoDB using CDK is pretty straightforward, but implementing backup is not easy. Could anyone help me to implement a backup using CDK? I tried to solve this problem, but not enough references on the internet. I would appreciate it if anyone could provide a full example of this scenario. Thanks in advance.

I tried using thishttps://aws-cdk.com/aws-backup/, but not really helpful.

like image 531
Andy Avatar asked Jan 23 '26 04:01

Andy


2 Answers

An example I'm using

const DataTable = new dynamodb.Table(this, 'Example', {
    tableName: 'Example',
    partitionKey: {
        name: 'id',
        type: dynamodb.AttributeType.STRING
    },
    sortKey: {
        name: 'name',
        type: dynamodb.AttributeType.STRING
    },
    pointInTimeRecovery: true,
    billingMode: dynamodb.BillingMode.PAY_PER_REQUEST
});
// Backup rules
// https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_backup-readme.html
const backupVault = new backup.BackupVault(this, "ExampleBackupVault", {
  backupVaultName: "ExampleBackupVault" 
})
const plan = new backup.BackupPlan(this, "ExampleBackupPlan")
plan.addRule(backup.BackupPlanRule.weekly(backupVault))
plan.addRule(backup.BackupPlanRule.monthly5Year(backupVault))
plan.addSelection("ExampleBackupSelection", {
    resources: [backup.BackupResource.fromDynamoDbTable(DataTable)]
})

like image 54
longzhiw Avatar answered Jan 25 '26 20:01

longzhiw


you can use the below code to use DynamoDB table with on-demand backup this one on python

from constructs import Construct
from aws_cdk import (
    Duration,
    Stack,
    aws_backup as backup,
    aws_dynamodb as dynamodb,
)


class CdkWorkshopStack(Stack):

    def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None:
        super().__init__(scope, construct_id, **kwargs)
        table = dynamodb.Table(self, "my-table",
        partition_key=dynamodb.Attribute(
        name="id", 
        type=dynamodb.AttributeType.STRING
    )
        )
        backup_vault = backup.BackupVault(self, "MyBackupVault", backup_vault_name="testvault")
        backup_plan = backup.BackupPlan(self, "MyBackupPlan",
                                         backup_vault=backup_vault,)
        backup_plan.add_selection("Selection",
         resources=[
             backup.BackupResource.from_dynamo_db_table(table)
         ])
        backup_plan.add_rule(backup.BackupPlanRule.weekly())
like image 29
rick Avatar answered Jan 25 '26 18:01

rick