Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS Lambda: Execute function B 10 minutes after function A

Im developing a serverless backend for a time sensitive application.

Is it possible to execute Lambda function B XX minutes after Lambda function A?

Im looking for a clean serverless solution. Using setTimeout or a similar approach is too much of a hack to be accepted.

Example: Send a notification using SNS 10 minutes after a Lambda function has executed.

like image 739
Vingtoft Avatar asked Jan 02 '23 04:01

Vingtoft


1 Answers

The easiest way is to use SQS with a delayed delivery.

  1. Lambda-A adds a message to an SQS queue and sets the delivery delay of 10 minutes. You can set any delay between 0 and 15 minutes. You can also use SQS Delay Queues to delay all messages added to the queue. https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/sqs-delay-queues.html

  2. After 10 minutes SQS message becomes visible and SQS triggers Lambda-B.

  3. Lambda-B receives Amazon SQS Event and deletes message from the queue using receipt handle. Here is an example from https://docs.aws.amazon.com/lambda/latest/dg/eventsources.html#eventsources-sqs:

.

 "Records": [
    {
        "messageId": "c80e8021-a70a-42c7-a470-796e1186f753",
        "receiptHandle": "AQEBJQ+/u6NsnT5t8Q/VbVxgdUl4TMKZ5FqhksRdIQvLBhwNvADoBxYSOVeCBXdnS9P+erlTtwEALHsnBXynkfPLH3BOUqmgzP25U8kl8eHzq6RAlzrSOfTO8ox9dcp6GLmW33YjO3zkq5VRYyQlJgLCiAZUpY2D4UQcE5D1Vm8RoKfbE+xtVaOctYeINjaQJ1u3mWx9T7tork3uAlOe1uyFjCWU5aPX/1OHhWCGi2EPPZj6vchNqDOJC/Y2k1gkivqCjz1CZl6FlZ7UVPOx3AMoszPuOYZ+Nuqpx2uCE2MHTtMHD8PVjlsWirt56oUr6JPp9aRGo6bitPIOmi4dX0FmuMKD6u/JnuZCp+AXtJVTmSHS8IXt/twsKU7A+fiMK01NtD5msNgVPoe9JbFtlGwvTQ==",
        "body": "{\"foo\":\"bar\"}",
        "attributes": {
            "ApproximateReceiveCount": "3",
            "SentTimestamp": "1529104986221",
            "SenderId": "594035263019",
            "ApproximateFirstReceiveTimestamp": "1529104986230"
        },
        "messageAttributes": {},
        "md5OfBody": "9bb58f26192e4ba00f01e2e7b136bbd8",
        "eventSource": "aws:sqs",
        "eventSourceARN": "arn:aws:sqs:us-west-2:594035263019:NOTFIFOQUEUE",
        "awsRegion": "us-west-2"
    }
]
like image 161
Sergey Kovalev Avatar answered Jan 13 '23 11:01

Sergey Kovalev