能否在同一CloudFormation模板中同时配置ASG动态与预测扩展?
ASG同时配置动态扩展与预测扩展报错处理
报错信息
Resource handler returned message: "You can't specify PredictiveScalingConfiguration for policy type: TargetTrackingScaling (Service: AutoScaling, Status Code: 400, Request ID: bd851c95-ad78-4afc-979b-f5e2e5bf188a)" (RequestToken: 387601f6-9734-ac0c-36bc-5e006f892bf2, HandlerErrorCode: GeneralServiceException)
问题
是否可以在同一个CloudFormation模板中同时使用动态扩展与预测扩展?
原模板代码
--- Resources: myasg: Type: AWS::AutoScaling::AutoScalingGroup Properties: MaxSize: '10' MinSize: '2' DesiredCapacity: '2' VPCZoneIdentifier: - subnet1 - subnet2 MixedInstancesPolicy: InstancesDistribution: OnDemandAllocationStrategy: lowest-price OnDemandBaseCapacity: 0 OnDemandPercentageAboveBaseCapacity: 0 SpotAllocationStrategy: lowest-price SpotInstancePools: 2 LaunchTemplate: LaunchTemplateSpecification: LaunchTemplateName: mylaunchtemplate Version: 1 Tags: - Key: Environment Value: Production PropagateAtLaunch: true - Key: Purpose Value: WebServerGroup PropagateAtLaunch: false scalingpolicy: Type: AWS::AutoScaling::ScalingPolicy Properties: AutoScalingGroupName: Ref: myasg PolicyType: TargetTrackingScaling TargetTrackingConfiguration: DisableScaleIn: false PredefinedMetricSpecification: PredefinedMetricType: ASGAverageCPUUtilization TargetValue: 30 PredictiveScalingConfiguration: MaxCapacityBreachBehavior: IncreaseMaxCapacity MaxCapacityBuffer: 0 MetricSpecifications: - TargetValue: 30 Mode: ForecastAndScale
解决方案
错误原因
你将PredictiveScalingConfiguration直接嵌套在TargetTrackingScaling类型的伸缩策略中,这是AWS不允许的配置方式。预测扩展需要创建独立的、类型为PredictiveScaling的伸缩策略,不能与目标追踪策略合并配置。
可行性说明
可以在同一个CloudFormation模板中同时使用动态扩展(如目标追踪)和预测扩展,但必须创建两个独立的伸缩策略:一个负责基于实时指标的动态扩缩容,另一个专门负责基于预测的提前扩缩容。
修改后的模板示例
--- Resources: myasg: Type: AWS::AutoScaling::AutoScalingGroup Properties: MaxSize: '10' MinSize: '2' DesiredCapacity: '2' VPCZoneIdentifier: - subnet1 - subnet2 MixedInstancesPolicy: InstancesDistribution: OnDemandAllocationStrategy: lowest-price OnDemandBaseCapacity: 0 OnDemandPercentageAboveBaseCapacity: 0 SpotAllocationStrategy: lowest-price SpotInstancePools: 2 LaunchTemplate: LaunchTemplateSpecification: LaunchTemplateName: mylaunchtemplate Version: 1 Tags: - Key: Environment Value: Production PropagateAtLaunch: true - Key: Purpose Value: WebServerGroup PropagateAtLaunch: false # 动态扩展:基于CPU使用率的目标追踪策略 targetTrackingScalingPolicy: Type: AWS::AutoScaling::ScalingPolicy Properties: AutoScalingGroupName: !Ref myasg PolicyType: TargetTrackingScaling TargetTrackingConfiguration: DisableScaleIn: false PredefinedMetricSpecification: PredefinedMetricType: ASGAverageCPUUtilization TargetValue: 30 # 预测扩展:独立的预测伸缩策略 predictiveScalingPolicy: Type: AWS::AutoScaling::ScalingPolicy Properties: AutoScalingGroupName: !Ref myasg PolicyType: PredictiveScaling PredictiveScalingConfiguration: MaxCapacityBreachBehavior: IncreaseMaxCapacity MaxCapacityBuffer: 0 MetricSpecifications: - TargetValue: 30 PredefinedMetricPairSpecification: PredefinedMetricType: ASGAverageCPUUtilization Mode: ForecastAndScale
关键修改点
- 将原有的单一策略拆分为两个独立资源:
targetTrackingScalingPolicy(负责动态扩缩容)和predictiveScalingPolicy(负责预测扩缩容) - 预测扩展策略必须指定
PolicyType: PredictiveScaling,并单独配置PredictiveScalingConfiguration - 两个策略均关联到同一个ASG,实现动态+预测的混合扩缩容逻辑
内容的提问来源于stack exchange,提问作者learner00
相关产品推荐
相关产品推荐

