How to copy a file from S3 to EC2 instance using AWS Lambda function? How to copy a file from S3 to EC2 instance using AWS Lambda function? jenkins jenkins

How to copy a file from S3 to EC2 instance using AWS Lambda function?


So if your are newbie on Lambda you have to know first that you can put your own code directly in the Lambda function command line or you can upload a .zip file that contains your function. The second one is the one we will use to achieve the copy from s3 to your EC2.

¿Why we will upload a .zip file with the function?Because in this way we can install all the dependencies that we need and want.

Now, to makes this possible, first of all, your lambda function needs to connect into your EC2 instance through SSH. After that you could execute some command lines in order to download the S3 file that you want.

So put this code into your lambda function (inside the exports.handler....) and install the simple-ssh dependency with "npm install simple-ssh"

// imagine that the input variable is the JSON sended from the client.//input = {    //s3_file_path : 'folder/folder1/file_name',    //bucket       : 'your-bucket',//};// Use this library to connect easly with your EC2 instance.var SSH = require('simple-ssh');var fs  = require('fs');// This is the full S3 URL object that you need to download the file.var s3_file_url    = 'https://' + input.bucket + '.s3.amazonaws.com/' + input.s3_file_path;/**************************************************//*                      SSH                       *//**************************************************/var ssh = new SSH({    host: 'YOUR-EC2-PUBLIC-IP',    user: 'USERNAME',    passphrase: 'YOUR PASSPHRASE', // If you have one    key : fs.readFileSync("../credentials/credential.pem") // The credential that you need to connect to your EC2 instance through SSH});// wget will download the file from the URL we passedssh.exec('wget ' + s3_file_url).start();// Also, if you wanna download the file to another folder, just do another exec behind to enter to the folder you want.ssh.exec('cd /folder/folder1/folder2').exec('wget ' + s3_file_url).start();

For this to work, you should make sure that your EC2 machine has the permissions enabled so that it can be entered via SSH.


The short answer is no. S3 itself can't copy anything anywhere.

The right way to think about this is S3 can send a notification that can launch a Lambda function. Your Lambda function could then do something with the instance. It seems rather complicated.

I'd skip using Lambda and wrote a script that would subscribe to the S3 bucket notifications SNS Topic directly from your instances. This script would download the file directly to your instance upon it's upload to S3. This solution is also scalable, you can have many instances subscribed to this topic etc.