Schedule DynamoDB Exports to S3 with Lambda

published on 05 May 2025

Want to automate DynamoDB backups to S3? Here’s how you can set up an AWS Lambda function to schedule DynamoDB exports, ensuring consistent backups and easier data recovery.

Key Steps:

  1. Enable Point-in-Time Recovery (PITR) on your DynamoDB table to allow exporting data from any point within the last 35 days.
  2. Set up an S3 bucket with proper permissions to store the exported data.
  3. Create a Lambda function (Python or Node.js) to automate the export process, using environment variables for flexibility.
  4. Schedule the Lambda function using Amazon EventBridge to run at regular intervals (e.g., daily, weekly).

Tools and Permissions:

  • AWS CLI: For testing and deploying configurations.
  • IAM Role: Grant permissions for dynamodb:ExportTableToPointInTime and s3:PutObject.
  • CloudWatch: Monitor logs and export performance.

Costs to Consider:

  • DynamoDB Exports: $0.10 per GB.
  • S3 Storage: $0.023 per GB/month (Standard).
  • Lambda: $0.20 per million requests.

Benefits:

  • Consistency: Automate regular backups.
  • Scalability: Handle large tables without affecting performance.
  • Cost Control: Use S3 lifecycle rules to manage storage costs.

By following these steps, you can simplify your DynamoDB backup routines and ensure reliable data exports to S3.

Required Setup

AWS Services Setup

Here’s how to set up the necessary AWS services:

DynamoDB Configuration

  • Choose the DynamoDB table you want to work with.
  • Turn on Point-In-Time Recovery (PITR) for the selected table.

S3 Configuration

  • Set up a dedicated S3 bucket for storing DynamoDB exports.
  • Assign the proper permissions using an IAM role to ensure smooth operations.

IAM Role Permissions
The Lambda function will need an IAM role with these permissions:

  • dynamodb:ExportTableToPointInTime
  • s3:PutObject
  • s3:GetObject
  • s3:ListBucket

Lambda Function Setup

Follow these steps to configure the Lambda function:

  1. Runtime: Use Python 3.9 or a newer version.
  2. Memory: Allocate a minimum of 512 MB to handle export processes efficiently.
  3. Timeout: Set the timeout to 5 minutes for standard export tasks.
  4. Environment Variables:
    • DYNAMODB_TABLE_NAME
    • S3_BUCKET_NAME
    • S3_PREFIX (optional, for organizing exports in S3)

AWS Tools Overview

Once your services are configured, these tools will help you deploy and monitor your setup:

AWS Management Console

  • Use it to configure services.
  • Monitor export processes visually.
  • Access CloudWatch logs for debugging.

AWS CLI

  • Ideal for testing export commands.
  • Helps troubleshoot permission issues.
  • Supports script-based deployments for automation.

Ensure the AWS CLI is set up with the correct credentials and region. Run aws configure to input your access key, secret key, and default region.

Tool Primary Use Required Permission Level
AWS Console Service configuration Admin Access
AWS CLI Export testing Programmatic Access
CloudWatch Monitoring export tasks Read Access

DynamoDB and S3 Export Settings

Enable Point-in-Time Recovery

If Point-in-Time Recovery (PITR) isn’t enabled yet, here’s how to activate it:

  1. Open the DynamoDB console.
  2. Select the table you want to back up.
  3. Go to the Backups tab.
  4. Turn on Point-in-time recovery.

PITR keeps a rolling backup of your table for the past 35 days. You can export data from any point within this time frame. Keep in mind, this feature incurs additional costs based on your table size and the volume of write operations.

Export Configuration

Once PITR is enabled, set up your export options:

Source Configuration

  • Choose the DynamoDB table you want to export.
  • Select the export time (within the 35-day PITR window).
  • Pick the data format: either DynamoDB JSON or ION format.

Destination Settings

  • Define the S3 bucket path for storing the export.
  • Set an export prefix, like exports/[table-name]/[date], to organize files.
  • Configure encryption using either SSE-S3 or SSE-KMS.
Export Setting Description Recommended Value
Data Format File format for exported data DynamoDB JSON
S3 Prefix Folder structure for exports exports/[table-name]/YYYY-MM-DD
Compression Type of data compression GZIP
Manifest Format Metadata format for export JSON

