Skip to content

(aws-iot-actions): Unable to bind two TopicRule with the same action of a lambda function #17508

@aritzg

Description

@aritzg

What is the problem?

cdk synth throws when binding TopicRule to same LambdaFunctionAction

throw new Error(`There is already a Construct with name '${childName}' in ${typeName}${name.length > 0 ? ' [' + name + ']' : ''}`);
            ^
Error: There is already a Construct with name '**invokedByAwsIotRule**' in Function [fn-lambda]
    at Node.addChild ./node_modules/constructs/src/construct.ts:381:13)
    at new Node (./node_modules/constructs/src/construct.ts:58:22)
    at new ConstructNode (./node_modules/@aws-cdk/core/lib/construct-compat.ts:184:24)

Same error happens if more than one TopicRule is bind to function in any way.
For example in:

  • "actions" attr of new iot.TopicRule
  • rule.addAction(callLambdaFunc);
  • callLambdaFunc.bind(rule);

Reproduction Steps

const fnLambdaFunc = new lambda.Function(this, 'fn-lambda', {
      code: lambda.Code.fromAsset('resources/fn-lambda/target/lambda-0.0.1-SNAPSHOT.jar'),
      handler: 'com.fn.lambda.Lambda',
      runtime: lambda.Runtime.JAVA_11,
      timeout: cdk.Duration.seconds(30),
    });


const callLambdaFunc  = new actions.LambdaFunctionAction(fnLambdaFunc);
const a  = new iot.TopicRule(this, 'AppRule', {
      topicRuleName: 'AppRule', 
      sql: iot.IotSql.fromStringAsVer20160323("SELECT * as payload, topic() as topic FROM '+/+/app' "),
      actions: [callLambdaFunc],
    });

const b  =new iot.TopicRule(this,  'TestConnectionRule', {
      topicRuleName: 'TestConnectionRule', 
      sql: iot.IotSql.fromStringAsVer20160323("SELECT * as payload, topic() as topic FROM '$aws/events/presence/connected/+' "),
      actions: [callLambdaFunc],
    });

What did you expect to happen?

I expected two TopicRules would be bind to the same lambda function action.

What actually happened?

Error is thrown when trying "cdk synth"

CDK CLI Version

1.132.0

Framework Version

No response

Node.js Version

v16.13.0

OS

Ubuntu 20.04.2 LTS

Language

Typescript

Language Version

No response

Other information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions