5

I write an AWS lambda function in Node.js for image resizing and trigger it when images upload. I have already more than 1,000,000 images existing in bucket.

I want to run this lambda function on that images but not find anything till yet.

How can I run AWS lamdba function on existing images of S3 bucket?

Note:- I know this question already asked on Stack overflow, but issue is that no solution of them given till yet

Haseeb Ahmad
  • 7,914
  • 12
  • 55
  • 133

4 Answers4

4

Unfortunately, Lambda cannot be triggered automatically for objects that are already existing in a S3 bucket.

You will have to invoke your Lambda function manually for each image in your S3 bucket.

First, you will need to list existing objects in your S3 bucket using the ListObjectsV2 action.

For each object in your S3 bucket, you must then invoke your Lambda function and provide the S3 object's information as the Payload.

spg
  • 9,309
  • 4
  • 36
  • 41
4

Yes , it's completely true that lambda cannot be triggered by objects already present there in your s3 bucket, but invoking your lambda manually for each object is a completely dumb idea.

With some clever techniques you can perform your tasks on those images easily :

  1. The hard way is, make a program locally that exactly does the same thing as your lambda function but add two more things, firstly you have to iterate over each object in your bucket, then perform your code on it and then save it to destination path of s3 after resizing. i.e, for all images already stored in your s3 bucket , instead of using lambda, you are resizing the images locally in your computer and saving them back to s3 destination.

  2. The easiest way is, first make sure that you have configured s3 notification's event type to be Object Created (All) as trigger for your lambda.

enter image description here

Then after this, move all your already stored images to a new temporary bucket, and then move those images back to the original bucket, this is how your lambda will get triggered for each image automatically. You can do the moving task easily by using sdk's provided by AWS. For example, for moving files using boto3 in python, you can refer this link to moving example in python using boto3

  1. Instead of using moving , i.e cut and paste , you can use copy and paste commands too.
Community
  • 1
  • 1
Mausam Sharma
  • 852
  • 5
  • 10
2

In addition to Mausam Sharma's comment you can run the copy between buckets using the aws cli:

aws s3 sync s3://SOURCE-BUCKET-NAME s3://DESTINATION-BUCKET-NAME --source-region SOURCE-REGION-NAME --region DESTINATION-REGION-NAME

from here: https://medium.com/tensult/copy-s3-bucket-objects-across-aws-accounts-e46c15c4b9e1

Julia Cowper
  • 206
  • 3
  • 3
2

You can simply copy back to the same bucket with the CLI which will replace the original file with itself and then run the lambda as a result.

aws s3 copy s3://SOURCE-BUCKET-NAME s3://SOURCE-BUCKET-NAME --recursive

You can also include/exclude glob patterns which can be used to selectively run against say a particular day, or specific extensions etc.

aws s3 copy s3://SOURCE-BUCKET-NAME s3://SOURCE-BUCKET-NAME --recursive --exclude "*" --include "2020-01-15*"

It's worth noting that like many of the other answers here, this will incur costs on s3 for read/write etc, so cautiously apply this in the event of buckets containing lots of files.

Ben Swinburne
  • 25,669
  • 10
  • 69
  • 108