Created
August 6, 2019 12:13
-
-
Save sgtoj/d193357ea3f95a68c18698df8c56d0ff to your computer and use it in GitHub Desktop.
Copy DynamoDB Table to Another Table
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import boto3 | |
# parameters for source account | |
SRC_ACCOUNT_PROFILE = 'aws_profile_name' | |
SRC_ACCOUNT_REGION = 'us-east-1' | |
SRC_TABLE = 'your-source-table-name' | |
# parameters for destination account | |
DST_ACCOUNT_PROFILE = 'aws_profile_name' | |
DST_ACCOUNT_REGION = 'us-east-1' | |
DST_TABLE = 'your-destination-table-name' | |
def pull_items(session, table_name): | |
ddb = session.client('dynamodb') | |
paginator = ddb.get_paginator('scan') | |
pages = paginator.paginate(TableName=table_name) | |
for page in pages: | |
items = page['Items'] | |
for item in items: | |
yield item | |
def push_items(session, table_name, items): | |
ddb = session.resource('dynamodb') | |
table = ddb.Table(table_name) | |
count = 0 | |
with table.batch_writer() as batch: | |
for item in items: | |
count = count + 1 | |
print(f'push item # {count}') | |
batch.put_item(Item=item) | |
def main(): | |
src_session = boto3.Session(region_name=SRC_ACCOUNT_REGION, profile_name=SRC_ACCOUNT_PROFILE) | |
dst_session = boto3.Session(region_name=DST_ACCOUNT_REGION, profile_name=DST_ACCOUNT_PROFILE) | |
items = pull_items(src_session, SRC_TABLE) | |
push_items(dst_session, DST_TABLE, items) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is a simple script that will copy a source ddb table to a destination table. This script doesn't copy the schema or secondary indexes. I will update this script if whenever I need to copy a ddb table with secondary indexes.