I have upload a zip file to S3 bucket.I need to trigger my below lambda
function immediately once the zip file has been uploaded.Kindly help me how to proceed
exports.handler = function (event, context) {
MyLambdaFuntion();
}
MyLambdaFuntion()
{
var bucketName = "TestBucket1";
var fileKey = "test.js";
s3.getObject(params, function (err, data) {
if (err)
console.log(err, err.stack);
else {
console.log(data);
}
});
}
There are some steps you need to follow correctly to do so.
step 1: First create your lambda function, select the
runtime
and selectblank function
or any blue print from the list.step 2: Select the blank square and choose S3 from the list of services.
step 3: Select the bucket you want to trigger from and choose the event type. In your case it should be
Object Created (All)
step 4: Enter prefix, incase if you have any folders inside the S3 and want to triggered only uploading to that folder.
step 5: Enter suffix, to triggered only for the specific suffix '.jpg'
step 6: Tick the enable trigger checkbox and choose Next.
step 7: Now give the fucntion a Name and description. If you want to upload the code or type in the editor there itself, change code entry type.
step 8: In Handler function choose
index.handler
this is the function name it will call once the file is uploaded. Index is file name and handler is function name.step 9: Choose
create a custom role
and it directs to a new page there leave all the fields as it is, don't change anything and chooseAllow
.step 10: Now come back to old tab, Select the role -->
choose from existing role
and select the newly createdrole name
step 11: Select Next, review all the selected options and click
Create Function
.Once created the function successfully, then go to trigger tab and you can see the S3 bucket configured for triggering.
Now start writing the code in the code editor or upload it from local to the lambda function in code tab.
Simple S3 code to read a file is below.
Hope this helps!!!
The best option I see is to have a lambda function ready to run automatically every time a file is placed in bucket S3. When the lambda function is called, an event with information from the created file will be sent to the lambda function.
Here is an example of how to trigger:
next:
Here's an example code lambda nodejs to do this:
I hope it helped you!
You can use the S3 events notification.
Then use the bucket PUT event to trigger a lambda function.