通过AWS lambda连接到EC2服务器

时间:2017-02-23 09:05:41

标签: node.js amazon-web-services amazon-ec2 aws-lambda

我的EC2服务器上有一个shell脚本,我希望从AWS lambda函数触发相同的脚本。任何人都可以建议我如何在我的lambda函数中访问该文件。 lambda和EC2之间没有连接问题。

我使用Putty gen生成私钥并将其保存在s3存储桶中并使用相同的密钥进行连接(使用此私钥可以通过putty连接)。我有这样的代码。

var driver, ssh;
driver = require('node-ssh');
ssh = new driver();
exports.handle = function(error, ctx, cb) {
ssh = new driver({
          host: 'EC2 public ip',
          username: 'uname',
          privateKey : 'url of s3/privatekey.ppk'
        });
ssh.connect().then(function() {
    console.log('connected')
        },function(error) {
            console.log(error);

        });
 }

首先,我试图查看是否可以连接到我的EC2服务器,然后我可以通过ssh客户端运行shell脚本。但是连接没有发生。记下以下错误。

{
"errorMessage": "config.host must be a valid string",
"errorType": "Error",
"stackTrace": [
"Object.<anonymous> (/var/task/node_modules/node-ssh/lib/helpers.js:15:13)",
"next (native)",
"step (/var/task/node_modules/node-ssh/lib/helpers.js:69:191)",
"/var/task/node_modules/node-ssh/lib/helpers.js:69:437",
"Object.<anonymous> (/var/task/node_modules/node-ssh/lib/helpers.js:69:99)",
"Object.normalizeConfig (/var/task/node_modules/node-  ssh/lib/helpers.js:42:17)",
"/var/task/node_modules/node-ssh/lib/index.js:53:25",
"SSH.connect (/var/task/node_modules/node-ssh/lib/index.js:52:14)",
"exports.handle (/var/task/index.js:13:7)"
]
}

1 个答案:

答案 0 :(得分:-1)

发布这个问题的答案。希望它会有所帮助。

 package com.wb.mars.ingest;

import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import com.wb.mars.ingest.CustomEventInput;
import com.wb.mars.ingest.CustomEventOutput;


public class EC2ConnectLambda implements RequestHandler<CustomEventInput,CustomEventOutput> {

    public CustomEventOutput handleRequest(CustomEventInput input, Context context) {
        context.getLogger().log("Input: " + input);
         System.out.println("test");
    try {

        String command1 = "cd /home/ec2-user/mydir; ./runjar.sh";


        JSch jsch = new JSch();

        String user = "ec2-user";

        String host = "*.*.*.*";
        int port = 22;

        //File file = new File( EC2ConnectLambda.class.getResource( "/Linux_EC2.pem" ).toURI() );
        File file = new File( EC2ConnectLambda.class.getResource( "/mykey.pem" ).toURI() );

        String privateKeyabsolutePath = file.getAbsolutePath();

        jsch.addIdentity(privateKeyabsolutePath);
        System.out.println("identity added ");

        Session session = jsch.getSession(user, host, port);
        System.out.println("session created.");

        java.util.Properties config = new java.util.Properties();
        config.put("StrictHostKeyChecking", "no");
        session.setConfig(config);

        session.connect();
        System.out.println("session connected.....");

        ChannelExec channel = (ChannelExec)session.openChannel("exec");
        OutputStream o = channel.getOutputStream();
        PrintWriter pw = new PrintWriter(o);
        InputStream in = channel.getInputStream();
        ((ChannelExec) channel).setCommand(command1);

        channel.connect();

        // 4 - Clean up
        channel.disconnect();
        session.disconnect();

    } catch (Exception e) {
        System.err.println(e);
        e.printStackTrace();
    }
    return new CustomEventOutput("lambdaInvoked");
}

}