3
votes

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.

2
May I ask why you cannot use Cloudwatch events? - namuny
I wanted to but doesn't know how to do it. That is why looking for resources or tutorials on the internet. - jatin mahajan

2 Answers

3
votes

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"
                ]
            }
        }
    }
}
0
votes
  • If you want to run a cronjob from cloudwatch event is the only option.
  • If you don't want to use cloudwatch events then go ahead with EC2 instance. But EC2 will cost you more than the cloudwatch event.

Note: Cloudwatch events rule steup is just like defining cronjob in crontab in any linux system, nothing much. In linux serevr you will define everything as a RAW one but here its just an UI based one.