amazon-web-servicesamazon-dynamodbaws-sdkaws-sdk-jsdynamoose

switch Dynamodb region on the fly


Is there any way to switch between the DynamoDB regions on the fly by using javascript aws-sdk or dynamoose npm package?

Suppose, 2 table exists in two different regions Mumbai (ap-south-1) and Frankfurt (eu-central-1). Initial when appliation boots it uses ap-south-1 and then after some business logic, it needs to route the DB instance to eu-central-1.

So, how can I achieve it by using javascript aws-sdk or dynmoose package.

Right now, I am using the below snippet. But it's not working

import dynamoose from 'dynamoose';
dynamoose.AWS.config.logger = console;
dynamoose.AWS.config.update({ region: 'ap-south-1'}); // working here

// some business login
// ...
// ...

// switch DynamoDB region
dynamoose.AWS.config.update({ region: 'eu-central-1'});  // but it's not working. Not even throwing any error.


Can anyone please help? Is there any way to log the AWS Dynamodb region the specific query run on? Like enabling AWS logger, it just outputs

[AWS dynamodb 200 0.042s 0 retries] getItem({ TableName: 'User', Key: { userId: { S: '99999' } } })

Solution

  • In this case I think you will have far better luck by setting a new DynamoDB instance each time. For example your example would change to the following.

    import {DynamoDB} from "aws-sdk";
    dynamoose.setDDB(new DynamoDB({ region: 'ap-south-1'}));
    
    // some business login
    // ...
    // ...
    
    // switch DynamoDB region
    dynamoose.setDDB(new DynamoDB({ region: 'eu-central-1'}));
    

    This will change the underlying instance that Dynamoose uses to communicate with DynamoDB as opposed to the global AWS SDK settings.

    Note, the code above changed in v2 of Dynamoose. See breaking changes for more information.