#Lambda function url with dynamodb running twice

5 messages · Page 1 of 1 (latest)

livid mirage
#

Spoiler warning! I’m going to post my code for the Lambda-DynamoDB section. I decided to use Lambda Function URLs instead of API gateway but I’m running into an error. The issue I am facing is when I envoke my code through the Lambda Function URL link, it will return a response of an increment of 1 which is what I want, but when I refresh the DynamoDB table it will increase the increment by 2. This doesn’t happen when I run the Lambda code through a test on the console, it will return a number and show that same number in the DynamoDB table. I have edited this code all morning, I started with using Get Item and Put Item, but completely rewrote my code to use atomic counters in DynamoDB hoping this would help my issue and while it did simplify my code a bunch I’m still having the same problem. Pictures are attached

junior grove
#

Congrats. I think you have just discovered the lambda requests idempotency problem.. which might be a really fun problem to fix.
First how did you 'invoke your lambda function url link'?

livid mirage
# junior grove Congrats. I think you have just discovered the lambda requests idempotency probl...

Hi! I clicked the function url link directly through the Lambda console. However, I kept on going with the project and figured I’d come back to the problem but when I coded the function URL to my html and JavaScript and got that section finished, when I refresh my website, the function url runs perfectly and syncs perfectly with my DynamoDB. So that leads me to the question, why when I click the function URL link directly through the Lambda console does it run the function twice?

junior grove
#

First time I ran into this, I found it really confusing as well. The problem is how your client handles making the request. For eg I've learnt some clients will try to guarantee an at least once invocation. Chrome browser has been known to make additional requests for (i believe favicon iirc). You'll need to do some network monitoring to be sure.
The solution lies in making your lambda idempotent. Ive written some blog posts about lambda function urls in general. This was a topic that i had planned to do a deep dive on.. sometime.

livid mirage
#

Ah I see exactly what you’re saying! Thank you, that makes sense- tomorrow I’ll go down the lambda idempotentcy rabbit hole and fix up my code/ look more into this and make sure it runs once only and not ‘at least once’. I would love to view your blogs! This is my first time using function URL so any information is super helpful, would you mind providing the link?