如何使用Cloud Formation或AWS CLI向自动扩展组添加扩展策略

时间:2018-09-13 00:52:19

标签: amazon-ec2 amazon-cloudformation autoscaling

如何使用Cloud Formation或AWS CLI将扩展策略添加到新的或现有的自动扩展组中

1 个答案:

答案 0 :(得分:1)

有一个重要的例子,但是下面是我现有的云形成模板之一的摘录。

1)参数 您应该将最大值和最小值作为参数

2)自动缩放组本身 我在下面包含了它,但是如果您不想包含它,可以将其作为参数。您还可以使用一个条件,该条件可以使用参数的现有条件作为条件来确定是否应创建ASG。请注意,如果确实使用该条件,则还将在所有带有in语句的引用上使用该条件(以确定是否在模板或参数中使用本地ASG)。

3)警报 这是自动缩放组的关键元素-确定警报。我正在使用群集的内存保留,但是我会说CPU是最常见的。您可以使用任何指标Cloudwatch监视器,甚至可以使用自定义指标。

4)政策 我目前正在快速做出上下反应...一个新实例产生影响大约需要30-60s,这就是为什么两次活动之间需要120s。您需要了解您的系统才能正确选择正确的数量,以避免过度扩展。

ECSClusterAutoScalingGroup:
  Type: 'AWS::AutoScaling::AutoScalingGroup'
  Condition: notDedicated
  Properties:
    VPCZoneIdentifier:
    - 'Fn::ImportValue': !Sub '${VPC}-PrivateSubnet1'
    - 'Fn::ImportValue': !Sub '${VPC}-PrivateSubnet2'
    - 'Fn::ImportValue': !Sub '${VPC}-PrivateSubnet3'
    MinSize: !Ref MinSize
    MaxSize: !Ref MaxSize
    HealthCheckGracePeriod: '600'
    HealthCheckType: EC2
    LaunchConfigurationName: !Ref ECSLaunchConfiguration
    MetricsCollection:
    - Granularity: 1Minute
ECSClusterScaleOutPolicy:
  Type: 'AWS::AutoScaling::ScalingPolicy'
  Condition: AutoScaleNotDedicated
  Properties:
    AdjustmentType: ChangeInCapacity
    AutoScalingGroupName: !Ref ECSClusterAutoScalingGroup
    Cooldown: '120'
    ScalingAdjustment: '1'
ECSClusterScaleOutAlarm:
  Type: 'AWS::CloudWatch::Alarm'
  Condition: AutoScaleNotDedicated
  Properties:
    EvaluationPeriods: '1'
    Statistic: Average
    Threshold: '70'
    AlarmDescription: Scale up alarm when Memory Reservation > 70% for 1 minute
    Period: '60'
    AlarmActions:
    - !Ref ECSClusterScaleOutPolicy
    Namespace: AWS/ECS
    Dimensions:
    - Name: ClusterName
      Value: !Ref ECSCluster
    ComparisonOperator: GreaterThanThreshold
    MetricName: MemoryReservation
ECSClusterScaleInPolicy:
  Type: 'AWS::AutoScaling::ScalingPolicy'
  Condition: AutoScaleNotDedicated
  Properties:
    AdjustmentType: ChangeInCapacity
    AutoScalingGroupName: !Ref ECSClusterAutoScalingGroup
    Cooldown: '120'
    ScalingAdjustment: '-1'
ECSClusterScaleInAlarm:
  Type: 'AWS::CloudWatch::Alarm'
  Condition: AutoScaleNotDedicated
  Properties:
    EvaluationPeriods: '1'
    Statistic: Average
    Threshold: '45'
    AlarmDescription: Scale down alarm when Memory Reservation <= 45% for 5 minutes
    Period: '300'
    AlarmActions:
    - !Ref ECSClusterScaleInPolicy
    Namespace: AWS/ECS
    Dimensions:
    - Name: ClusterName
      Value: !Ref ECSCluster
    ComparisonOperator: LessThanOrEqualToThreshold
    MetricName: MemoryReservation