How to wait to trigger Lambda function until specific files are uploaded - amazon-web-services

I'm new to AWS and working on Lambda function triggered by S3.
I have two json files and need to use the both data to send queue to SQS.
For example, two files, "file_a.json" and "file_b.json" are to be uploaded to S3 at the same time.
file_a.json
{"data": "123"}
file_b.json
{"data": "456"}
Then, Lambda will send queue message like ["123", "456"] which is created with the data from both files.
However, S3 invokes Lambda function every time the single file is uploaded and this makes it hard to send queue to SQS as I wrote above. I'm thinking if S3 can wait until the two files are uploaded to trigger Lambda, it can solve the issue.
If anyone had faced the similar situation, I would like to know how to resolve this problem. Thanks!

S3 notifications are simple events in response to changes in S3, so you cant "make s3 wait" for both files, its upto you to handle that in your lambda.
You could store the message (or whatever data you need) - s3, rds, paramstore etc, or if you can deduce the other filename from the message you can query S3 and check if the other file exists yet and act accordingly.

Related

Lambda invocation on two SNS events at the sametime

I have a usecase where I need to read the two files which are in a different account, I will be receiving an SNS event with the filename and I need to create an EMR cluster from the Lambda only if two files are available in the other s3 bucket.
Currently I am writing dummy files to s3 bucket every time I receive a SNS event and then creating the EMR cluster only after ensuring that on the second SNS event that I have received, the first file is available in my accounts s3 bucket- This approach is working fine.
But I am unable to solve the issue of what really happens if we receive two files at the same time in the other s3 bucket and if we get two sns events around the same time, as each event thinks the other file hasn’t been arrived yet.
How would I solve this problem .

S3 trigger sending bulk upload notification to AWS SNS

I want to execute two lambda function when a new object uploaded on s3.
I am using s3 trigger at bucket level and sending notifications to AWS SNS, 2 lambda functions have subscribe to these SNS to get notifications.
I tried uploading 100 files and I could see all file name got printed in log in both function.
But in one function I could see 2 logs where as in another I could see 3 logs when combined I can see name of all 100 files.
How SNS works if it gets too many object upload notification ?
How Lambda function works, for e.g let's say its still processing the first file and it gets notification to process second file and third file
Is there a chance of few files getting skipped by Lambda because it received too many notification

Is there a way to add delay to trigger a lambda from S3 upload?

I have a Lambda function which is triggered after put/post event of S3 bucket. This works fine if there is only one file uploaded to S3 bucket.
However, at times there could be multiple files uploaded which can take upto 7 minutes to complete the upload process. This triggers my lambda function multiple times which adds overhead of handling this from the code.
Is there any way to either trigger the lambda only once for the complete upload or add delay in the function and avoid multiple execution of Lambda function?
There is no specific interval when the files will be uploaded to S3 hence could not use scheduler.
Delay feature was added for Lambda that has Kinesis or DynamoDB Event Sources recently. But it's not supported for S3 events.
You can send events from S3 to SQS. Then your Lambda will consume SQS events. It consumes them in batch by default.
It seems Multi Part Upload is being used here from the client.
Maybe a duplicate of this? - AWS Lambda and Multipart Upload to/from S3
An alternative might be to have your Lambda function check for existence of all required files before moving on to the action you need to take. The Lambda function would still fire each time, but would exit quickly if not all files have been received yet.

Amazon S3 Event Notification not triggering sometimes

We have an Amazon S3 Bucket with Event notification setup for POST and Multipart upload completed and initially we had it set to trigger a Lambda directly but due to error handling concerns we change it to SQS to get the "backout" feature of SQS to easier capture any message in error.
The files are put to S3 from a SFTP server (EC2 instance) and the events are put to SQS in like 99.9% of the cases but ever so often a file is missed...
We can easily spot this as the SQS will in turn trigger a Lambda and the first thing the Lambda does is to rename the file to ".processing" and as soon as the processing s completed the file is moved to another Bucket.
Now and again we find files with the original file name which has not gotten the ".processing" extension and there are no SQS messages or logs that shows that the Lambda has picked them up. This happens like once in a thousand files or something like that...
Files are always transferred the same way to the Bucket but sometimes there are large batches and it seems to happen more frequently in large batches...
What could be the reason some files are not triggering a notification?
Or what can I check to find what could possibly cause this?

Missing s3 events in AWS SQS

I have an AWS Lambda function that is supposed to be triggered by messages from Simple Queue Service SQS. This SQS is supposed to get a notification when new json file is written into my s3 bucket, or when existing json file in s3 bucket is overwritten. Event type for both cases is s3:ObjectCreated, and I see notification for both cases is my SQS.
Now, the problem is that pretty frequently there is a new file in s3 (or updated existing file in s3), but there is no corresponding message in sqs! So many files are missing and Lambda is not aware that those should be processed. In Lambda I print the whole content of received SQS payload into the log file, and then try to find those missed files with something like aws --profile aaa logs filter-log-events --log-group-name /aws/lambda/name --start-time 1554357600000 --end-time 1554396561982 --filter-pattern "missing_file_name_pattern" but can't find anything, which means that s3:objectCreated event was not generated for this missing file. Are there some conditions that prevents s3:objectCreated events for new/updated s3 files? Is there a way to fix it? Or workaround of some kind, may be?
According to AWS Documentation:
If two writes are made to a single non-versioned object at the same time, it is possible that only a single event notification will be sent. If you want to ensure that an event notification is sent for every successful write, you can enable versioning on your bucket. With versioning, every successful write will create a new version of your object and will also send an event notification.
https://docs.aws.amazon.com/AmazonS3/latest/dev/NotificationHowTo.html
Also, why not directly trigger lambda from S3?
Two possibilities:
Some events may be delayed or not sent at all: "Amazon S3 event notifications typically deliver events in seconds but can sometimes take a minute or longer. On very rare occasions, events might be lost.", although it is very rare.
You have some mistake and the lambda is either not printing what you expect when processing this message / you don't search correctly for the log.
You should also make sure on SQS that all the records were ingested and processed successfully.
Make sure that you have all of the create object events checked off as a trigger.
I had an issue where files > 8MB were being uploaded as multi-part uploads which are listed as another trigger separately to the PUT trigger.