2

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();
    });
}; 
Becca
  • 169
  • 1
  • 4
  • 12
  • "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? – Quentin Jul 20 '19 at 18:23
  • Why have you declared your `handler` function as `async` when you don't use `await` in it? – Quentin Jul 20 '19 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. – Becca Jul 20 '19 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? – Maurice Jul 20 '19 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). – Quentin Jul 20 '19 at 19:18
  • @Maurice I didn't set any VPC. In the network section, I did set the Virtual Private Cloud (VPC) option to No VPC. I'm not sure if that's causing the issue. – Becca Jul 20 '19 at 19:39
  • @Quentin it's returning null instead of the path in the server. I'm not sure if my network setup for the lambda function is blocking it's access to the internet. – Becca Jul 20 '19 at 19:47
  • @Becca — "it's returning null instead of the path in the server" — How can you tell? As I said, you have nothing in that code which would inspect the value. And again, **what** is returning null? You keep saying "it" but you haven't explained which "it". – Quentin Jul 20 '19 at 20:24
  • @Quentin I'm referring to this line of code here ```return sftp.list(Path);``` – Becca Jul 20 '19 at 21:34
  • @Becca — How do you know it returns null? You never look at the return value of it. – Quentin Jul 20 '19 at 21:43

1 Answers1

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);
    });
}; 
Anders
  • 3,198
  • 1
  • 20
  • 43