After configuring these settings, run a manual test to ensure everything works as expected.

Test Export Process

Follow these steps to test your export:

  1. Export and Monitor
    • Manually start an export using your configured settings.
    • Keep an eye on the export task in the DynamoDB console.
  2. Verify Export Results
    • Check that the S3 bucket contains the following:
      • A manifest file with export metadata.
      • Compressed data files containing the table contents.
      • A logical file structure based on your export prefix.

Testing ensures your setup is correct and that the exported data is stored and organized properly in S3.

Lambda Export Automation

This process builds on the previously set up DynamoDB and S3 export configurations. After testing manual exports, you can now automate the export process using Lambda.

Write Lambda Function

Here's a Node.js Lambda function that automates DynamoDB exports, complete with error handling and logging:

const AWS = require('aws-sdk');
const dynamodb = new AWS.DynamoDB();

exports.handler = async (event) => {
    try {
        const params = {
            TableArn: process.env.TABLE_ARN,
            S3Bucket: process.env.S3_BUCKET,
            S3Prefix: `exports/${process.env.TABLE_NAME}/${new Date().toISOString().split('T')[0]}`, // Date formatted as YYYY-MM-DD
            ExportFormat: 'DYNAMODB_JSON'
        };

        const result = await dynamodb.exportTableToPointInTime(params).promise();
        console.log(`Export started: ${result.ExportDescription.ExportArn}`);

        return {
            statusCode: 200,
            body: JSON.stringify(result)
        };
    } catch (error) {
        console.error('Export failed:', error);
        throw error;
    }
};

This function uses environment variables for flexibility. Check the Lambda Function Setup for details on configuring these variables. Once the function is ready, schedule it with EventBridge for regular execution.

Set Export Schedule

Use Amazon EventBridge to schedule your Lambda function. Below are some common scheduling options:

Schedule Type Cron Expression Description
Daily Backup 0 0 * * ? * Runs daily at midnight UTC
Weekly Backup 0 0 ? * SUN * Runs every Sunday at midnight
Monthly Backup 0 0 1 * ? * Runs on the first day of the month

To configure the schedule:

  • Open EventBridge and create a new rule.
  • Choose "Schedule" as the rule type.
  • Enter the appropriate cron expression.
  • Select your Lambda function as the target.
  • Ensure the function has the required IAM permissions.

Lambda Configuration Tips

Set up your Lambda function with these settings for optimal performance:

Setting Value Reason
Memory 512 MB Handles export tasks efficiently
Timeout 5 minutes Accounts for larger export operations
Retry Attempts 2 Handles temporary failures
Concurrent Executions 5 Prevents overuse of resources

You'll also need these IAM permissions for the Lambda function to work properly:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "dynamodb:ExportTableToPointInTime",
                "s3:PutObject"
            ],
            "Resource": [
                "arn:aws:dynamodb:*:*:table/*",
                "arn:aws:s3:::your-bucket/*"
            ]
        }
    ]
}

Error Handling

To ensure smooth operations, consider these practices:

  • Use exponential backoff for retries.
  • Set up CloudWatch alarms to track failed exports.
  • Configure SNS notifications to receive updates on export status.

Monitor the function's performance using CloudWatch metrics and logs. This will help you quickly identify and resolve any issues, ensuring your backups remain consistent and reliable.

sbb-itb-6210c22

Operations and Limits

Export Monitoring

You can keep track of DynamoDB exports using CloudWatch Logs. To identify completed exports, use CloudWatch Logs Insights with the following query:

fields @timestamp, @message
filter @message like /EXPORT_COMPLETED/
| sort @timestamp desc
| limit 20

Consider setting up a CloudWatch dashboard to visualize export performance and quickly spot any issues. Once you have a clear view of performance, assess the associated costs to ensure you're staying within budget.

Export Costs

Automated DynamoDB exports come with specific costs. Here's a quick breakdown:

Component Cost (US Region) Notes
DynamoDB Export $0.10/GB Does not use RCUs
S3 Standard Storage $0.023/GB/month Use lifecycle policies for cost control
Lambda Invocation $0.20/million requests Minimal impact for typical use

