aws-elasticloadbalancingv2: false warning about not being able to register listener on imported target group
- Dominant language
- TypeScript
- Stars
- 12.9k
- Forks
- 4.6k
- Avg merge
- 2d 3h
- Merged PRs (30d)
- 83
Description
### Describe the bug
When importing an application load balancer and a target group from another stack and registering a new listener on it using addListener, there is a warning printed during `cdk deploy` stating that registering the listener is not possible.
Deployment finishes successfully and the listener is registered on the load balancer pointing to the target group though.
Was there possibly a fix implemented but the warning not removed?
### Regression Issue
- [ ] Select this option if this issue appears to be a regression.
### Last Known Working CDK Version
_No response_
### Expected Behavior
no warning after `cdk deploy` as task is executed contrary to what is stated
### Current Behavior
> % cdk deploy --all
> [Warning at /IssueListenerStack/target-group] Cannot register listener on imported target group -- security groups might need to be updated manually [ack: @aws-cdk/aws-elbv2:albTargetGroupCannotRegisterListener]
### Reproduction Steps
ALB & target group stack:
```TypeScript
import * as cdk from "aws-cdk-lib";
import { Construct } from "constructs";
import { CfnOutput } from "aws-cdk-lib";
import * as ec2 from "aws-cdk-lib/aws-ec2";
import * as elbv2 from "aws-cdk-lib/aws-elasticloadbalancingv2";
export class IssueAlbStack extends cdk.Stack {
constructor(scope: Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);
const vpc = new ec2.Vpc(this, "vpc", {
ipAddresses: ec2.IpAddresses.cidr("10.55.0.0/16"),
maxAzs: 2,
natGateways: 0,
subnetConfiguration: [
{
cidrMask: 24,
name: "private",
subnetType: ec2.SubnetType.PRIVATE_ISOLATED,
},
],
});
const albSecurityGroup = new ec2.SecurityGroup(this, "alb-security-group", {
vpc,
allowAllOutbound: true,
});
const applicationLoadBalancer = new elbv2.ApplicationLoadBalancer(
this,
"internal-alb",
{
vpc,
internetFacing: false,
vpcSubnets: vpc.selectSubnets({
subnetType: ec2.SubnetType.PRIVATE_ISOLATED,
}),
securityGroup: albSecurityGroup,
}
);
applicationLoadBalancer.addListener("http-listener", {
port: 80,
protocol: elbv2.ApplicationProtocol.HTTP,
defaultAction: elbv2.ListenerAction.fixedResponse(404, {
contentType: "text/plain",
messageBody: "Not Found",
}),
});
new CfnOutput(this, "alb-arn", {
value: applicationLoadBalancer.loadBalancerArn,
exportName: `${this.stackName}-alb-arn`,
});
new CfnOutput(this, "alb-security-group-id", {
value: albSecurityGroup.securityGroupId,
exportName: `${this.stackName}-alb-security-group-id`,
});
new CfnOutput(this, "alb-hosted-zone-id", {
value: applicationLoadBalancer.loadBalancerCanonicalHostedZoneId,
exportName: `${this.stackName}-alb-hosted-zone-id`,
});
new CfnOutput(this, "alb-dns-name", {
value: applicationLoadBalancer.loadBalancerDnsName,
exportName: `${this.stackName}-alb-dns-name`,
});
const targetGroup = new elbv2.ApplicationTargetGroup(
this,
"s3-target-group",
{
targetType: elbv2.TargetType.IP,
protocol: elbv2.ApplicationProtocol.HTTP,
port: 443,
vpc: vpc,
healthCheck: {
path: "/",
healthyHttpCodes: "200,307,405",
port: "80",
protocol: elbv2.Protocol.HTTP,
},
}
);
new CfnOutput(this, "target-group-arn", {
value: targetGroup.targetGroupArn,
exportName: `${this.stackName}-target-group-arn`,
});
}
}
```
Listener stack:
```TypeScript
import * as cdk from "aws-cdk-lib";
import { Construct } from "constructs";
import * as elbv2 from "aws-cdk-lib/aws-elasticloadbalancingv2";
import { Fn } from "aws-cdk-lib";
export class IssueListenerStack extends cdk.Stack {
constructor(
scope: Construct,
id: string,
{ albStackName }: { albStackName: string },
props?: cdk.StackProps
) {
super(scope, id, props);
const applicationLoadBalancer =
elbv2.ApplicationLoadBalancer.fromApplicationLoadBalancerAttributes(
this,
`alb`,
{
loadBalancerArn: Fn.importValue(`${albStackName}-alb-arn`),
securityGroupId: Fn.importValue(
`${albStackName}-alb-security-group-id`
),
loadBalancerCanonicalHostedZoneId: Fn.importValue(
`${albStackName}-alb-hosted-zone-id`
),
loadBalancerDnsName: Fn.importValue(`${albStackName}-alb-dns-name`),
}
);
const targetGroup = elbv2.ApplicationTargetGroup.fromTargetGroupAttributes(
this,
`target-group`,
{
targetGroupArn: Fn.importValue(`${albStackName}-target-group-arn`),
}
);
const listener = applicationLoadBalancer.addListener(`listener`, {
port: 443,
protocol: elbv2.ApplicationProtocol.HTTP,
defaultTargetGroups: [targetGroup],
});
listener.addAction("redirect", {
action: elbv2.ListenerAction.redirect({
port: "#{port}",
protocol: "HTTP",
host: "#{host}",
path: "/#{path}index.html",
query: "#{query}",
permanent: true,
}),
conditions: [
elbv2.ListenerCondition.hostHeaders([`test.example.org`]),
elbv2.ListenerCondition.pathPatterns(["*/"]),
],
priority: 3,
});
listener.addAction("static", {
action: elbv2.ListenerAction.forward([targetGroup]),
conditions: [elbv2.ListenerCondition.hostHeaders([`test.example.org`])],
priority: 4,
});
}
}
```
Stack instantiation in bin/, using albStackName as parameter for the listener stack so Fn.importValue works (make sure to fill in your region and account)
```TypeScript
const issueAlbStack = new IssueAlbStack(app, "IssueAlbStack", {
env: {
region: "",
account: "",
},
});
const issueListenerStack = new IssueListenerStack(app, "IssueListenerStack", {
albStackName: issueAlbStack.stackName,
}, {
env: {
region: "",
account: "",
},
});
issueListenerStack.addDependency(issueAlbStack);
```
### Possible Solution
Remove warning if no longer relevant
### Additional Information/Context
_No response_
### CDK CLI Version
2.151.0 (build b8289e2)
### Framework Version
2.151.0
### Node.js Version
v20.11.0
### OS
macOS 14.5
### Language
TypeScript
### Language Version
TypeScript (5.5.4)
### Other information
_No response_
Contributor guide
Research direction
Start with the aws-elasticloadbalancingv2 ApplicationLoadBalancer.addListener path and the warning acknowledged as aws-cdk/aws-elbv2:albTargetGroupCannotRegisterListener. Reproduce the two-stack example from the issue, then verify that deployment still registers the listener without emitting a misleading warning.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- aws, typescript
- Domain
- cloud, infrastructure
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100