Cloudformation设置目标组和ASG的问题

44次阅读
没有评论

问题描述

正在尝试使用Cloudformation设置一个ASG,并且希望将一个目标组与ASG关联起来。目标组已经成功创建,但是当用户尝试将其附加到ASG时,出现了错误消息。用户希望能够解决这个问题,因为他想在ASG前面使用一个ALB。
错误消息:

Value of property TargetGroupARNs must be of type List of String

Cloudformation代码:

TargetGroup:
  Type: AWS::ElasticLoadBalancingV2::TargetGroup
  Properties:
    VpcId:
      Fn::ImportValue:
        !Sub "${Prefix}-VpcId"
    TargetType: instance
    Port: 443
    Protocol: HTTPS
AutoScalingGroup:
  Type: AWS::AutoScaling::AutoScalingGroup
  Properties:
    AutoScalingGroupName: Autoscaling Group Alpha
    Cooldown: 120
    DesiredCapacity: 1
    LaunchConfigurationName: AlphaLaunchConfiguration
    MaxSize: 1
    MinSize: 1
    Tags:
      - Key: Name
        Value: Alpha ASG
        PropagateAtLaunch: 'true'
    TargetGroupARNs:
      Ref: TargetGroup

解决方案

请注意以下操作注意版本差异及修改前做好备份。

方案1

根据错误消息,TargetGroupARNs的值应该是一个字符串列表。根据Cloudformation文档,可以使用!Ref函数来引用目标组资源。
以下是修改后的Cloudformation代码:

TargetGroup:
  Type: AWS::ElasticLoadBalancingV2::TargetGroup
  Properties:
    VpcId:
      Fn::ImportValue:
        !Sub "${Prefix}-VpcId"
    TargetType: instance
    Port: 443
    Protocol: HTTPS
AutoScalingGroup:
  Type: AWS::AutoScaling::AutoScalingGroup
  Properties:
    AutoScalingGroupName: Autoscaling Group Alpha
    Cooldown: 120
    DesiredCapacity: 1
    LaunchConfigurationName: AlphaLaunchConfiguration
    MaxSize: 1
    MinSize: 1
    Tags:
      - Key: Name
        Value: Alpha ASG
        PropagateAtLaunch: 'true'
    TargetGroupARNs:
      - !Ref TargetGroup

在上面的代码中,我们使用!Ref函数将目标组资源引用为一个字符串列表,然后将其赋值给TargetGroupARNs属性。

方案2

另一种解决方案是将TargetGroupARNs的值从Ref: TargetGroup更改为- !Ref TargetGroup
以下是修改后的Cloudformation代码:

TargetGroup:
  Type: AWS::ElasticLoadBalancingV2::TargetGroup
  Properties:
    VpcId:
      Fn::ImportValue:
        !Sub "${Prefix}-VpcId"
    TargetType: instance
    Port: 443
    Protocol: HTTPS
AutoScalingGroup:
  Type: AWS::AutoScaling::AutoScalingGroup
  Properties:
    AutoScalingGroupName: Autoscaling Group Alpha
    Cooldown: 120
    DesiredCapacity: 1
    LaunchConfigurationName: AlphaLaunchConfiguration
    MaxSize: 1
    MinSize: 1
    Tags:
      - Key: Name
        Value: Alpha ASG
        PropagateAtLaunch: 'true'
    TargetGroupARNs:
      - !Ref TargetGroup

在上面的代码中,我们将TargetGroupARNs的值从Ref: TargetGroup更改为- !Ref TargetGroup,这样就将目标组资源引用为一个字符串列表。
请注意,根据Cloudformation文档,TargetGroupARNs属性是一个字符串列表,因此需要使用-符号来表示列表中的每个元素。

正文完