#AWS Lambda FTP Function
The following How-To will demonstrate how to use AWS Lambda service to:
- Connect to FTP
- Download files
- Archive files in FTP (moving them to
archive
folder) - Save files to S3
Later this will trigger another event on Lambda to parse these files and send data to API Gateway. Make sure that S3 and Lambda are on the same region, otherwise Lambda event will not work.
Flow diagram:
This how-to assume that you have an AWS account with admin access.
Create S3 bucket.
Create Role to access this S3 bucket
- Click on Identity & Access Management (IAM)
- Click on Roles
- Create New Role
- Name it
lambda_ftp_to_s3
- Select AWS Lambda.
- Select
AWSLambdaFullAccess
and click Next. - This will then show you a review screen like below.
Create Lambda function
-
Click on Get started now.
-
Skip the next screen as it just showing pre-defined functions
-
In the code section, copy and paste the code in aws_lambda_ftp_function.py
-
**Please make sure to populate data with your own specific credentials for S3 bucket name and FTP connection.
-
Handler, keep it as it is
-
Role, use the one that we have created on step 2.
-
Timeout is quite improtant in this application as you may expect delays, slow FTP connection or large number of files to download. I would put it as 1 minute.
-
Click create function
Now we need to setup the event to call this function to fetch FTP and download files.
- In Lambda function page, go to
Event sources
- Add event source
- Choose
CloudWatch Events - Schedule
- Use Rule name as you prefer and choose
Schedule expression
- You can either use pre-defined rates (1 minute, 5 minutes, 15 minutes, 1 hour or cron type schedule)
To test your function, run Test from function page and you should be able to see output at buttom.
If everything was ok, you should be able to see files in your S3.
Add code to archive files in FTP- Add code to read files from S3, parse them and send them to API gateway