Can AWS Lambda reach/interact with S/FTP?

14,727

Solution 1

in short, ftp will not work with lambda since they use ephemeral ports.

sftp will work nicely with lambda. i tested using java code via jsch with no issues; tho i cant see how it wouldnt work with any js sftp lib.

Solution 2

It is possible tested just now. Make sure ur timeout is set to be long enough and you are calling context.succeed() on process termination

function __main__(event, context) {
    var JSFtp = require("jsftp");
    var ftp = new JSFtp({
        host: "speedtest.tele2.net",
        port: 21, // defaults to 21
    });
    ftp.ls(".", function(err, res) {
      var results = []; res.forEach(function(file) {
        results.push(file.name);
      });
      context.succeed(results);
    });
};

Solution 3

By default, Lambda functions only have 3 seconds to complete. If it takes longer than that, you'll get the error you are seeing.

You can adjust the timeout to anything up to 5 minutes. To change it using the aws CLI, run:

aws lambda update-function-configuration --function-name my-lambda-function --timeout 300
Share:
14,727

Related videos on Youtube

iCodeLikeImDrunk
Author by

iCodeLikeImDrunk

Updated on June 04, 2022

Comments

  • iCodeLikeImDrunk
    iCodeLikeImDrunk almost 2 years

    I wrote some basic js to just list the files of a FTP but I get:

    "Process exited before completing request"

    Is that because Lambda can't interact with FTP?

    I'm using jsftp btw.

    Here's my setup:

    • I use Serverless to create the project
    • For my lambda, I used nodejs and I'm using JSFTP to deal with the ftp stuff.

    My code:

    // Require Serverless ENV vars
    var ServerlessHelpers = require('serverless-helpers-js').loadEnv();
    
    // Require Logic
    var lib = require('../lib');
    
    // Lambda Handler
    module.exports.handler = function (event, context) {
    
        lib.respond(event, function (error, response) {
            return context.done(error, response);
        });
    };
    

    My ftp lambda code:

    var JSFtp = require("jsftp");
    
    module.exports.respond = function (event, cb) {
    
        var ftp = new JSFtp({
            host: "host",
            user: "user",
            password: "password"
        });
    
        ftp.auth(ftp.user, ftp.password, function(err, res) {
            if (err) console.log(err);
            else console.log(res);
    
            ftp.ls(".", function (err, res) {
                var results = [];
                res.forEach(function (file) {
                    results.push(file.name);
                });
    
                ftp.raw.quit();
    
                return cb(null, results.length);
            })
        });
    };
    

    I added some console.log() all over the place and it seems like it choked once it tried to ftp.auth.

    The output I see in cloud watch:

    START RequestId: __ID__ Version: $LATEST
    END RequestId: __ID__
    REPORT RequestId: __ID__    Duration: 526.46 ms Billed Duration: 600 ms     Memory Size: 1024 MB    Max Memory Used: 33 MB  
    Process exited before completing request
    

    So it looks like it just choked somewhere...

  • javamonkey79
    javamonkey79 over 7 years
    This is not true. Maybe it was when you answered this, so I won't downvote on that presumption. We have FTP transfer code in python working in production AWS Lambda.
  • iCodeLikeImDrunk
    iCodeLikeImDrunk over 7 years
    not sure if anything has changed since i worked on it. note that this was before vpc was supported. i tried using java and js, didnt check python though.
  • Zakir Jaafar
    Zakir Jaafar over 7 years
    aws.amazon.com/lambda/faqs says that incoming TCP connections are not allowed. This means FTP PASV won't work at least. Q: What restrictions apply to AWS Lambda function code? ... Inbound network connections are blocked by AWS Lambda, and for outbound connections only TCP/IP sockets are supported, and ptrace (debugging) system calls are restricted. TCP port 25 traffic is also restricted as an anti-spam measure.