Lambda function to download sftp file
The centerpiece is a Node. It uses the ftp client module for communicating with FTP server. Every time CloudWatch Event triggers Lambda, it will execute this method:. It iterates through the content of the given folder and moves each file to the S3 bucket.
As soon as the file is successfully moved, it removes the file from its original location. Notice event. They are coming from the CloudWatch Event Rule definition, which will be described in a following section. Every Rule consists of Cron expression and Input Constant.
Input Constant is exactly the mechanism we can use to pass source and destination. This is exactly the value of the Input Constant and it is shown in the logged output as:. Parameter Store is a nice way for storing configuration and secret data. Value is stored as JSON:. It will execute on schedule. Another thing is how much data it can handle.
AWS Lambda has its limitations. Since this solution is built to scan entire folder and transfer all files from it, if there are too many files, or files are very large, it can happen that Lambda hits one of its limits. It works well when there were dozen of files and each file was never larger than a few KBs.
If there are network issues during transfer, Lambda will break, but since Amazon CloudWatch Events invokes Lambda functions asynchronously, it will retry execution. But I encourage you to explore its limits on your own, and let me know in the comments section if you see how to build more resilience for failures. Failed to load latest commit information. View code. SQS queues is up to you. Sample configurations are provided TODO.
Serverless is recommended. Each push "flow" will need it's own SQS queue. This will be slower when writing multiple objects to a bucket but has the following advantages: It prevents the SFTP server from being over-loaded with multiple simultaneous connects It prevents some unusual edge cases occuring, for example if you put an S3 object then immediately overwrite it, 2 lambdas would be triggered for the same object, potentially racing each other to put different versions of the same object.
Which will win? It's better to not leave this to chance. Getting started with python on Windows. Taking the Online Strategy to Life. Creating an Online Strategy. What to Upload to SlideShare.
Related Books Free with a 30 day trial from Scribd. Related Audiobooks Free with a 30 day trial from Scribd. Elizabeth Howell. Seong-Jung Kim. Views Total views. Actions Shares. No notes for slide. AWS Lambda Scheduled file transfer sftp to s3 python 2. Steps 1. Create a new Administrator user in the IAM 2. Create a Role and allow Lambda execution and permissions for S3 operations 3.
Take note of the User ARN 4. Deploy bit Amazon Linux EC2 instance 5. Create requirements. Write the Handler 8. Zip up code and Dependencies 9.
Create Lambda Function Manually invoke the Lambda Function