Skip to content

Instantly share code, notes, and snippets.

@Dineshkarthik
Last active June 20, 2021 10:27
Show Gist options
  • Save Dineshkarthik/d0944c45b06726a327a9536a33dabdd2 to your computer and use it in GitHub Desktop.
Save Dineshkarthik/d0944c45b06726a327a9536a33dabdd2 to your computer and use it in GitHub Desktop.

Revisions

  1. Dineshkarthik revised this gist Jun 21, 2018. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions dynamodb_replicate_table.py
    Original file line number Diff line number Diff line change
    @@ -40,8 +40,8 @@ def replicate(table_name, existing_region, new_region, new_table_name):
    KeySchema=existing_table.key_schema,
    AttributeDefinitions=existing_table.attribute_definitions,
    ProvisionedThroughput={
    'ReadCapacityUnits': 1,
    'WriteCapacityUnits': 1
    'ReadCapacityUnits': 5,
    'WriteCapacityUnits': 5
    })
    print("Table status:", table.table_status)

  2. Dineshkarthik revised this gist Jun 21, 2018. 1 changed file with 1 addition and 5 deletions.
    6 changes: 1 addition & 5 deletions dynamodb_replicate_table.py
    Original file line number Diff line number Diff line change
    @@ -52,11 +52,7 @@ def replicate(table_name, existing_region, new_region, new_table_name):
    print("Updating table with data...")
    if table.table_status == 'ACTIVE':
    for item in items:
    response = table.put_item(Item={
    'key': item['key'],
    'value': item['value']
    })

    response = table.put_item(Item=item)
    print("PutItem status:",
    response['ResponseMetadata']['HTTPStatusCode'])

  3. Dineshkarthik revised this gist Jun 21, 2018. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion dynamodb_replicate_table.py
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,6 @@
    # Copyright (C) 2018 Dineshkarthik Raveendran

    from __future__ import print_function # Python 2/3 compatibility
    import time
    import boto3
    import argparse

  4. Dineshkarthik revised this gist Jun 21, 2018. 1 changed file with 4 additions and 5 deletions.
    9 changes: 4 additions & 5 deletions dynamodb_replicate_table.py
    Original file line number Diff line number Diff line change
    @@ -41,15 +41,14 @@ def replicate(table_name, existing_region, new_region, new_table_name):
    KeySchema=existing_table.key_schema,
    AttributeDefinitions=existing_table.attribute_definitions,
    ProvisionedThroughput={
    'ReadCapacityUnits': 5,
    'WriteCapacityUnits': 5
    'ReadCapacityUnits': 1,
    'WriteCapacityUnits': 1
    })
    print("Table status:", table.table_status)

    # It take 10 - 15 seconds for the table to be created
    time.sleep(15)
    table.wait_until_exists()
    table.reload()

    table = dynamodb.Table(new_table_name)
    print("Table status:", table.table_status)
    print("Updating table with data...")
    if table.table_status == 'ACTIVE':
  5. Dineshkarthik revised this gist Jun 20, 2018. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions dynamodb_replicate_table.py
    Original file line number Diff line number Diff line change
    @@ -41,8 +41,8 @@ def replicate(table_name, existing_region, new_region, new_table_name):
    KeySchema=existing_table.key_schema,
    AttributeDefinitions=existing_table.attribute_definitions,
    ProvisionedThroughput={
    'ReadCapacityUnits': 1,
    'WriteCapacityUnits': 1
    'ReadCapacityUnits': 5,
    'WriteCapacityUnits': 5
    })
    print("Table status:", table.table_status)

  6. Dineshkarthik revised this gist Jun 20, 2018. No changes.
  7. Dineshkarthik revised this gist Jun 20, 2018. No changes.
  8. Dineshkarthik created this gist Jun 20, 2018.
    98 changes: 98 additions & 0 deletions dynamodb_replicate_table.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,98 @@
    # Copyright (C) 2018 Dineshkarthik Raveendran

    from __future__ import print_function # Python 2/3 compatibility
    import time
    import boto3
    import argparse


    def replicate(table_name, existing_region, new_region, new_table_name):
    """
    Replicate table in new region.
    Creates a new table with existing KeySchema and AttributeDefinitions
    read and write capacity untis are set to 5. Change it as required.
    Parameters
    ----------
    table_name: string
    Name of the existing table to be replicated.
    existing_region: string
    Region in which the table is present.
    new_region: string
    Region in which the table needs to be replicated.
    new_table_name: string
    Name for the new table to be created, if not given
    existing table name is used.
    """

    existing_table = boto3.resource(
    'dynamodb', region_name=existing_region).Table(table_name)
    items = existing_table.scan()['Items']

    dynamodb = boto3.resource('dynamodb', region_name=new_region)

    print("Creating table '{0}' in region '{1}'".format(
    new_table_name, new_region))

    table = dynamodb.create_table(
    TableName=new_table_name,
    KeySchema=existing_table.key_schema,
    AttributeDefinitions=existing_table.attribute_definitions,
    ProvisionedThroughput={
    'ReadCapacityUnits': 1,
    'WriteCapacityUnits': 1
    })
    print("Table status:", table.table_status)

    # It take 10 - 15 seconds for the table to be created
    time.sleep(15)

    table = dynamodb.Table(new_table_name)
    print("Table status:", table.table_status)
    print("Updating table with data...")
    if table.table_status == 'ACTIVE':
    for item in items:
    response = table.put_item(Item={
    'key': item['key'],
    'value': item['value']
    })

    print("PutItem status:",
    response['ResponseMetadata']['HTTPStatusCode'])

    print("Total items created:", table.scan()['Count'])


    if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument(
    '-t',
    '--table_name',
    type=str,
    required=True,
    help="Name of the table to be replicated in new region")
    parser.add_argument(
    '-r',
    '--region',
    type=str,
    required=True,
    help="Region in which the table is present")
    parser.add_argument(
    '-nr',
    '--new_region',
    type=str,
    required=True,
    help="Region in which the table needs to be replicated")
    parser.add_argument(
    '-nt',
    '--new_table_name',
    type=str,
    help="Name for the new table [Optional], Old table name will be used")
    args = parser.parse_args()
    if args.new_table_name is None:
    args.new_table_name = args.table_name

    replicate(args.table_name, args.region, args.new_region,
    args.new_table_name)