首页 > 解决方案 > 将目标组分配给现有的 ASG

问题描述

我有以下代码:

    this.nlb = new NetworkLoadBalancer(
      this.parent,
      this.id,
      {
        vpc: this.vpc,
        vpcSubnets: {
          subnets: subnets,
        },
        internetFacing: true,
      }
    );

    const targetGroup = new NetworkTargetGroup(
      this.parent,
      `${this.id}-TG-${listenerPort}-${targetPort}`,
      {
        port: targetPort,
        protocol: Protocol.TCP_UDP,
        targetType: TargetType.INSTANCE,
        vpc: this.vpc,
      },
    );

    const listener = this.nlb.addListener(`${this.id}-TG-${listenerPort}`, {
      port: listenerPort,
      protocol: Protocol.TCP_UDP,
      defaultTargetGroups: [
        targetGroup,
      ]
    });

它按预期工作,创建网络负载均衡器、目标组和绑定到 NLB 的侦听器。

我无法完成的是将这个新的 TG 添加到现有的 AutoScaling 组中。

ASG 由另一个项目管理,但我需要将我的 TG 添加到它。

如果我尝试:

    const asg = AutoScalingGroup.fromAutoScalingGroupName(
      this.parent,
      `${this.id}-ASG-${listenerPort}-${targetPort}`,
      config.autoScalingGroup, // This has the name of the autoscaling group
    );

    const targetGroup = new NetworkTargetGroup(
      this.parent,
      `${this.id}-TG-${listenerPort}-${targetPort}`,
      {
        port: targetPort,
        protocol: Protocol.TCP_UDP,
        targets: [asg], // Add the imported ASG as target
        targetType: TargetType.INSTANCE,
        vpc: this.vpc,
      },
    );

它不起作用,因为导入的 SG 没有提供正确的方法。

标签: aws-cdkaws-sdk-js

解决方案


推荐阅读