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 passed
ssh.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.