Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Schedule AWS-Lambda with Java and CloudWatch Triggers

I am new to AWS and AWS-Lambdas. I have to create a lambda function to run a cron job in every 10 minutes. I am planning to add a Cloudwatch trigger to trigger the same in every 10 minutes but without any event. I looked up on the internet and found that some event needs to be there to get it running.

I need to get some clarity and leads on below 2 points:

  • Can I schedule a job using AWS-Lambda with cloudwatch triggering the same in span of 10 minutes without any events.
  • How do one need to make it interact with MySQL databases that have been hosted on AWS.

I have my application built on SpringBoot running on multiple instances with a shared database (single source of truth). I have devised everything stated above using Spring's in-built scheduler and proper synchronisation on DB level using locks but because of the distributed nature of instances, I have been advised to do the same using lambdas.

like image 861
jatin mahajan Avatar asked Apr 27 '26 13:04

jatin mahajan


1 Answers

You need to pass ScheduledEvent object to the handleRequest() of the lambda.

handleRequest(ScheduledEvent event, Contex context)

Configure a cron job that runs every 10 minutes in your cloudwatch template (if using cloudformation). This will make sure to trigger your lambda after every 10 min.

Make sure to add below mentioned dependency to your pom.

<dependency>
        <groupId>com.amazonaws</groupId>
        <artifactId>aws-lambda-java-events</artifactId>
        <version>2.2.5</version>
    </dependency>

Method 2:

You can specify something like this in your cloudformation template. This will not require any argument to be passed to your handler(), incase you do not require any event related information. This will automatically trigger your lambda as per your cron job.

"ScheduledRule": {
        "Type": "AWS::Events::Rule",
        "Properties": {
            "Description": "ScheduledRule",
            "ScheduleExpression": {
                "Fn::Join": [
                    "",
                    [
                        "cron(",
                        {
                            "Ref": "ScheduleCronExpression"
                        },
                        ")"
                    ]
                ]
            },
            "State": "ENABLED",
            "Targets": [
                {
                    "Arn": {
                        "Fn::GetAtt": [
                            "LAMBDANAME",
                            "Arn"
                        ]
                    },
                    "Id": "TargetFunctionV1"
                }
            ]
        }
    },
    "PermissionForEventsToInvokeLambdaFunction": {
        "Type": "AWS::Lambda::Permission",
        "Properties": {
            "FunctionName": {
                "Ref": "NAME"
            },
            "Action": "lambda:InvokeFunction",
            "Principal": "events.amazonaws.com",
            "SourceArn": {
                "Fn::GetAtt": [
                    "ScheduledRule",
                    "Arn"
                ]
            }
        }
    }
}
like image 196
IllegalSkillsException Avatar answered Apr 30 '26 05:04

IllegalSkillsException



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!