DEV Community

Cover image for Schedule our Lambda function
Gaute Meek Olsen
Gaute Meek Olsen

Posted on • Updated on • Originally published at gaute.dev

Schedule our Lambda function

This article is part of the Twitter bot with Kotlin in AWS series showing how I created a Twitter bot for Vue 3 updates. But this article works as an independent article on how to run a Lambda function on a scheduled time period.

Now we want our Lambda function to run every 5 minutes. We will use the AWS CLI to set it up.

First, let's create a scheduled event where you specify when it should trigger based on a cron expression or a rate expression. More information about the syntax here.

aws events put-rule --schedule-expression "rate(5 minutes)" --name FiveMinRule
Enter fullscreen mode Exit fullscreen mode

Copy the RuleArn from the output.

Now we will add permission for the CloudWatch event to trigger the function.

aws lambda add-permission --function-name twitter-bot-vue-3 --action lambda:InvokeFunction --principal events.amazonaws.com --source-arn <rule-arn-from-above> --statement-id my-scheduled-event
Enter fullscreen mode Exit fullscreen mode

At last, we will use put-targets to add the Lambda function to the rule. First we need to run aws lambda list-functions and copy the FunctionArn for the function.

aws events put-targets --rule FiveMinRule --targets "Id"="1","Arn"="<function-arn>"
Enter fullscreen mode Exit fullscreen mode

That's it, you can now see that the Lambda function has a trigger attached to it in the AWS console.

Top comments (0)