aws / aws/aws-cdk

aws-autoscaling-hooktargets: "Topic" already exists (can only use FunctionHook once per stack)

Open
#29,010 1 comment 1 reaction 1 assignee Claimed by @pahud View on GitHub
@aws-cdk/aws-autoscaling-hooktargets bug effort/medium p2
Dominant language
TypeScript
Stars
12.9k
Forks
4.6k
Avg merge
2d 3h
Merged PRs (30d)
83

Description

### Describe the bug

If you try to use `FunctionHook` more than once in a stack, or you use it once but try to attach it to more than one lifecycle event, you get this error:

> Error: A subscription with id "Topic" already exists under the scope mystack/lambdaAutoScaling

### Expected Behavior

I expected all lifecycle events to be attached to the Lambda function hook without any errors.

### Current Behavior

The stack fails to synth with an error saying the name `Topic` is used more than once.

### Reproduction Steps

```
const lambdaAutoScaling = new NodejsFunction(this, 'lambdaAutoScaling', {});
const autoScalingGroup = new AutoScalingGroup(this, 'example', {});

autoScalingGroup.addLifecycleHook('lambda-onLaunch', {
lifecycleTransition: LifecycleTransition.INSTANCE_LAUNCHING,
notificationTarget: new FunctionHook(lambdaAutoScaling),
});

autoScalingGroup.addLifecycleHook('lambda-onTerminate', {
lifecycleTransition: LifecycleTransition.INSTANCE_TERMINATING,
notificationTarget: new FunctionHook(lambdaAutoScaling),
});
```
The problem still exists if you create only one `FunctionHook` and pass it to both `addLifecycleHook()` calls.

### Possible Solution

The problem appears to be in [lambda-hook.ts:28](https://github.com/aws/aws-cdk/blob/0e6e37e2bb7c4e014e0ae8e0b61f523477af5dcf/packages/aws-cdk-lib/aws-autoscaling-hooktargets/lib/lambda-hook.ts#L28) where it creates an SNS topic with the hard-coded name `Topic`. So you can only create a single one or the name conflicts.

I guess using something like `cdk.Names.nodeUniqueId()` is needed here?

### Additional Information/Context

I'm using a loop to create four hooks - launch and terminate, for AMD and ARM architectures. So even if the events were somehow combined into an array, I'd still have to call `addLifecycleHook()` once for each architecture's auto scaling group.

### CDK CLI Version

2.126.0 (build fb74c41)

### Framework Version

_No response_

### Node.js Version

v21.6.0

### OS

Arch Linux

### Language

TypeScript

### Language Version

TypeScript (5.3.3)

### Other information

_No response_

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.