According to the documentation and experience of many people, lambda is executed at least once: http://docs.aws.amazon.com/lambda/latest/dg/API_Invoke.html
This is quite an issue if you have endpoints mutating the data. Is there anything in the serverless framework to prevent that? Making every endpoint idempotent on the application level would be quite a big hassle.
Did anybody ever face issue related to having lambda triggered multiple times in response to a single api call?
Nope, it’s up to your code to handle the behaviour as appropriate.
Another scenario where your Lambda function may be called more than once is retries; If your function does not call the success method (without an error object, in the case of NodeJs) then Lambda will re-invoke the function 3 times (by default, and it is configurable).
It is tricky to handle it in the application, given two calls will happen in different time. Is there anything in the request available to the app that can be used to identify that given request got already processed? Otherwise clients would need to pass some sort of message id, which is not very convenient.
I know it can happen as retry, but receiving the request again after successfully returning a response is a different thing.
There’s nothing built-in that I’m aware of. It really doesn’t happen often (I have never personally seen it), but imagine they’re just covering themselves for in the even of failure scenarios where the system might re-invoke just to be sure that the event was received at least once.
Like Rowan I’ve never seen a Lambda executed twice for a single event except where it is well documented this will happen (i.e. SNS retrying failures).
With mutations you’ll find a lot of the time they are idempotent, can easily be made idempotent or it doesn’t really matter. For example: Instead of returning an error if someone tries to delete something that’s already been deleted try returning a success.
Mutations that create resources are a little trickier. If you absolutely 100% cannot have a duplicate (i.e. you’re processing financial transactions) then you should design your API appropriately. If you don’t care that much but you’re still a little concerned then you can probably use the request ID from the API gateway to de-duplicate.
In my case, anything I can make idempotent easily is made idempotent and everything else I just ignore.
so you are saying that request from the API gateway is available inside the my code? is there anything I need to do for that or serverless framework gives it to me for free?
The event your Lambda receives depends on how it was trigger. AWS publishes a list of sample events. If it was trigger by the API gateway then yes, otherwise no.
You should think of the Serverless Framework as a deployment tool rather than a framework like Ruby on Rails. Once the deployment is complete you’re living 100% in the platform you deployed to (AWS, Azure, etc).
I have seen this for a function triggered off a cloud watch scheduled event.
I have a function that I want to run once every 10 minutes, however every now and again 2 invocations will happen at the same time rather than just one.
I have also seen similar behaviour when a function is triggered off an SNS message.
There appear to be other people experiencing this here: https://forums.aws.amazon.com/thread.jspa?threadID=241313