如何使用Cloud Formation或AWS CLI将扩展策略添加到新的或现有的自动扩展组
有一个重要的例子,但下面是我现有的一个云形成模板的片段。
1)参数您应该将最小值和最大值作为参数
2)自动缩放组本身我在下面包含它,但是如果你不想包含它,你可以将它作为参数。您还可以使用将使用现有参数作为条件的条件来确定是否应创建ASG。请注意,如果您确实使用了该条件,那么您还将使用in语句对所有引用使用该条件(以确定是否在模板中使用本地ASG或参数)。
3)警报这是自动调整组的关键要素 - 确定警报。我正在使用群集的内存预留,但我会说CPU是最常见的。您可以使用任何指标cloudwatch监视器甚至自定义指标。
4)政策我目前正在迅速做出反应......一个新实例需要大约30-60秒来产生影响,这就是我在事件之间有120秒的原因。您需要了解您的系统才能正确选择合适的数量以避免过度扩展。
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