Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

add trigger to lambda function using cli

I am trying to add a trigger rule to a lambda version using cli:

I try the following command:

aws events put-targets --rule rule-name --targets "Id"="1","Arn"="arn..."

This commands run successfully and I can see my lambda function in Event Bridge console under targets. But when I go to lambda function and to the version I don't see any trigger event being added.

I am not sure if this an error/bug or expected behavior. Is there a way to add a trigger event to a published version of lambda function such that it shows in trigger console (essentially to show that trigger event is added successfully) using aws cli.

like image 829
monte Avatar asked Nov 16 '25 08:11

monte


1 Answers

Use CDK. It will work

Create a lambda function and a rule using cdk. Then you can add that rule to lambda.

This works with CDK. But it doesn't work with CLI as you said. The trigger doesn't get added in lambda.

Sample code: Note: This is not the complete CDK code. This is just the part for creating lambda,rule and adding it to lambda. This example is in Python

        fn = lambda_.Function(self, "Name",
            runtime=lambda_.Runtime.PYTHON_3_7,
            handler="index.lambda_handler",
            role=custom_role,
            code=lambda_.Code.from_asset(
                os.path.join(
                    up_dir(__file__, 2),
                    "resources/lambda/pathtoyourcode",
                )
            ),
        )

        # Run Every Minute
        run_every_minute = _events.Rule(
            self,
            "runEveryMinute",
            schedule=_events.Schedule.rate(core.Duration.minutes(1))
        )

        # Add Lambda to CW Event Rule
        run_every_minute.add_target(_targets.LambdaFunction(fn))
like image 180
Aman Deep Avatar answered Nov 19 '25 09:11

Aman Deep