enGMzizo / copy-dynamodb-table

Copy Dynamodb table to another in the same or different zone , It is 100% safe. Speed depends on your destination table user-defined write provisioned throughput
130 stars 39 forks source link

Where is this intended to be run, in a Lambda? #30

Closed PeteDuncanson closed 3 years ago

PeteDuncanson commented 3 years ago

I've setup a Lambda to run this but I must be doing something wrong. I've hardcoded the API keys and secret in to my code (taken from the AWS Console https://aws.amazon.com/blogs/security/how-to-find-update-access-keys-password-mfa-aws-management-console/).

The Lamba runs but nothing is happening with the code, no errors are reported and nothing in the logs. I'm running the Lambda via the Test button in the web interface which should pass in a dummy event but I can't see where this code needs any of the environment vars it would get from running it in any other way and again its not complaining about anything so I think that should be ok?

What am I doing wrong?

var copy = require('copy-dynamodb-table').copy;

var globalAWSConfig = { 
  accessKeyId: 'MYKEY',
  secretAccessKey: 'MYSECRET',
  region: 'eu-west-1'
};

function copyTable( from, to ) {
    console.info( "Copying table " + from + " to " + to );

    copy({
        config: globalAWSConfig,
        source: {
            tableName: from, // required
        },
        destination: {
            tableName: to, // required
        },
        log: true, // default false
        create : true // create destination table if not exist
    },
    function (err, result) {
        if (err) {
            console.log(err);
        }
        console.log("Result", result );
    });

    console.info( "Done?");
}

exports.handler = async (event) => {
    console.info( event );
    copyTable( "my-source-table-dev", "my-target-table-live" );

    const response = {
        statusCode: 200,
        body: JSON.stringify('Hello from Lambda!'),
    };
    return response;
};
enGMzizo commented 3 years ago

You need to use the promise version mentioned in this comment https://github.com/enGMzizo/copy-dynamodb-table/issues/12#issuecomment-592869559 and update your code to support it.

var copy = require('copy-dynamodb-table').copy;

function promiseCopy(data) {
  return new Promise((resolve, reject) => {
    copy(data, function (err, result) {
      if (err) {
        return reject(err)
      }
      resolve(result)
    })
  })
}

var globalAWSConfig = { 
  accessKeyId: 'MYKEY',
  secretAccessKey: 'MYSECRET',
  region: 'eu-west-1'
};

async function copyTable( from, to ) {
    console.info( "Copying table " + from + " to " + to );

    await promiseCopy({
        config: globalAWSConfig,
        source: {
            tableName: from, // required
        },
        destination: {
            tableName: to, // required
        },
        log: true, // default false
        create : true // create destination table if not exist
    });

    console.info( "Done?");
}

exports.handler = async (event) => {
    console.info( event );
    await copyTable( "my-source-table-dev", "my-target-table-live" );

    const response = {
        statusCode: 200,
        body: JSON.stringify('Hello from Lambda!'),
    };
    return response;
};