0

I am writing a lambda that would restore my Database schema into RDS. I have packed node modules in the zip that i have uploaded on aws lambda console. What is the issue over here? where i am going wrong?

This is my lambda function

var  MysqlTools= require('mysql-tools');
var endpoint = process.env.MYSQLDB_HOST;
var username = process.env.MYSQLDB_USER;
var database = process.env.MYSQLDB_DATABASE;
var password = process.env.MYSQLDB_PASSWORD;

exports.restore = (event,context)=>{
  var tool = new MysqlTools();
  tool.restoreDatabase({host: endpoint, user: username, password:password, sqlFilePath: 'epmoliteDump.sql', database:  database
  }, function (error, output, message) {
     if (error instanceof Error) {
        console.log(error);
     } else {
        console.log(output);
        console.log(message);
     }
  });
}

Cloud watch error log

START RequestId: 303cd306-9ed3-11e7-874e-a5a3ce668160 Version: $LATEST 2017-09-21T13:45:54.531Z 303cd306-9ed3-11e7-874e-a5a3ce668160 { Error: Command failed: mysql -h me10uo7cwmpj.c8icuw7t60la.us-east-1.rds.amazonaws.com -u xxxxxx -pxxxxxx eLite_DB

at ChildProcess.exithandler (child_process.js:204:12)
at emitTwo (events.js:106:13)
at ChildProcess.emit (events.js:191:7)
at maybeClose (internal/child_process.js:886:16)
at Process.ChildProcess._handle.onexit (internal/child_process.js:226:5)   killed: false,   code: 127,  

signal: null, cmd: 'mysql -h me10uo7cwmpj.c8icuw7t60la.us-east-1.rds.amazonaws.com -u xxxxxx -pxxxxxx eLite_DB

1
  • 1
    The mysql-tools library is just a wrapper around the mysql and mysqldump commands. It doesn't actually include those tools inside the NodeJS library. Those tools are a dependency that it expects you to have installed already. You would need to package those binaries in your Lambda deployment package. I'm not aware of anyone successfully installing and running the mysql or mysqldump utilities on AWS Lambda at this time.
    – Mark B
    Sep 21, 2017 at 14:17

1 Answer 1

1

You cannot use mysql-tools as it expects you to have the mysql utilities in your $PATH and there seems to be no way of configuring it in mysql-tools.

You can, however, include the mysql binary in your Lambda package provided that it is built for the matching version of Amazon Linux.

Assuming the binary's location is at <root directory of lambda function>/bin/mysql, you can change your code like this:

const exec = require('child_process').exec
const endpoint = process.env.MYSQLDB_HOST;
const username = process.env.MYSQLDB_USER;
const database = process.env.MYSQLDB_DATABASE;
const password = process.env.MYSQLDB_PASSWORD;

exports.restore = (event, context) => exec(`${process.env['LAMBDA_TASK_ROOT']}/bin/mysql -u ${username} -p${password} -h ${endpoint} ${database} < epmoliteDump.sql`, function (error, output, message) {
   if (error instanceof Error) {
      console.log(error);
   } else {
      console.log(output);
      console.log(message);
   }
});
1
  • It talks about dumping the backup, i am asking for restoring my existing SQL dump to RDS
    – Veer3383
    Sep 22, 2017 at 8:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.