0
votes

I am working on a code where Lambda Function 1 (call it, L1) executes on messages from an SQS queue. I want to execute another lambda (call it, L2) exactly a week after L1 completes and want to pass L1's output to L2.

Execution Environment: Java

For my application, we are expecting around 10k requests on L1 per day. And same number of requests for L2.

If it runs for a week, we can have around 70k active executions at peak.

Things that I have tried:

Cloudwatch events with cron: I can schedule a cron with specified time or date which will trigger L2. But I couldn't find way to pass input with scheduled Cloudwatch event.

Cloudwatch events with new rules: At the end of first lambda I can create a new cloudwatch rule with specified time and specified input. But that will create as many rules (for my case, it could be around 10k new cloudwatch rules everyday). Not sure if that is a good practice or even supported.

Step function: There are two types step functions in play today. Standard: Supports wait for a year, but only supports 25k active executions at any time. Won't scale since my application will already have 70k active executions at the end of first week. https://docs.aws.amazon.com/step-functions/latest/dg/limits.html

Express: Doesn't have limit on number of active executions but supports max 5 minutes executions. It will time out after that. https://docs.aws.amazon.com/step-functions/latest/dg/express-limits.html

2
What is the time resolution - i.e. how accurate does the 1 week later have to be? - stdunbar
It should be at least 1 week later. - Vivek Ranjan
Added a new way of handling this to my answer. - stdunbar

2 Answers

2
votes

It would be easy to create a new Cloudwatch Rule with the "week later" Lambda as a target as the last step in the first Lambda. You would set a Rule with a cron that runs 1 time in 1 week. Then, the Target has an input field. In the console it looks like:

enter image description here

You didn't indicate your programming environment but you can do something similar to (psuedo code, based on Java SDK v2):

String lambdaArn = "the one week from today lambda arn";
String ruleArn = client.putRule(PutRuleRequest.builder()
                .scheduleExpression("17 20 23 7 *")
                .name("myRule")).ruleArn();
Target target = TargetBuilder.builder().arn(lambdaArn).input("{\"message\": \"blah\"}").rule("myRule");
client.putTargets(PutTargetsRequest.builder().targets(target));

This will create a Cloudwatch Event Rule that runs one time, 1 week from today with the input as shown.

Major Edit

With your new requirements (at least 1 week later, 10's of thousands of events) I would not use the method I described above as there are just too many things happening. Instead I would have a database of events that will act as a queue. Either a DynamoDB or RDS database will suffice. At the end of each "primary" Lambda run, insert an event with the date and time of the next run. For example, today, July 18 I would insert July 25. The table would be something like (PostgreSQL syntax):

create table event_queue (
    run_time        timestamp not null,
    lambda_input    varchar(8192),
);

create index on event_queue( run_time );

Where the lambda_input column has whatever data you want to pass to the "week later" Lambda. In PostgreSQL you would do something like:

insert into event_queue (run_time, lambda_input)
    values ((current_timestamp + interval '1 week'), '{"value":"hello"}');

Every database has something similar to the date/time functions shown or the code to do this isn't terrible.

Now, in CloudWatch create a rule that runs once an hour (the resolution can be tuned). It will trigger a Lambda that "feeds" an SQS queue. The Lambda will query the database:

select * from event_queue where run_time < current_timestamp

and, for each row, put a message into an SQS queue. The last thing it does is delete these "old" messages using the same where clause.

On the other side you have your "week later" Lambdas that are getting events from the SQS queue. These Lambdas are idle until a set of messages are put into the queue. At that time they fire up and empty the queue, doing whatever the "week later" Lambda is supposed to do.

By running the "feeder" Lambda hourly you basically capture everything that is 1 week plus up to 1 hour old. The less often you run it the more work that your "week later" Lambda's have to do and conversely, running every minute will add load to the database but remove it from the week later Lambda.

This should scale well, assuming that the "feeder" Lambda can keep up. 10k transactions / 24 hours is only 416 transactions and the reading of the DB and creation of the messages should be very quick. Even scaling that by 10 to 100k/day is still only ~4000 rows and messages which, again, should be very doable.

2
votes

Cloudwatch is more for cron jobs. To trigger something at a specific timestamp or after X amount of time I would recommend using Step Functions instead.

You can achieve your use-case by using a State Machine with a Wait State (you can pass tell it how long to wait based on your input) followed by your Lambda Task State. It will be similar to this example.