简体   繁体   English

如何使用 aws-cdk 将具有特定端口的目标添加到现有的 ApplicationTargetGroup

[英]How do I add Targets with specific port using aws-cdk to existing ApplicationTargetGroup

I have an existing LoadBalancer and ApplicationTargetGroup and I am trying to add an autoscaling group as a target as follows我有一个现有的 LoadBalancer 和 ApplicationTargetGroup,我正在尝试添加一个自动缩放组作为目标,如下所示

const asg = new autoscaling.AutoScalingGroup(this, 'AutoScalingGroup', {
      vpc,
      machineImage,
      instanceType: ec2.InstanceType.of(
        ec2.InstanceClass.T3,
        ec2.InstanceSize.MICRO
      ),
      allowAllOutbound: true,
      associatePublicIpAddress: true,
      vpcSubnets: {
        subnetType: ec2.SubnetType.PUBLIC,
      },
      keyName,
      autoScalingGroupName,
      desiredCapacity: 1,
      healthCheck: autoscaling.HealthCheck.ec2({
        grace: cdk.Duration.seconds(60),
      }),
    })

    asg.userData.addCommands(...commands)
    

    const tg = elbv2.ApplicationTargetGroup.fromTargetGroupAttributes(
      this,
      'TargetGroup',
      {
        targetGroupArn,
      }
    )
    
    tg.addTarget(asg)

This all works fine except the port for the ec2s in registered targets is 80 but I need it to be a different port, I am unsure how to do this with existing infrastructure这一切都很好,除了注册目标中的 ec2s 的端口是 80,但我需要它是一个不同的端口,我不确定如何使用现有的基础设施来做到这一点

I was about to start on a similar problem and was researching.我正要开始研究一个类似的问题并且正在研究。 I came across this that may be of help我遇到了这个可能会有所帮助

  1. He creates an ApplicationLoadBalancer and adds a listener to it on a specific port.他创建了一个 ApplicationLoadBalancer 并在特定端口上添加了一个侦听器。

    
    
    const alb = new elbv2.ApplicationLoadBalancer(this, 'alb', {
      vpc,
      internetFacing: true,
    });
    
    const listener = alb.addListener('Listener', {
      port: 80,
      open: true,
    });
    
    

  1. He then adds targets to the listener on a specific port to an auto scaling group:然后,他将目标添加到 Auto Scaling 组的特定端口上的侦听器:

    
    
    //create auto scaling group
    const asg = new autoscaling.AutoScalingGroup(this, 'asg', {
      vpc,
      instanceType: ec2.InstanceType.of(
        ec2.InstanceClass.T2,
        ec2.InstanceSize.MICRO,
      ),
      machineImage: new ec2.AmazonLinuxImage({
        generation: ec2.AmazonLinuxGeneration.AMAZON_LINUX_2,
      }),
      userData,
      minCapacity: 2,
      maxCapacity: 3,
    });
    
    // add target to the ALB listener
    listener.addTargets('default-target', {
      port: 80,
      targets: [asg],
      healthCheck: {
        path: '/',
        unhealthyThresholdCount: 2,
        healthyThresholdCount: 5,
        interval: cdk.Duration.seconds(30),
      },
    });
    
    

It looks like you want to investigate the config of the ALB listener.看起来您想调查 ALB 侦听器的配置。 Hopefully that helps.希望这会有所帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 AWS-CDK 设置 AWS Cloud9 实例? - How do I set up a AWS Cloud9 instance using AWS-CDK? 如何通过 Instance-name 获取现有 EC2 实例并使用 Python 中的 AWS CDK 将它们作为目标添加到 ALB - How can I fetch existing EC2 Instances via Instance-name and add them as targets to ALB using AWS CDK in Python 销毁 AWS-CDK 中的堆栈时不要删除现有资源 - Do not delete existing resources when destroying a stack in AWS-CDK 如何使用 AWS-CDK 将 AWS 堆栈的 cfnoutputs 输出到文件 - How to get cfnoutputs of AWS stack to a file using AWS-CDK 如何使用 aws-cdk 将查询结果小部件添加到 Cloudwatch 仪表板 - How to add Query Results Widget to Cloudwatch dashboard using aws-cdk 如何使用 aws-cdk 在 EC2 和 RDS 之间创建 DependsOn 关系 - How can I create a DependsOn relation between EC2 and RDS using aws-cdk 如何使用 aws-cdk python 设置 API 密钥 - How to set up API keys using aws-cdk python 有没有办法使用 AWS-CDK 将新的 AWS ApiGateway 连接到现有的 lambda 函数? (打字稿) - Is there a way to connect a new AWS ApiGateway to existing lambda funtion using AWS-CDK? (TypeScript) 有没有办法使用 AWS-CDK 将新的 lambda function 连接到现有的 AWS ApiGateway? (Python) - Is there a way to connect a new lambda function an existing AWS ApiGateway using AWS-CDK? (Python) 如何使用 python 中的 aws-cdk 设置 Amazon S3 通知以在您的存储桶中发生某些事件时接收通知? - How do I set up Amazon S3 notification to receive notifications when certain events happen in your bucket using aws-cdk in python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM