1
votes

I have multiple cloudwatch events. Each of them triggers the same Lambda called app with different inputs at the same time: i.e. event1 triggers lambda app at a schedule using input: app_name=app1 event2 triggers lambda app at the same schedule using input: app_name=app2. event3 triggers lambda app at the same schedule using input: app_name=app3.

As you can see all the event has the same schedule. I really do not need so many duplicated events.

Is there any way I can use one CloudWatch event to trigger one Lambda with multiple input? i.e. at a time, the same event will trigger lambda app with input app1; it also triggers the same lambda with input app2, it also triggers the same lambda with input app3?

it will make my structure neat. one event, one lambda (with different input) for multiple app.

1
Can you clarify why you need multiple inputs, what are you doing inside your lambda? Shouldn't your inputs be a constant/env var inside your Lambda? - jogold
jogged: e.g. I want to have CloudWatch schedule event1=every day at 10PM to trigger lambda to start EC2_1; event2=every day at 10PM (same time) to trigger lambda to start EC2_2; event3=every day at 10PM to trigger lambda to start EC2_3; as you can see the event is the same. the only reason I use event1, event2, event 3 is otherwise I cannot input different input (i.e. ec2_1, ec2_2, ec2_3) to the lambda. I want to have one event schedule to trigger the same lambda with different input or even different lambda. Thanks, - user389955

1 Answers

1
votes

You can have one CloudWatch Rule with a Schedule event source and one Lambda function target. You will need to configure the input to use a Constant (JSON text) with an array of data as shown here:

enter image description here

Then in your Lambda function the event will be your constant. Example with Node.js 8.10 to start EC2 instances:

const AWS = require('aws-sdk');
const ec2 = new AWS.EC2();

exports.handler = async (event) => {
  console.log('Starting instances: %j', event);
  const data = await ec2.startInstances({ InstanceIds: event }).promise();
  console.log(data);
};