question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[aws-events] Rule.addTarget(new LambdaFunction(Function.fromFunctionArn(...))) does not add trigger to Lambda

See original GitHub issue

No trigger on AWS Lambda for Rule Target in different CF Stack

  1. Rule.addTarget(new LambdaFunction(Function.fromFunctionArn(...))) does not add trigger to Lambda, where the Lambda is on another existing CF stack. The Rule has correct Lambda name in place and when I do edit+save on the Rule on AWS Console without changing anything, the trigger gets added to Lambda.

  2. We would like this Rule to be updated on every release to point a new Lambda (arn), so that in any time the Rule has only single target, but also the previous Lambda doesn’t have the trigger in place anymore.

Reproduction Steps

  import { Function as LFunction } from "@aws-cdk/aws-lambda";

  public eventSubscription = new Rule(
    this,
    "MyEvent for my Lambda",
    {
      eventBus: EventBus.fromEventBusArn(
        this,
        "My EventBus",
        this.stack.getEnvrionmentVariable("eventBusArn")
      ),
      eventPattern: {
        detailType: [
          "myEvent",
        ],
      },
    }
  ).addTarget(
    new LambdaFunction(
      LFunction.fromFunctionArn(
        this,
        "myLambda",
        this.config.myLambdaArn
      )
    )
  );

What did you expect to happen?

Expected the Lambda to have the trigger in place and thus get the events from EventBridge as input events.

What actually happened?

Lambda didn’t get any input trigger and didn’t get any events either.

Environment

  • CLI Version : CDK 1.61.1 (?)
  • Framework Version: CDK 1.61.1
  • Node.js Version: NodeJS 12 (?)
  • OS : aws/codebuild/standard:3.0… TBD
  • Language (Version): Typescript (?)

Other

The Rule has correct Lambda name in place and when I do edit+save on the Rule on AWS Console without changing anything, the trigger gets added to Lambda.


This is 🐛 Bug Report

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:5
  • Comments:14 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
dforsbercommented, Sep 28, 2020

I added permission for the Lambda for events.amazonaws.com principal in the same stack where the Lambda is created. Then the Rule specified in another started working. However, Lambda AWS Console does not show any triggers as source, but the resource policy (permissions tab) still shows the permission. So, it works, but AWS Console is a bit inconsistent.

In general, I tend to like the fact that a stack can’t modify resources in another stack. Not sure if this is by design or whether CF would need to have some additional permissions to be able to do cross stack changes while deploying the stack.

1reaction
yogeshdasscommented, Feb 23, 2021

one workaround is like below use CfnPermission() instead of addpermission() :

        eventBus = _events.EventBus(self, "r1", event_bus_name="eb-bus")
        _ = _lambda.Function.from_function_arn(self, "lambda",function_arn=core.Fn.import_value("lambda-arn"))

        eventBusRule = _events.Rule(self, "r2", 
            enabled=True,
            event_bus=eventBus,
            event_pattern=_events.EventPattern(
                source="com.mycompany.event",
                detail_type="test-events"
            ),
            rule_name="eventbus-rule",
            targets=[
                _events_targets.LambdaFunction( _, event=_events.RuleTargetInput.from_event_path("$.detail"))
            ]
        )
        
        _lambda.CfnPermission(self, "lambdapermissionsforeventbus",
                              principal="events.amazonaws.com",
                              action="lambda:InvokeFunction",
                              source_arn=eventBusRule.rule_arn,
                              function_name=_.function_arn
        )
Read more comments on GitHub >

github_iconTop Results From Across the Web

Why wasn't my Lambda function triggered by my EventBridge ...
I created an Amazon EventBridge rule using the AWS Command Line Interface (AWS CLI), API, or AWS CloudFormation. However, the target AWS ...
Read more >
How to create an EventBridge (CloudWatch Events) rule and ...
addTarget (new LambdaFunction(notificationFunction));. The code creates the EventBridge with the Lambda target correctly, but it doesn't add the ...
Read more >
@aws-cdk/aws-lambda - NPM Package Overview - Socket
Provided IAM roles will not automatically be given permissions to execute the Lambda function. To provide a role and grant it appropriate permissions:....
Read more >
AWS CDK with .Net Core - LinkedIn
In this blog, we are going to create Lambda with execution role, EventBus and EventBridge Rule and adding target as Lambda to the...
Read more >
@aws-cdk/aws-lambda NPM | npm.io
This is limited to supported runtimes and the code cannot exceed 4KiB. lambda.Code. ... You could create a version to your lambda function...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found