Skip to content

aws-applicationautoscaling StepScalingPolicy: LambdaAction fails if same Lambda added to multiple policies #33958

@vincejtl

Description

@vincejtl

Describe the bug

Adding the same lambda as the action for to the alarms for StepSCalingPolicy for different ECS services causes the following error.
There is already a Construct with name 'LowerAlarmAlarmPermission' in NodejsFunction

This happens even with the following setting in cdk.json
"@aws-cdk/aws-cloudwatch-actions:changeLambdaPermissionLogicalIdForLambdaAction": true,

Regression Issue

  • Select this option if this issue appears to be a regression.

Last Known Working CDK Version

No response

Expected Behavior

The same lambda can be added to multiple step scaling policies for different ECS services

Current Behavior

We get the following error

/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 'LowerAlarmAlarmPermission' in NodejsFunction [JobCompletionFunction]
    at Node.addChild (node_modules/constructs/src/construct.ts:447:13)
    at new Node (node_modules/constructs/src/construct.ts:71:17)
    at new Construct (node_modules/constructs/src/construct.ts:499:17)
    at new CfnElement (node_modules/aws-cdk-lib/core/lib/cfn-element.js:1:743)
    at new CfnResource (node_modules/aws-cdk-lib/core/lib/cfn-resource.js:1:1399)
    at new CfnPermission (node_modules/aws-cdk-lib/aws-lambda/lib/lambda.generated.js:1:115380)
    at NodejsFunction.addPermission (node_modules/aws-cdk-lib/aws-lambda/lib/function-base.js:2:1059)
    at LambdaAction.bind (node_modules/aws-cdk-lib/aws-cloudwatch-actions/lib/lambda.js:1:1063)
    at node_modules/aws-cdk-lib/aws-cloudwatch/lib/alarm.js:1:5002
    at Array.map (<anonymous>)

Reproduction Steps

const stepScalingPolicy = new autoscaling.StepScalingPolicy(this, 'MyStepScalingPolicy', {
scalingTarget: serviceScalableTarget,
...
}
const stepScalingPolicy2 = new autoscaling.StepScalingPolicy(this, 'MyStepScalingPolicy2', {
scalingTarget: serviceScalableTarget2,
...
}

stepScalingPolicy.lowerAlarm?.addAlarmAction(new LambdaAction(lambda));
stepScalingPolicy2.lowerAlarm?.addAlarmAction(new LambdaAction(lambda));

Possible Solution

No response

Additional Information/Context

A similar issue has already been addressed
#30264
#29514

CDK CLI Version

2.166.0

Framework Version

No response

Node.js Version

v20.14.0

OS

macOS 14.6.1

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