can one aws lambda function handle multiple triggers at once? - amazon-web-services

I have an aws lambda function. When it receives only one trigger, it always succeds. But when it receives more than one trigger, it sometimes throws error. The first trigger always succeds.
Can I configure one aws lambda function receives only one trigger?

can one aws lambda function handle multiple triggers at once?
Yes, Lambda functions can handle multiple triggers at once.
when it receives more than one trigger, it sometimes throws error
This is most probably related to your implementation. Are you doing something different based on the inputs? Is the code behaving differently based on time?
Can I configure one aws lambda function receives only one trigger?
You can limit the concurrency of the Lambda function. If you set it to 1, you can only have one Lambda function running at any given time.
See: Set Concurrency Limits on Individual AWS Lambda Functions

Related

Is it possible to trigger two AWS lambda function via a single SQS Message

As per my existing solution I have two lambda function which gets triggered by the different SQS message and create a folder structure in S3.
Now, I have the requirement where I need to use the single SQS message to trigger both the lambda function.
Is it possible to trigger multiple lambda function via a single SQS message if yes then can you please explain the process and how efficient it would be?
If is there any other approach I can follow please let me know.
Thanks!
No, you can't do that. The best way is to create fan out setup with SNS + two SQS queues.
Otherwise, you have to develop other solution, e.g. one lambda gets triggered by sqs, and then invokes the second one passing the message as input.

AWS Lambda Functions: Will Different Triggers Reuse an Exection Enviornment?

Here's what I know, or think I know.
In AWS Lambda, the first time you call a function is commonly called a "cold start" -- this is akin to starting up your program for the first time.
If you make a second function invocation relatively quickly after your first, this cold start won't happen again. This is colloquially known as a "warm start"
If a function is idle for long enough, the execution environment goes away, and the next request will need to cold start again.
It's also possible to have a single AWS Lambda function with multiple triggers. Here's an example of a single function that's handling both API Gateway requests and SQS messages.
My question: Will AWS Lambda reuse (warm start) an execution environment when different event triggers come in? Or will each event trigger have it's own cold start? Or is this behavior that's not guaranteed by Lambda?
Yes, different triggers will use the same containers since the execution environment is the same for different triggers, the only difference is the event that is passed to your Lambda.
You can verify this by executing your Lambda with two types of triggers (i.e. API Gateway and simply the Test function on the Lambda Console) and looking at the CloudWatch logs. Each Lambda container creates its own Log Stream inside of your Lambda's Log Group. You should see both event logs going to the same Log Stream which means the 2nd event is successfully using the warm container created by the first event.

FInd out who invoked AWS lambda function

My AWS lambda function is getting invoked my multiple places like api-gateway, aws-SNS and cloud-watch event. Is there any way to figure out who invoked the lambda function? My lambda function's logic depends on the invoker.
Another way to achieve this is having three different lambda functions but I don't want to go that way if I can find invoker information in a single Lambda function itself.
I would look at the event object as all of the three services will have event of different structure.
For example, for CloudWatch Events I would check if there is a source field in the event. For SNS I would check for Records and API gateway for httpMethod.
But you can check for any other attribute that is unique to a given service. If you are not sure, just print out to logs example events from your function for the three services and check what would be the most suited attribute to look for.

Lambda function concurrent invocation per event trigger

I have a lambda function that is is logging into a server on a specific interval, defined from a CloudWatch event rule. There are multiple servers that need to be logged into on different intervals, each defined by their own respective CloudWatch event rule. However, I only want one lambda function invocation hitting a specific server at a time. Can each CloudWatch event rule be limited to just one lambda function invocation at a time, or would I have to create a duplicate lambda function for each specific CloudWatch event rule and set the concurrent invocation to 1 that way? I was hoping to avoid that as it just adds duplicate lambda functions. I'd just want to keep it simple, if possible.
If you know the IDs of these instances, you can pass them as arguments to your CloudWatch Event rules in the form of constant:
Your single function would get the ID of the instance from the event object and perform operations on that one specific instance.

Using step functions to trigger a lambda once another has finished executing

I have two lambdas. Lambda A is triggered once an SQS is populated with messages. I want Lambda B to execute once Lambda A is done executing. How can I do this? Lambda A will have multiple invocations running at the same time, does that make a difference?
Steps functions are created for this purpose. You can transfer data from one lambda to another, so you will have the context of what is being worked on by the previous lambda will be sent to the next lambda.
To do this, you'd need to trigger the Step Function directly from a lambda. As far as I know, you can't trigger Step Functions directly from SQS.
So, either
If Lambda A is triggered by something else than SQS, trigger your Step Function instead. It will then run Lambda A and Lambda B depending on how you set it up.
If Lambda is triggered by SQS, you make a third lambda (Lambda 0), triggered by SQS, whose sole purpose is to in turn trigger your Step Function (which will run Lambda A then Lambda B) or you directly trigger Lambda B from Lambda A (in which case, Step Functions are pointless and you should rather go with SQS / SNS).
A note on this; one thing Step Functions cannot do out of the box is, for example execute Lambda B only once all Lambda A invocations are done. It will act on a per execution basis.