Skip to content

(aws-cloudwatch-actions): LambdaAction fails if added to multiple action types #29514

@morrijm4

Description

@morrijm4

Describe the bug

Adding the same lambda as the action for multiple status changes (alarm, ok, insufficient data) causes an error because of logical id conflicts.

Expected Behavior

I expect to be able to add the same lambda for multiple action types on a CloudWatch alarm.

Current Behavior

/Users/james.morrison/work/sales-domain/node_modules/.pnpm/[email protected]/node_modules/constructs/src/construct.ts:447
      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 'ApiGatewayClientErrorAlarmAlarmPermission' in Import [SlackLambda]
    at Node.addChild (/Users/james.morrison/work/sales-domain/node_modules/.pnpm/[email protected]/node_modules/constructs/src/construct.ts:447:13)
    at new Node (/Users/james.morrison/work/sales-domain/node_modules/.pnpm/[email protected]/node_modules/constructs/src/construct.ts:71:17)
    at new Construct (/Users/james.morrison/work/sales-domain/node_modules/.pnpm/[email protected]/node_modules/constructs/src/construct.ts:499:17)
    at new CfnElement (/Users/james.morrison/work/sales-domain/node_modules/.pnpm/[email protected][email protected]/node_modules/aws-cdk-lib/core/lib/cfn-element.js:1:743)
    at new CfnRefElement (/Users/james.morrison/work/sales-domain/node_modules/.pnpm/[email protected][email protected]/node_modules/aws-cdk-lib/core/lib/cfn-element.js:2:823)
    at new CfnResource (/Users/james.morrison/work/sales-domain/node_modules/.pnpm/[email protected][email protected]/node_modules/aws-cdk-lib/core/lib/cfn-resource.js:1:1399)
    at new CfnPermission (/Users/james.morrison/work/sales-domain/node_modules/.pnpm/[email protected][email protected]/node_modules/aws-cdk-lib/aws-lambda/lib/lambda.generated.js:1:113187)
    at Import.addPermission (/Users/james.morrison/work/sales-domain/node_modules/.pnpm/[email protected][email protected]/node_modules/aws-cdk-lib/aws-lambda/lib/function-base.js:2:1036)
    at LambdaAction.bind (/Users/james.morrison/work/sales-domain/node_modules/.pnpm/[email protected][email protected]/node_modules/aws-cdk-lib/aws-cloudwatch-actions/lib/lambda.js:1:947)
    at /Users/james.morrison/work/sales-domain/node_modules/.pnpm/[email protected][email protected]/node_modules/aws-cdk-lib/aws-cloudwatch/lib/alarm-base.js:1:1587

Reproduction Steps

declare const lambda: lambda.IFunction;
declare const alarm: cloudwatch.Alarm;

const action = new actions.LambdaAction(lambda);
alarm.addAlarmAction(action);
alarm.addOkAction(action);

Possible Solution

In the LambdaAction bind function, check if a permission with the same logical id has been added to the lambda using the this.lambdaFunction.permissionsNode.tryFindChild(idPrefix) function.

Additional Information/Context

No response

CDK CLI Version

2.126.0 (build fb74c41)

Framework Version

No response

Node.js Version

v18.18.0

OS

macOS Ventura Version 13.6.4 (22G513)

Language

TypeScript

Language Version

5.3.3

Other information

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions