3

I'm trying to access sftp server using an aws lambda function but it keeps returning null and I'm not sure why. The host, username and password are correct because I tested the connection using WinSCP. I also tried connecting to other free sftp servers but the response kept returning null. I'm not sure what I'm doing wrong in the script. I did install the npm packages, then zipped the file and uploaded the zip file to aws lambda.

exports.handler = async (event) => {
    let Client = require('ssh2-sftp-client');
    let Path = '/path';

    let sftp = new Client();
    sftp.connect({
        host: 'host', 
        port: 22,
        username: 'username',
        password: 'password'
    }).then(() => {
        return sftp.list(Path);
    }).then(() => {
        context.done();
    }).catch((err) => {
        console.log('Catch Error: ', err);
        context.fail();
    });
}; 
10
  • "kept returning null" —What exactly is returning null? How are you determining that it is returning null? Is there an error message about it? What does that error message say? Commented Jul 20, 2019 at 18:23
  • Why have you declared your handler function as async when you don't use await in it? Commented Jul 20, 2019 at 18:24
  • @Quentin I was trying to return the file path but it returns null, even when I insert a string in place of the path, it still returns null. I'm not sure what I'm doing wrong. Also I'm using this in aws lambda. Commented Jul 20, 2019 at 18:47
  • Just to rule this out: Is your Lambda Function running inside a VPC? If yes, do the subnets the function gets deployed to have internet access? Commented Jul 20, 2019 at 19:09
  • @Becca — Again what exactly returns null and how can you tell? You have about 10 function calls there, and nothing to inspect the results of any of them (except the console.log in the catch). Commented Jul 20, 2019 at 19:18

1 Answer 1

4

It's been 9 months so I hope it is solved but if anyone else needs the solution:

exports.handler = (event) => {
    let Client = require('ssh2-sftp-client');
    let Path = '/path';

    let sftp = new Client();
    return sftp.connect({
        host: 'host', 
        port: 22,
        username: 'username',
        password: 'password'
    }).then(() => {
        return sftp.list(Path);
    }).then((list) => {
        // If you want to do something with the list, do it here 
        // else just return the list from the function above and remove this
        return list;
    }).catch((err) => {
        console.log('Catch Error: ', err);
        throw new Error(err);
    });
}; 
Sign up to request clarification or add additional context in comments.

Comments

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.