I am having an elastic load balancer serving traffic to an EC2 instance. I have an application running on port 443 and it runs just fine.
Now I want to run another application on the EC2 instance on port 444. I want to be able to run the first application by hitting port 443 and the second application by hitting port 444.
Somehow I cant manage to add port 444 to the load balancer in CDK. I am doing something like this.
const appLoadbalancer = new elbv2.ApplicationLoadBalancer(this, `${props.type}AppLoadBalancer`, {
vpc: vpc,
vpcSubnets: subnets,
internetFacing: true,
});
const httpsListener = appLoadbalancer.addListener(`${props.type}HTTPSListener`, {
port: 443,
open: true,
certificates: [props.certificate]
});
httpsListener.addTargets(`${props.type}HTTPSTarget`, {
port: 443,
targets: [autoscalingGroup],
healthCheck: {
enabled: true,
healthyHttpCodes: "200,302"
}
});
const httpsListener2 = appLoadbalancer.addListener(`${props.type}HTTPSListener2`, {
port: 444,
protocol: elbv2.ApplicationProtocol.HTTPS,
open: true,
certificates: [props.certificate]
});
httpsListener2.addTargets(`${props.type}HTTPSTarget2`, {
port: 444,
protocol: elbv2.ApplicationProtocol.HTTPS,
targets: [autoscalingGroup],
healthCheck: {
enabled: true,
healthyHttpCodes: "200,302"
}
});
Everything is working just fine if it set it up only for port 443. But When I try the above i get something like:
Error: Cannot add AutoScalingGroup to 2nd Target Group
I dont know what is meant by this and how to fix it in cdk...
I ended up with a solution like this:
const appLoadbalancer = new elbv2.ApplicationLoadBalancer(this, `${props.type}AppLoadBalancer`, {
vpc: vpc,
vpcSubnets: subnet,
internetFacing: true,
});
const tg1 = new elbv2.ApplicationTargetGroup(this, "tg1", {
vpc: vpc,
protocol: elbv2.ApplicationProtocol.HTTPS})
const tg2 = new elbv2.ApplicationTargetGroup(this, "tg2", {vpc: vpc,
protocol: elbv2.ApplicationProtocol.HTTPS, port: 444})
const httpsListener = appLoadbalancer.addListener(`${props.type}HTTPSListener`, {
port: 443,
protocol: elbv2.ApplicationProtocol.HTTPS,
open: true,
certificates: [props.certificate]
});
httpsListener.addTargetGroups("RestTarget", {
targetGroups: [tg1]
});
const httpsListener2 = appLoadbalancer.addListener(`${props.type}HTTPSListener2`, {
port: 444,
protocol: elbv2.ApplicationProtocol.HTTPS,
open: true,
certificates: [props.certificate]
});
httpsListener2.addTargetGroups("RestTarget", {
targetGroups: [tg2]
});
const ServiceAsg = autoscalingGroup.node.defaultChild as autoscaling.CfnAutoScalingGroup
ServiceAsg.targetGroupArns = [tg1.targetGroupArn, tg2.targetGroupArn]