如何获取云形成模板中指定的ECS实例id tp?

How to get the ECS instance id tp specify in the cloud formation template?

我正在为我的 ECS 服务构建云形成模板(YML 格式)并卡在负载均衡器目标组中,它无法附加到我的 ECS 实例并尝试通过引用添加 Targets这个官方 AWS 文档 https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-elasticloadbalancingv2-targetgroup-targetdescription.html

下面是我的目标组,当我多次停止启动(终止)我的实例时,我的实例 ID 将一直在变化,并且不会是静态的,如 VPC 或子网 ID 和如何在目标的 Id 字段中动态构建值?

TargetGroup:
    Type: AWS::ElasticLoadBalancingV2::TargetGroup
    Properties: 
      Matcher: 
       HttpCode: "200"
      Name: "foo"
      Port: "8080"
      Protocol: "HTTP"
      Targets:
        Id: String // This I need to build dynamically
        Port: 8080
      TargetType: "instance"
      UnhealthyThresholdCount: 3
      VpcId: "vpc-79251d11"            

注意:我尝试搜索 EC2 资源并找到了这个 https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/quickref-ec2.html 但它对我没有帮助。 另外,我正在使用 ASG 和 LC 创建我的 ECS 实例。

基于聊天中的讨论。

由于实例将 运行 在 Auto Scaling Group 中,因此无需在类型的 TargetGroup 资源中直接指定它们的 ID AWS::ElasticLoadBalancingV2::TargetGroup

TargetGroup ARN 应该在 AWS::AutoScaling::AutoScalingGroup 资源中提供。具体来说,TargetGroupARNs参数:

A list of Amazon Resource Names (ARN) of target groups to associate with the Auto Scaling group. Instances are registered as targets in a target group, and traffic is routed to the target group.

例如,由于您的 AWS::ElasticLoadBalancingV2::TargetGroup 资源被称为 TargetGroup,在定义您的 ASG 时,您将执行以下操作(如果模板文件相同):

MyASG:
    Type: AWS::AutoScaling::AutoScalingGroup
    Properties: 
      # only one parameter shown
      TargetGroupARNs: 
        - !Ref TargetGroup

当然,您会跳过 TargetGroup 中的 Targets 参数。这将使 MyASG 自动成为 register/de-register 您来自 TargetGroup.

的实例