AWS Lambda event parameter syntax

0

I created a lambda function to be called every month by using an EventBridge schedule. The lambda signature is as follows: module.exports.handler = async (event, context) => {...}

I am trying to figure out the "event" parameter -- I set it up so that I can call this lambda directly using a parameter like so: serverless invoke -f ScheduledBillingReportLambda** --data '2023-09-30'** -s prod and this works perfectly, but when it's called automatically from the EventBridge, it doesn''t have a parameter.

I check for this like so, and this blew up upon firing (I guess because of the way I'm checking).

if (event.length > 0) {
        var splitDates = event.toString().split("/");
        if (splitDates.length === 1) {...}

What is the correct way of checking if it's called using the above (--data ''2023-09-03") or the way it is called from the EventBridge?

In Cloudwatch is see this:

With params:

INFO [BlockedScheduledBillingReport.handler] event: 2023-09-30

From EventBridge:

INFO [BlockedScheduledBillingReport.handler] event: { version: '0', id: 'fa651916-9883-4f58-9ef3-9f5ceb28aa41', 'detail-type': 'Scheduled Event', source: 'aws.scheduler', account: '117807757432', time: '2023-10-01T06:50:00Z', region: 'us-east-1', resources: [ '...:schedule/default/Sep1Sep30' ], detail: '{}' }

  • Is the "date" being passed today's date? Is there a reason why you cannot programmatically get the date inside the function being called instead of passing a date to it?

asked 7 months ago534 views
3 Answers
0
Accepted Answer

Check the event object if it contains the "detail-type" field or not. if it does, it is from EventBrdige. If it does not, it is a manual invocation.

profile pictureAWS
EXPERT
Uri
answered 7 months ago
0

I think the question is this: Is the date parameter you are trying to parse added based on some custom logic of yours, or just the date of the invocation?

If it is just the datetime of invocation then you could read it out of the event parameters passed by EventBridge.

If the datetime is based on a custom logic, then what you could do is have the source of the event, pass that custom data into a custom event, which an EventBridge rule would then pass to Lambda. This custom data would be in the "detail" section of the EventBridge custom event.

One thing to also consider, Lambda Powertools has some nice utilities that make it easy to parse Event sources, like EventBridge, and will handle all the logging for you to make it easy to inspect what was passed, which generally makes it easier to develop and troubleshoot, imo.

answered 7 months ago
0

when it's called automatically from the EventBridge, it doesn''t have a parameter.

EventBridge automatically generates event for Lambda invocations, and its structure is described in this page. You cannot specify event for invocations triggered by EventBridge.

What is the correct way of checking if it's called using the above (--data ''2023-09-03") or the way it is called from the EventBridge?

No, there is no way to check the caller principal within your Lambda function (though you can retrieve the user identity for Cognito users from context input argument).

You might check the structure of the event, but this won't work because you can pass the same structure as other callers (e.g., EventBridge, SNS) with manual invokes to bypass the check.

The only way to tell the caller of a specific function invocation is to use CloudTrail logging to capture data events for Lambda execution and analyze the detail (e.g., userIdentity) of the invocation by its request ID. This way might not be what you want.

profile picture
HS
answered 7 months ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions