2

I want to pass some input json along with event trigger data to AWS lambda function.

So when i configured AWS lambda with Cloudwatch Event and added input target as Constant ie {"job_name":"job1"}

Now when i execute below code in lambda event data is missing

print(str(event))

output:{"job_name":"job1"}

if i say event['Records'] lambda throws error as Keyerror 

So how to pass input along with actual event data

shiv455
  • 7,384
  • 19
  • 54
  • 93
  • Why don't you access the event object like this: `event['job_name']` ? If you've configured a CloudWatch rule with a constant JSON event input, then this constant data is used as the event data. If you just provide `{ "job_name":"job1"}` as event data, then the only key in your event data is `event['job_name']`. – s.hesse Nov 18 '17 at 09:04
  • @shesse Your comment IS the answer. You should change it into an answer. – Noel Llevares Nov 18 '17 at 17:56

1 Answers1

0

If you use a CloudWatch rule and provide a constant JSON as the event input for a target, it will override the actual CloudWatch event. From the cli documentation for put-target:

If Input is specified in the form of valid JSON, then the matched event is overridden with this constant

This means in your case you should be able to access it with event['job_name'].

This question is pretty similar to yours.

s.hesse
  • 1,900
  • 10
  • 13
  • but my question is im able to access the input json in lambda but im losing the actual event data ie.. i want to verify whats the event in lambda like s3 or cron..so when i say event['Records'] in lambda it throws Keyerror as constant json replaced the matched event..I need to fetch both matched event data along with inoput json – shiv455 Nov 19 '17 at 04:18
  • In that case you should use an InputTransformer and use a JSON string as the template. From the docs I've linked above: "If InputTransformer is specified, then one or more specified JSONPaths are extracted from the event and used as values in a template that you specify as the input to the target." (This can also be set and used in the CloudWatch console UI) – s.hesse Nov 19 '17 at 09:12
  • can i use this for S3 event as well? i mean i want to pass input JSON on s3 event as well – shiv455 Nov 20 '17 at 21:52
  • You can react to events by adding a [CloudWatch rule to match event patterns](http://docs.aws.amazon.com/AmazonCloudWatch/latest/events/CloudWatchEventsandEventPatterns.html). For a more [detailed list of supported events see here](http://docs.aws.amazon.com/AmazonCloudWatch/latest/events/WhatIsCloudWatchEvents.html) or take a look at the AWS console for CloudWatch Rules. But afaik, this is not the same as registering for an event source which can invoke a Lambda function directly, so it might not support the same event types as far as i can see. – s.hesse Nov 22 '17 at 07:29