almarc
almarc

Reputation: 1658

Attach a specific event to a cloudwatch events rule

So let's say I have a cloudwatch rule that triggers the lambda function lambdaName every monday. It works perfectly fine, but now I want to customize the event it will send to that lambda function to add the data I need.

In other words, attach a JSON object to go with the event that triggers the lambdaName function. Is that possible? I'm sure it has to be a thing, but I struggle to find the proper documentation for it. I'm currently using the AWS SDK for Node.js for development. Thank you in advance.

Upvotes: 2

Views: 2908

Answers (2)

JeffR
JeffR

Reputation: 1792

You can't make any modifications to the object that's sent to Lambda if it's triggered by another AWS service, like if you're having an S3 event trigger the function.

If you use CloudWatch rules to trigger on an event then it is possible to pick apart and pass on only the parts of the object that you may need but AFAIK you can't inject anything new into the object.

The only way I know to get other data in the function is to either add Environment variables to the function or have the function grab values from the Parameter store.

If you just want to trigger a function manually on a schedule then you can specify a custom json object that's sent within CloudWatch rules, but it doesn't sound like that's what you want.

Edit: If you want to send in json by launching it through a schedule, do the following:

  1. Open CloudWatch, on the left select Events, Rules on the left.
  2. Click Create Rule
  3. Under Event Source select Schedule".
  4. Select either Fixed Rate or Cron expression for how often the function should be triggered.
  5. Under Targets click Add target.
  6. Select the function you want to launch
  7. Under Configure input select Constant (JSON text) and input whatever JSON you want sent to the function.

When the function is executed the JSON you specified should be sent in as the "Event" object.

Note that for #3 you can also select an Event Pattern and trigger it off of almost any API event, BUT you can't modify the Event object. You can either send the full object, send only specific pieces of the object, or send a completely different object.

See also: Use constant values in Cloudwatch event triggered Lambda functions

Edit 2: Looking at this again it looks like you can modify an event. In step 7 above there's also an option for Input Transformer and looking into that it looks like you can take the event object and turn it into a new object, including whatever new json that you would want. Search for "aws cloudwatch rules input transformer" or see this for more info: Tutorial: Input Transformer

Upvotes: 4

Mrk Fldig
Mrk Fldig

Reputation: 4486

So as JeffR said you can't add it to the trigger BUT

What you can do is in your handler before you call your Lambda function you add the data you need to your event and pass it to the main handler like so:

const yourFunction = require('yourFunction.js')

const funcHandler = (func) => {
    return (event, context, callback) => {

       // Query your data sources here ie event.whatever(body?) = ....
       func(event, context, callback)

    }
}

export const yourFunctionHandler = funcHandler(yourFunction)

Upvotes: 0

Related Questions