You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

CDK v2技术问题:如何将查询到的ALB附加到NLB目标组

解决方案:将查询到的ALB添加为NLB目标组的目标

你已经创建了ALB类型的目标组,但缺少将查询到的ALB关联为目标的步骤。在AWS CDK中,只需调用目标组的addTarget()方法即可完成这一操作,和控制台手动附加的效果一致。

修改后的完整代码如下:

const alb = elbv2.ApplicationLoadBalancer.fromLookup(this, 'vtocALB', {
  loadBalancerArn: "arn:aws:elasticloadbalancing:us-west-2:111111:loadbalancer/app/xxxx-alb/8eefa0843c6b512f"
});

// Lookup a VPC by its name
const vpc = ec2.Vpc.fromLookup(this, 'nameHere', {
  vpcName: 'Name',
});

// 创建内部NLB,通过VPC Link指向API网关
const nlb = new elbv2.NetworkLoadBalancer(this, 'AuthNLB', {
  vpc,
  internetFacing: false, 
});
const tg = new elbv2.NetworkTargetGroup(this, 'AuthTGAlb', {
  port: 443,
  protocol: elbv2.Protocol.TCP,
  vpc: vpc,
  targetType: elbv2.TargetType.ALB,
});

// 将查询到的ALB添加为目标组的目标
tg.addTarget(alb);

const listener = nlb.addListener('listener80', {
  port: 80,
  defaultTargetGroups: [tg],
});

如果直接传入alb对象不生效,也可以显式指定ALB的ARN作为目标ID:

tg.addTarget({
  id: alb.loadBalancerArn,
});

内容的提问来源于stack exchange,提问作者Mike

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.10 21:55:02