Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
PeteDuncanson opened this issue Nov 26, 2020 · 1 comment
Closed

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

PeteDuncanson opened this issue Nov 26, 2020 · 1 comment

Comments

@PeteDuncanson
Copy link

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
Copy link
Owner

You need to use the promise version mentioned in this comment
#12 (comment)
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;
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants