【问题标题】:How to configure ELB for two different ports in CDK?如何为 CDK 中的两个不同端口配置 ELB?
【发布时间】:2021-05-26 19:32:50
【问题描述】:

我有一个弹性负载均衡器为 EC2 实例提供流量。我有一个在 443 端口上运行的应用程序,它运行得很好。

现在我想在 EC2 实例的 444 端口上运行另一个应用程序。我希望能够通过访问端口 443 运行第一个应用程序,并通过访问端口 444 运行第二个应用程序。

不知何故,我无法将端口 444 添加到 CDK 中的负载平衡器。我正在做这样的事情。

 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"
            }
        });

如果只为端口 443 设置它,一切都会正常工作。但是当我尝试上述操作时,我会得到类似:

Error: Cannot add AutoScalingGroup to 2nd Target Group

我不知道这是什么意思以及如何在 cdk 中修复它...

【问题讨论】:

    标签: amazon-web-services amazon-elb aws-cdk


    【解决方案1】:

    我最终得到了这样的解决方案:

     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]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-25
      相关资源
      最近更新 更多