Search code examples
fileuploadslack

Lambda slack file upload is getting triggered multiple times


My app architecture is slack events -> API Gateway -> Lambda -> does someoperation and returns an .png file which is generated using numpy and matplotlib.

When i deal with just text output in lambda, it works fine, but when i deal with file uploads, it works strange,

It uploads files to slack using[files.upload] method and then after a minute again my lambda gets triggered and ends up in uploading another file.

Is it because slack return HTTP response for file.upload method and somehow my app catches that and it runs agian?

It would be of great help as even in the slack events, events are same without any difference but i am really not sure why my lambda gets invoked again and i verified the request ID's and it is different and even at API getway there are two different request ID's but i have requested only one time...it drives me crazy...


Solution

  • I found out the way to do this. With the help of this article https://aws.amazon.com/premiumsupport/knowledge-center/custom-headers-api-gateway-lambda/ i added HTTP Header [client Header information] in API Gateway and i pass it to lambda. So, in Lambda i catch the retry events from slack with the help of header which contains X-Slack-Retry-Num for retry events and return it immediately as return 200.

    if 'X-Slack-Retry-Num' in output['headers']:
        slk_retry = output['headers']['X-Slack-Retry-Num']
        return 200
    else:
        "Consider this as first event and provide your actual code and logic"