You can reduce expenses by applying S3 lifecycle rules, such as:

  • Moving exports older than 30 days to Glacier Instant Retrieval (at $0.004/GB/month).
  • Deleting exports that are older than 365 days.
  • Enabling Intelligent-Tiering to adjust for varying access patterns automatically.

Now, let’s look at the system limits that could affect your export operations.

System Limits

DynamoDB exports are subject to the following limits:

Limit Type Default Value Adjustable
Concurrent Exports 300 per account Yes
Total Export Size 100TB in-flight No
Export Duration 24 hours max No
API Rate 50 requests/second Yes

For tables larger than 100TB, you can split the export process using the ExportTime parameter.

To manage your export operations effectively, consider these strategies:

  • Use checkpointing via DynamoDB Streams for exports nearing the 24-hour limit.
  • Implement exponential backoff to handle API rate limits. For example:
const backoff = (attempt) => Math.min(100 * Math.pow(2, attempt), 2000);

Set up CloudWatch alarms to monitor:

  • The DynamoDBExportFailed metric.
  • Any Lambda errors exceeding zero.
  • S3 object counts to verify successful exports.

One key advantage: exports do not use read capacity units, making them an efficient option for large-scale data migrations without affecting table performance.

Summary

Streamline DynamoDB exports by setting up Point-in-Time Recovery, configuring an S3 bucket, and scheduling a Lambda function. Here are some key practices to ensure exports are reliable and cost-efficient:

  • Track performance and costs: Use CloudWatch to monitor exports and AWS Cost Explorer to review expenses for DynamoDB, S3, and Lambda.
  • Set up safeguards: Implement CloudWatch alarms and robust monitoring to catch potential issues early.
  • Check account limits: Ensure your export settings align with account limitations.

Adjust your export settings periodically based on:

  • Performance data
  • Cost-saving opportunities
  • Changes in data volume
  • Shifts in access patterns

Combining regular monitoring through CloudWatch with smart cost management via AWS Cost Explorer helps you maintain a system that’s both reliable and efficient. Make sure your export settings match your backup needs while keeping performance and costs in check.

FAQs

What permissions does my Lambda function need to export DynamoDB data to S3?

To ensure your Lambda function can export DynamoDB data to S3, you need to attach an IAM role with the appropriate permissions. This role should include:

  • DynamoDB permissions: Grant dynamodb:ExportTableToPointInTime to allow the function to initiate exports.
  • S3 permissions: Grant actions like s3:PutObject and s3:GetBucketLocation for the destination bucket.

Make sure the IAM role is correctly assigned to your Lambda function. You can define these permissions in the AWS Management Console or using an infrastructure-as-code tool like AWS CloudFormation or Terraform.

How can I reduce costs when storing DynamoDB export data in S3?

To optimize costs when storing DynamoDB export data in S3, consider the following strategies:

  • Use S3 storage classes wisely: Choose cost-effective storage classes such as S3 Glacier or S3 Glacier Deep Archive for infrequently accessed data. For data that requires quick retrieval, S3 Standard-Infrequent Access (S3 Standard-IA) can be a good option.
  • Enable S3 Lifecycle policies: Set up lifecycle rules to automatically transition older data to cheaper storage classes or delete data that is no longer needed.
  • Compress your data: Export data in compressed formats like GZIP or Parquet to reduce storage size and save costs.
  • Monitor and analyze usage: Use tools like AWS Cost Explorer or S3 Storage Lens to review your storage usage and identify cost-saving opportunities.

By implementing these strategies, you can effectively manage your S3 storage costs while maintaining access to your DynamoDB export data as needed.

What should I do if my scheduled DynamoDB exports to S3 are failing?

If your scheduled DynamoDB exports to S3 are failing, start by checking the AWS CloudWatch logs for your Lambda function. These logs often provide detailed error messages or clues about the issue, such as permission problems or resource limits.

Additionally, verify that your Lambda function's IAM role has the necessary permissions to access both DynamoDB and S3. Common permissions include dynamodb:ExportTableToPointInTime and s3:PutObject.

If the issue persists, ensure that your Lambda function is configured with the correct timeout settings and has sufficient memory allocated to handle the export process. Misconfigurations in these areas can cause the function to fail during execution.

Related posts

Read more