Ethan Harlig
Ethan Harlig

Reputation: 766

Invoke AWS Lambda function only once, at a single specified future time

I want to be able to set a time to invoke an AWS Lambda function, then have that function be invoked then and only then. For example, I want my Lambda function to run at 9:00pm on December 19th, 2017. I don't want it to repeat, I don't want it to invoke now, just at 9:00pm on the 19th.

I understand that CloudWatch provides Scheduled Events, and I was thinking that when a time to schedule this reminder for is inputted, a CloudWatch Scheduled Events is created to fire in that amount of time from now (so like if you schedule it at 8:22pm to run at 9pm, it’ll be 38 mins), then it invokes the Lambda function at 9pm which then deletes the CloudWatch Scheduled Event. My issue with this is that when a CloudWatch Scheduled Event is created, it executes right then, then at the specified interval.

Any other ideas would be appreciated, as I can't think of another solution. Thanks in advance!

Upvotes: 34

Views: 26323

Answers (5)

david_adler
david_adler

Reputation: 10912

You can schedule a step function which can wait until a specific point in time before invoking the lambda with an arbitrary payload.

https://docs.aws.amazon.com/step-functions/latest/dg/amazon-states-language-wait-state.html

Something like this

const stepFunctions = new AWS.StepFunctions()
const payload = {
    stateMachineArn: process.env.SCHEDULED_LAMBDA_SF_ARN,
    name: `${base64.encode(email)}-${base64.encode(timestamp)}`, // Dedupe key
    input: JSON.stringify({
      timestamp,
      lambdaName: 'myLambdaName',
      lambdaPayload: {
        email,
        initiatedBy
      },
    }),
  }
await stepFunctions.startExecution(payload).promise()

Upvotes: 13

Haziq
Haziq

Reputation: 2288

I understand its quite late to answer this question. But anyone who wants to use CRON expression to trigger an event(or call an API) only once can use following example:

This event will be triggered only once on January 1, 2025 - 12:00:00 GMT

00 12 01 01 ? 2025

For those who do not have much knowledge of cron syntax:

Minutes Hours DayOfMonth Month DayOfWeek Year

I am using this with AWS Cloudwatch Events and the result looks like this: enter image description here

Note: I did not have to specify Day of week, since I have given it a fixed date and thats obvious.

Upvotes: 5

Muhammad Soliman
Muhammad Soliman

Reputation: 23756

You can use DynamoDB TTL feature to implement this easily, simply do the following:

1- Put item with TTL, the exact time you want to execute or invoke a lambda function.

2- Configure DynamoDB Streams to trigger a lambda function on item's remove event.

Once the item/record is about to expire, your lambda will be invoked. you don't have to delete or cleanup anything as the item in dynamodb is already gone.

NOTE: However the approach is easy to implement and scales very well, but there's one precaution to mention; using DynamoDB TTL as a scheduling mechanism cannot guarantee exact time precision as there might be a delay. The scheduled tasks are executed couple of minutes behind.

Upvotes: 20

Gaurav Singh
Gaurav Singh

Reputation: 9

Invoking a lambda function via Events is asynchronous invocation option. By using CloudWatchEvent to trigger Lambda function we can use cron job but we are still facing issues as Lambda function triggered multiple times for the same cron schedule.PFB Link: https://cloudonaut.io/your-lambda-function-might-execute-twice-deal-with-it/

But this needs Dynamo DB to be implemented in your account and then make your Lambda function Idempotent.

Upvotes: -1

Kush Vyas
Kush Vyas

Reputation: 6079

You can schedule lambda event using following syntax:

cron(Minutes Hours Day-of-month Month Day-of-week Year)

Note: All fields are required and time zone is UTC only

Please refer this AWS Documentation for Details.

Thanks

Upvotes: 27

Related Questions