amazon-web-servicesaws-lambda

how do I detect if my lambda function was triggered by a schedule?


I have a lambda with a schedule - however the schedule has to be Aus eastern time, and so account for daylight savings - so I'm using a hack I read about here where I schedule it for both utc +10 and utc +11 - and then at the first line check if it's actually 2am and just exit if it's not

However - sometimes I explicitly run it - from a console or from another app - in and that case I always want it to run.

So what I want to be able to do is somehow check if it's running from the eventbridge schedule. I'm imagining something like this:

    if ((context.Source == EventBridge) && (hour_in_sydney_time != 2))
        return;

is there any information in the lambda context (or otherwise) that I can use to tell that it's being called from the schedule?


Solution

  • The event object from EventBridge looks like:

    {
        "version": "0",
        "id": "53dc4d37-cffa-4f76-80c9-8b7d4a4d2eaa",
        "detail-type": "Scheduled Event",
        "source": "aws.events",
        "account": "123456789012",
        "time": "2015-10-08T16:53:06Z",
        "region": "us-east-1",
        "resources": [
            "arn:aws:events:us-east-1:123456789012:rule/my-scheduled-rule"
        ],
        "detail": {}
    }
    

    You can match the source field, e.g.:

    if event.get("source") != "aws.events":
        do_stuff
    

    See more in the EventBridge docs.