skip to Main Content

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.

4

Answers


  1. You can provision a back up table – ie, set up the resource.

    But CDK does not actually do anything by itself in the cloud. It only generates Cloudformation templates that it passes on to Cloudformation, and that service stands up resources for you. If you want something to run and back up your table to another table or a csv file in an s3 bucket or something, you need to use an AWS Lambda for example, to do this for you after the table is set up.

    The link you posted is not actually using CDK to do the backup, its using CDK to set up the AWS Backup service, which will do the backups for you.

    Login or Signup to reply.
  2. It might help if you clarify what backup you need. DynamoDB comes with Point-In-Time-Restore (PITR) backup feature, which you can easily enable via CDK. It gives you ability to restore table to any second within last 35 days. If you need a longer backup, then I would recommend using AWS Backup service which enables scheduled backups. You can easily configure it via CDK as per documentation.

    Login or Signup to reply.
  3. 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())
    
    Login or Signup to reply.
  4. 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)]
    })
    
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search