4

I want to create a function in AWS Lambda Nodejs that can send some SSH commands to a linux machine. Is it possible?

I know there are some nodejs modules to do this, but AWS Lambda Nodejs doesn't have them, and I don't want to involve any EC2 to this Lambda.

I just want to know how can I, from AWS Lambda function, execute some commands in a linux machine, either by SSH or some other methods that I couldn't think of.

4
  • Can you do a REST call to the other server to accomplish what you want? Commented Dec 17, 2015 at 3:16
  • or, maybe you can invoke the shell using something like this? dzone.com/articles/execute-unix-command-nodejs but will Lambda even allow you to ssh would be an interesting question to answer Commented Dec 17, 2015 at 3:19
  • I don't have an answer to your question, however just wanted to mention, Lambda currently does not have the ability to interact with any resources behind a VPC. Check out this project, it might prove useful? alestic.com/2014/11/aws-lambda-environment Commented Dec 17, 2015 at 14:44
  • Thanks guy, but I think I'll just use my home server API for now Commented Dec 17, 2015 at 17:20

3 Answers 3

1

This post is relatively old now but for whoever finds this, AWS made a blog post describing mostly what you're looking for: https://aws.amazon.com/fr/blogs/compute/scheduling-ssh-jobs-using-aws-lambda/

Obviously the AWS blog post describes how to use this for AWS Services but the example still stands using python.

Sign up to request clarification or add additional context in comments.

Comments

0

Number of options: You can popen ssh from Python, use a package like https://www.npmjs.com/package/simple-ssh from nodejs, etc.

1 Comment

This question is titlled "Send SSH commands from AWS Lambda (NodeJS)". Whereas simple-ssh package allows interacting with the ssh executable on your system via nodejs. It is NOT enough to run ssh on lambda. (Because AWS Lambda machine doesn't have ssh executable installed). Thus this as accepted answer is a bit misleading as it doesn't solve core OPs problem
0

Can confirm this is working on lambda with Alexa and simple-ssh. Couple things of note: ssh.on("close" is required because otherwise the lambda function will shut down before the SSH command is finished working. (Lambda shuts down on alexa.execute call.)

This is one of the top Google results for "ssh lambda aws" so I'm posting this here in hopes of saving some time for others that need to do this.

Working example: https://github.com/PockyBum522/alexa_nodejs_send_ssh_commands_lambda

var SSH = require('simple-ssh');
var Alexa = require("alexa-sdk");

var ssh = new SSH({
    host: 'yourserver.com',
    user: 'username',
    key: `-----BEGIN RSA PRIVATE KEY-----
MIIEpAIBAAKCAQEApQtRxugrDMU8YNBy2j2Lyk6yAxMSKaiusrNUamXKLxFvdlZ1
6HCN+jjaE7q8OYFEmq9l2B5U8GCYMFldXWBxIv7fvRWyi1ZTw3olaZ8DmGYwPKLM
TOQ3MOm/zcJZbiTY1/cx2CW6NupPX78O42hLKM2iJwp6epgxC5t2Vw==
-----END RSA PRIVATE KEY-----`
});

exports.handler = function(event, context, callback) {
    var alexa = Alexa.handler(event, context);
    alexa.registerHandlers(handlers);

    ssh.on("close", function () {alexa.execute()});
    ssh
        .exec('nohup /home/username/script.sh > /dev/null 2>&1 &', {  // Nohup runs script in BG, > /dev/null redirects output. & also runs in BG. Trying to keep things fast to make alexa response time good.
            out: console.log.bind(console)
        })
        .exec('exit', {                                               // Also for trying to keep things fast.
            out: console.log.bind(console)
        }).start(); 
};

var handlers = {
    'LaunchRequest': function(){
        this.emit(':tell', 'Now opening the garage door.'); 
    }
};

1 Comment

Not sure why this have been downvoted. This is the correct answer! And it's still working in 2019

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.