title: AWS Lambda function with nodejs to ssh to ec2 and run command, on testing gives 200 success but does nothing
body: <p>I have an index.js written in node.js 8.10 with the necessary node modules uploaded and the pem file in an aws lambda function. The lambda function needs to ssh to an ec2 instance and run a python script (creates another file inside the directory) inside it. </p>
  
  <p>On running a test, I am getting 200 success but I don't see a new file (intended output of script). I am using simple-ssh to get to run the ec2 script.</p>
  
  <pre><code>'use strict';
  
  console.log('Loading lambda function');
  
  exports.handler = function(event, context, callback) {
  
    let bag_size = event.bag_size === undefined ? 10 : event.bag_size;
  
    var SSH = require('simple-ssh');
    var fs = require('fs');
    var ssh = new SSH({
        host: '##############',
        user: 'ubuntu',
        key: fs.readFileSync('key.pem'),
        passphrase: '##########'
        //pass: 'password'
    });
  
    var pythonCommand = 'python lambda_test.py ' + bag_size;
  
    ssh.exec('cd /home/ubuntu/***/***/***').exec('ls -al', {
     out: function(stdout) {
        console.log('ls -al got:');
        console.log(stdout);
        console.log('now launching command');
        console.log(pythonCommand);
     }
    }).exec('' + pythonCommand, {
    out: console.log.bind(console),
    exit: function(code, stdout, stderr) {
        console.log('operation exited with code: ' + code);
        console.log('STDOUT from EC2:\n' + stdout);
        console.log('STDERR from EC2:\n' + stderr);
        context.succeed('Success!');
    }
    }).start();
  
  
    var response = {
    statusCode: 200,
    headers: {
      "Access-Control-Allow-Credentials" : true, // Required for cookies, authorization headers with HTTPS 
      "Access-Control-Allow-Origin":"*",
      "Access-Control-Allow-Methods":"POST,GET,OPTIONS",
      "Access-Control-Allow-Headers":"Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token",
      "Content-Type":"application/json"
    },
    body: JSON.stringify({ "message": "Success" })
  };
  
    // Return response to the caller
    callback(null, response); 
  };
  </code></pre>
  
  <p>Log Output from CloudWatch:</p>
  
  <pre><code>START RequestId: 247cd************************480b Version: $LATEST
  END RequestId: 247cd************************480b
  REPORT RequestId: 247247cd************************5b480b0b  Duration: 10962.61 ms   Billed Duration: 11000 ms   Memory Size: 128 MB Max Memory Used: 49 MB  
  </code></pre>
  
  <p>I am not sure where I am going wrong. Please help!</p>