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

使用CDK集成Amazon API Gateway与EKS:NLB关联VPC Link求助

CDK集成API Gateway与EKS:自动关联NLB至VPC Link示例

核心思路

当通过ALB Controller部署LoadBalancer类型的K8s Service时,AWS会自动创建NLB。要在CDK中感知NLB创建完成并关联至API Gateway的VPC Link,需完成三步:部署K8s Service并等待负载均衡状态就绪、获取NLB对象引用、创建VPC Link并关联API Gateway集成。

CDK示例代码(TypeScript)

import * as cdk from 'aws-cdk-lib';
import * as eks from 'aws-cdk-lib/aws-eks';
import * as apigateway from 'aws-cdk-lib/aws-apigatewayv2';
import * as ec2 from 'aws-cdk-lib/aws-ec2';

export class EksApiGatewayStack extends cdk.Stack {
  constructor(scope: cdk.App, id: string, props?: cdk.StackProps) {
    super(scope, id, props);

    // 1. 引入已存在的EKS集群(若未创建,需先初始化集群并部署ALB Controller)
    const cluster = eks.Cluster.fromClusterAttributes(this, 'ExistingCluster', {
      clusterName: 'your-eks-cluster-name',
      kubectlRoleArn: 'arn:aws:iam::123456789012:role/eks-kubectl-role',
      vpc: ec2.Vpc.fromLookup(this, 'ExistingVpc', { vpcId: 'vpc-xxxxxx' }),
    });

    // 2. 部署LoadBalancer类型的K8s Service,触发ALB Controller创建NLB
    const nlbService = cluster.addManifest('NlbService', {
      apiVersion: 'v1',
      kind: 'Service',
      metadata: {
        name: 'my-nlb-service',
        annotations: {
          'service.beta.kubernetes.io/aws-load-balancer-type': 'external',
          'service.beta.kubernetes.io/aws-load-balancer-nlb-target-type': 'ip',
          'service.beta.kubernetes.io/aws-load-balancer-scheme': 'internet-facing',
        },
      },
      spec: {
        type: 'LoadBalancer',
        selector: { app: 'my-app' },
        ports: [{ port: 80, targetPort: 8080 }],
      },
    });

    // 3. 异步获取Service的负载均衡DNS名称,等待NLB创建完成
    const lbDnsName = new eks.KubernetesObjectValue(this, 'LbDnsName', {
      cluster,
      objectType: 'Service',
      objectName: 'my-nlb-service',
      jsonPath: '.status.loadBalancer.ingress[0].hostname',
    });

    // 4. 通过DNS名称获取NLB对象引用
    const nlb = ec2.NetworkLoadBalancer.fromLookup(this, 'MyNlb', {
      loadBalancerDnsName: lbDnsName.valueAsString,
    });

    // 5. 创建API Gateway VPC Link,关联目标NLB
    const vpcLink = new apigateway.VpcLink(this, 'EksNlbVpcLink', {
      vpc: cluster.vpc,
      targets: [nlb],
      name: 'Eks-NLB-VpcLink',
    });

    // 6. 创建HTTP API并配置NLB集成
    const httpApi = new apigateway.HttpApi(this, 'EksHttpApi', {
      apiName: 'Eks-NLB-Api',
    });

    httpApi.addRoutes({
      path: '/{proxy+}',
      methods: [apigateway.HttpMethod.ANY],
      integration: new apigateway.HttpNlbIntegration('NlbIntegration', nlb, {
        vpcLink,
      }),
    });

    // 输出关键资源信息
    new cdk.CfnOutput(this, 'ApiGatewayUrl', { value: httpApi.apiEndpoint });
    new cdk.CfnOutput(this, 'NlbArn', { value: nlb.loadBalancerArn });
  }
}

关键说明

  • KubernetesObjectValue:用于异步获取K8s Service的负载均衡DNS名称,会自动等待Service状态更新至就绪,确保NLB已完成创建。
  • NLB配置注解:Service的annotations需指定ALB Controller创建NLB的规则,aws-load-balancer-type: external明确要求创建NLB而非ALB。
  • VPC Link关联:通过VpcLink打通API Gateway与VPC内NLB的网络通道,实现流量从API Gateway转发至EKS集群服务。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.18 09:55:24