Schedule our Lambda function

cover image
Published: June 9, 2020

Twitter bot with Kotlin in AWS (7 part series)

  1. Twitter Bot - Vue 3 Updates
  2. Creating an AWS Lambda Kotlin function
  3. Schedule our Lambda function

  4. DynamoDB for our Kotlin Lambda function
  5. Posting a tweet from Kotlin
  6. Secrets in AWS and reading from Kotlin
  7. CI/CD to AWS with GitHub Actions

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 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.

bash
aws events put-rule --schedule-expression "rate(5 minutes)" --name FiveMinRule

Copy the RuleArn from the output.

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

bash
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

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.

bash
aws events put-targets --rule FiveMinRule --targets "Id"="1","Arn"="<function-arn>"

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

Twitter bot with Kotlin in AWS (7 part series)

  1. Twitter Bot - Vue 3 Updates
  2. Creating an AWS Lambda Kotlin function
  3. Schedule our Lambda function

  4. DynamoDB for our Kotlin Lambda function
  5. Posting a tweet from Kotlin
  6. Secrets in AWS and reading from Kotlin
  7. CI/CD to AWS with GitHub Actions