ResponsiblyUnranked
ResponsiblyUnranked

Reputation: 1846

How to run Lambda created in CDK on a regular basis?

As the title says - I've created a Lambda in the Python CDK and I'd like to know how to trigger it on a regular basis (e.g. once per day).

I'm sure it's possible, but I'm new to the CDK and I'm struggling to find my way around the documentation. From what I can tell it will use some sort of event trigger - but I'm not sure how to use it.

Can anyone help?

Upvotes: 9

Views: 9552

Answers (3)

Jaroslav Bezděk
Jaroslav Bezděk

Reputation: 7625

Personally, I needed to set up lambda function to run in some specific times, so using aws_cdk.core.Duration was not a way for me. That is why I share also a code snippet how to schedule a lambda function using CRON.

from aws_cdk import (
    Stack,
    aws_events,
    aws_events_targets,
    aws_lambda,
    aws_lambda_python_alpha,
)
from constructs import Construct


class MyStack(Stack):
    def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None:
        super().__init__(scope, construct_id, **kwargs)

        # define lambda function
        lambda_fn = aws_lambda_python_alpha.PythonFunction(
            self,
            id="MyLambdaFunction",
            entry="/path/to/my/function",
            runtime=aws_lambda.Runtime.PYTHON_3_12,
        )

        # define CRON schedule
        event_rule = aws_events.Rule(
            self,
            id="MyEventRuleId",
            schedule=aws_events.Schedule.cron(day="10", hour="8", minute="0"),
        )

        # assign lambda to CRON schedule
        event_rule.add_target(aws_events_targets.LambdaFunction(handler=lambda_fn))

Upvotes: 4

Steve Chambers
Steve Chambers

Reputation: 39404

The question is for Python but thought it might be useful to post a Javascript equivalent:

const aws_events = require("aws-cdk-lib/aws-events");
const aws_events_targets = require("aws-cdk-lib/aws-events-targets");

const MyLambdaFunction = <...SDK code for Lambda function here...>

new aws_events.Rule(this, "my-rule-identifier", {
    schedule: aws_events.Schedule.rate(aws_cdk_lib.Duration.days(1)),
    targets: [new aws_events_targets.LambdaFunction(MyLambdaFunction)],
});

Note: The above is for version 2 of the SDK - might need a few tweaks for v3.

Upvotes: 2

ResponsiblyUnranked
ResponsiblyUnranked

Reputation: 1846

Sure - it's fairly simple once you get the hang of it.

First, make sure you're importing the right libraries:

from aws_cdk import core, aws_events, aws_events_targets

Then you'll need to make an instance of the schedule class and use the core.Duration (docs for that here) to set the length. Let's say 1 day for example:

lambda_schedule = aws_events.Schedule.rate(core.Duration.days(1))

Then you want to create the event target - this is the actual reference to the Lambda you created in your CDK earlier:

event_lambda_target = aws_events_targets.LambdaFunction(handler=lambda_defined_in_cdk_here)

Lastly you bind it all together in an aws_events.Rule like so:

lambda_cw_event = aws_events.Rule(
    self,
    "Rule_ID_Here",
    description=
    "The once per day CloudWatch event trigger for the Lambda",
    enabled=True,
    schedule=lambda_schedule,
    targets=[event_lambda_target])

Hope that helps!

Upvotes: 33

Related Questions