Home > Net >  How to set "targets" in creating ALB in CDK?
How to set "targets" in creating ALB in CDK?

Time:11-08

Current Code

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,
})

Problem

This code is working but IpTarget is deprecated.

I cannot understand how to replace it.

How do you make it work without using any deprecated class?


Fixed Code

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

New Error

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.

According to the document, attachToApplicationTargetGroup is called automatically when you add the target to a load balancer.

I guess I don’t need to call this but error says that attachToApplicationTargetGroup is missing in type InstanceIdTarget[].

What is the problem?

CodePudding user response:

You need to use the aws-cdk.aws-elasticloadbalancingv2-targets package.

It also has an IpTarget construct.

You can also just use an instance target and point it to your instance:

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

EDIT: Regarding your edit, pass the array to targets and not an array with an array inside:

targets: [pgAdminTarget], should be targets: pgAdminTarget

  • Related