CDK创建ALB时如何设置"targets"?

How to set "targets" in creating ALB in CDK?

当前代码

import * as elasticloadbalancingv2 from "@aws-cdk/aws-elasticloadbalancingv2";
.
.
.

target: ec2.Instance

const targetGroups = new elasticloadbalancingv2.ApplicationTargetGroup(this, "TargetGroup", {
  healthCheck: {
    path: "/",
    port: "80",
    protocol: elasticloadbalancingv2.Protocol.HTTP
  },
  port: 80,
  protocol: elasticloadbalancingv2.ApplicationProtocol.HTTP,
  targets: [new elasticloadbalancingv2.IpTarget(target.instancePrivateIp)],
  targetType: elasticloadbalancingv2.TargetType.IP,
  vpc,
})

问题

此代码有效,但 IpTargetdeprecated

我无法理解如何替换它。

如何在不使用任何已弃用的 class 的情况下使其工作?


固定代码

    import * as elasticloadbalancingv2 from "@aws-cdk/aws-elasticloadbalancingv2";
    import * as elasticloadbalancingv2targets from "@aws-cdk/aws-elasticloadbalancingv2-targets";

    .
    .
    .

    const pgAdminTarget: elasticloadbalancingv2targets.InstanceIdTarget[] = [];
    pgAdminTarget.push(new elasticloadbalancingv2targets.InstanceIdTarget(props.instance.instanceId, 80));

    const pgAdminTg = new elasticloadbalancingv2.ApplicationTargetGroup(this, "TargetGroup", {
      healthCheck: {
        path: "/health.html",
        port: "80",
        protocol: elasticloadbalancingv2.Protocol.HTTP
      },
      port: 80,
      protocol: elasticloadbalancingv2.ApplicationProtocol.HTTP,
      targetType: elasticloadbalancingv2.TargetType.INSTANCE,
      targets: [pgAdminTarget],
      vpc,
    })

    const alb = new elasticloadbalancingv2.ApplicationLoadBalancer(this, "ALB", {
      vpc,
      internetFacing: true,
      loadBalancerName: "ec2-alb",
      vpcSubnets: { subnetType: ec2.SubnetType.PUBLIC },
    });

    alb.addListener("lister", {
      certificates: [certificate],
      defaultTargetGroups: [pgAdminTg],
      port: 443,
      protocol: elasticloadbalancingv2.ApplicationProtocol.HTTPS,
    });

新错误

Property 'attachToApplicationTargetGroup' is missing in type 'InstanceIdTarget[]' but required in type 'IApplicationLoadBalancerTarget'. targets: [pgAdminTarget],

node_modules/@aws-cdk/aws-elasticloadbalancingv2/lib/alb/application-target-group.d.ts:291:5 291 attachToApplicationTargetGroup(targetGroup: IApplicationTargetGroup): LoadBalancerTargetProps; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 'attachToApplicationTargetGroup' is declared here.

根据 the document,将目标添加到负载均衡器时会自动调用 attachToApplicationTargetGroup

我想我不需要调用它,但错误提示类型 InstanceIdTarget[] 中缺少 attachToApplicationTargetGroup

有什么问题?

您需要使用 aws-cdk.aws-elasticloadbalancingv2-targets 包。

它还有一个 IpTarget 结构。

您也可以只使用实例目标并将其指向您的实例:

new InstanceTarget(instance: Instance, port?: number)

https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-elasticloadbalancingv2-targets.IpTarget.html

https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-elasticloadbalancingv2-targets.InstanceTarget.html

编辑: 关于您的编辑,将数组传递给 targets 而不是内部包含数组的数组:

targets: [pgAdminTarget], 应该是 targets: pgAdminTarget