1
votes

I want to add a Lambda target to an existing CloudWatch rule. I used the following to refer to the existing rule:

rule = events.Rule.from_event_rule_arn(self, "Rule",event_rule_arn='')

Later I add a target with:

rule.add_target(targets.LambdaFunction(lambdaFn))

When I execute a cdk synth or deploy, I get the following error:

AttributeError: '+' object has no attribute 'add_target'

I know the IRule element does not have that method but I cannot find a clear way how to achieve what I need.

I also try using event source in Lambda but got the following error:

Unrecognized event source, must be kinesis, dynamodb stream or sqs.

2

2 Answers

0
votes

I do not think this is possible. You need to reference the lambda function and manage the rule from the stack to which the rule belongs.

0
votes

As MilanG suggests, it is not possible to do.

My use case needs to create several Lambda functions and set the same trigger to them and CloudWatch Rules is not a fit for it because of a 5 targets per rule hard limit. I use SNS instead as follows:

sns_topic = aws_sns.Topic.from_topic_arn(scope, id, topic_arn=config)

lambdaFn.add_event_source(aws_lambda_event_sources.SnsEventSource(sns_topic))