monte
monte

Reputation: 1885

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.

Upvotes: 2

Views: 2160

Answers (4)

pcko1
pcko1

Reputation: 991

aws-cli solution

Disclaimer: All credit goes to AWS Support because they provided me with the following command.

After you have created the rule with aws events put-rule and you have successfully added your lambda as a target to the rule with aws events put-targets, you have to run:

aws lambda add-permission \
--function-name lambda_function_name \
--statement-id lambdaResourcePolicy \
--action lambda:InvokeFunction \
--principal events.amazonaws.com \
--source-arn eventbridge_rule_arn

In the above, you only need to adjust lambda_function_name and eventbridge_rule_arn and you should be good to go. Now if you reload the lambda dashboard you should see the rule appearing as a trigger.

Upvotes: 2

mjesbar
mjesbar

Reputation: 101

Via awscli > $ aws s3api put-bucket-notification-configuration

CONSOLE

I have had the same problem, it's a little bit frustating but, i've found other way and maybe a more logical way. Triggers in Lambda Console only support a few message notification services. And seems to be mostly for test purposes. Although, there's a way to invoke your lambda function from an event in S3.

To configure S3 to send some event file at some lambda function from some event occurs on your bucket, just go to your bucket through this path in S3 Console:

BucketName > Properties > EventNotifications !

AWSCLI

there you can configure your event source, even awscli support it vi 's3api' service command:

@$ aws s3api put-bucket-notification # Deprecated
@$ aws s3api put-bucket-notification-configuration

the last one support the following destination from S3:

  1. Lambda functions
  2. SNS Topic
  3. SQS Queue

Ref using S3 Triggers with Lambda https://docs.aws.amazon.com/lambda/latest/dg/with-s3-tutorial.html#with-s3-tutorial-configure-event-source

Upvotes: 2

Aman Deep
Aman Deep

Reputation: 91

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))

Upvotes: 3

Tim
Tim

Reputation: 21

It seems like this is not possible at the moment. I have checked the aws-sdk and there is a createEventSourceMapping method but that one only allows for DynamoDB, Kinesis, etc.

https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/Lambda.html#createEventSourceMapping-property

Upvotes: 2

Related Questions