Our Products:   CompleteFTP  edtFTPnet/Free  edtFTPnet/PRO  edtFTPj/Free  edtFTPj/PRO
0 votes
176 views
in .NET FTP by (51.6k points)
As user asked us how to use edtFTPnet/PRO inside an AWS Lambda function.

1 Answer

0 votes
by (51.6k points)

Yes, you can. The basic strategy is:

  1. create a VS project
  2. reference the required NuGet packages
  3. add a runtime config file
  4. write the code that uses SecureFTPConnection, writing logging using a custom log appender
  5. build the project
  6. publish the project
  7. zip up the published folder
  8. add the AWS Lambda function in the AWS Console
  9. upload the zip file created in step 7
  10. test the function
  11. view the logs on CloudWatch
Here are more detailed instructions:

Preparing Your AWS Lambda Deployment Package

  1. Create a Lambda-Compatible Project: Start by creating a .NET Core AWS Lambda project in Visual Studio. Target .NET 6.0 as your framework to ensure compatibility with AWS Lambda's runtime.

  2. Add Required NuGet Packages: Incorporate necessary packages such as Amazon.Lambda.Core, Amazon.Lambda.Serialization.SystemTextJson, and System.Text.Encoding.CodePages. The latter is crucial for handling character encoding, especially relevant when dealing with FTP data.

  3. Include edtFTPnet/PRO: Add a reference to edtFTPnetPROSTD.dll in your project. This specific DLL is built for .NET Standard 2.0 and is well-suited for AWS Lambda. Ensure that the DLL is set to copy to the output directory.

  4. Runtime Configuration: Include the .runtimeconfig.json file in your deployment package to ensure the .NET runtime correctly configures the Lambda environment. The Copy to Output Directory property should be set to Copy always.

  5. Write Code: Some sample code is included below. The best way to see what's going on is to write logs of logging, which can be sent to AWS CloudWatch using a custom log appender.
  6. Build the project: Since you can't use the Visual Studio debugger you may as well just do a release build.
  7. Publish Your Project: Instead of a simple build, use Visual Studio's Publish feature:

    • Right-click the project in Solution Explorer.
    • Choose Publish.
    • Follow the prompts to publish the project to a local folder.
    • Once published, manually zip the contents of this folder to create a deployment package. I just zipped the files, rather than the folder.

Deploying to AWS Lambda

  1. Create a Lambda Function: In the AWS Management Console, navigate to AWS Lambda and create a new function. Select the appropriate runtime (.NET 6.0 or later), and specify the handler based on your project settings.

  2. Upload Your Zip File: Upload the zip file that you created from the published folder. This package should contain all necessary executables, libraries, and configuration files.

Testing and Debugging

  1. Test Your Function: Configure test events in the AWS Lambda console to simulate different inputs to your function. This helps in testing the integration with FTP servers.

  2. View Logs in CloudWatch: Logs generated by your function are automatically sent to CloudWatch. Review these logs for insights into your function's operation and to troubleshoot any issues.

Code

using Amazon.Lambda.Core;

using EnterpriseDT.Net.Ftp;
using EnterpriseDT.Util.Debug;

namespace NetProOnAWSLambda
{
    public class Function
    {
        // The function handler
        public void testSecureFTPConnection(ILambdaContext context)
        {
            // Logging setup
            Logger.AddAppender(new LambdaAppender());
            Logger.CurrentLevel = Level.DEBUG;

            var log = Logger.GetLogger("NetProOnAWSLambda.Function");

            // FTP connection details
            const string host = "test.rebex.net";  // Change to your FTP server's hostname
            const string username = "demo";        // Change to your FTP username
            const string password = "password";    // Change to your FTP password

            log.Info($"host = {host}, username = {username}");

            try
            {
                // Instantiate and configure the FTP connection
                using (SecureFTPConnection ftp = new SecureFTPConnection())
                {
                    ftp.LicenseOwner = "TrialUser";
                    ftp.LicenseKey = "123-4567-8901-2345";  // execute 'reg query "HKEY_CURRENT_USER\Software\Enterprise Distributed Technologies\edtFTPnet PRO" /v LicenseKey' to get the license key.

                    ftp.ServerAddress = host;
                    ftp.Protocol = FileTransferProtocol.SFTP;
                    ftp.UserName = username;
                    ftp.Password = password;

                    // Select SFTP protocol
                    ftp.Protocol = FileTransferProtocol.SFTP;

                    // Optional: Disable server certificate validation (for testing only)
                    ftp.ServerValidation = SecureFTPServerValidationType.None;

                    // Connect to the FTP server
                    log.Info($"Connecting to {host}...");
                    ftp.Connect();

                    log.Info("Connection successful." + Environment.NewLine);

                    // Perform operations, e.g., list files
                    log.Info($"Fetching listing of {ftp.ServerDirectory}");
                    string[] files = ftp.GetFiles();
                    log.Info("Files at server:" + Environment.NewLine + string.Join(Environment.NewLine, files));
                }
            }
            catch (Exception ex)
            {
                log.Error(ex);
            }
            finally
            {
                Logger.ClearAppenders();
            }
        }
    }

    public class LambdaAppender : Appender
    {
        public void Log(string msg)
        {
            LambdaLogger.Log(msg + Environment.NewLine);
        }

        public void Log(Exception t)
        {
            LambdaLogger.Log($"Exception: {t.ToString()}" + Environment.NewLine);
        }

        public void Close()
        {
        }
    }
}

Categories

...