【问题标题】:Add Suspended Processes to an Autoscaling Group with Cloudformation使用 Cloudformation 将暂停的进程添加到自动缩放组
【发布时间】:2021-01-20 08:04:09
【问题描述】:

我需要将暂停的进程添加到 Cloudformation。

我尝试添加 SuspendedProcesses 属性。

  ASG:
    Type: AWS::AutoScaling::AutoScalingGroup
    Properties:
      DesiredCapacity: 1
      MinSize: 1
      MaxSize: 2
      LaunchConfigurationName: !Ref LaunchConfigurationName
      SuspendedProcesses:
        - ReplaceUnhealthy

但是,我收到一条错误消息,指出它是不受支持的属性。

【问题讨论】:

    标签: amazon-cloudformation


    【解决方案1】:

    您可以将UpdatePolicy 属性添加到您的AutoScaleGroup 来控制它。

    AWS 在此处提供了一些相关文档:
    https://aws.amazon.com/premiumsupport/knowledge-center/auto-scaling-group-rolling-updates/

    这是添加SuspendProcesses的示例:

    ASG:
      Type: AWS::AutoScaling::AutoScalingGroup
      UpdatePolicy: 
        AutoScalingRollingUpdate:
          SuspendProcesses:
            - "ReplaceUnhealthy"
      Properties:
        DesiredCapacity: 1
        MinSize: 1
        MaxSize: 2
        LaunchConfigurationName: !Ref LaunchConfigurationName
    

    有关使用UpdatePolicy 属性的完整信息可在此处获得:
    https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-attribute-updatepolicy.html#cfn-attributes-updatepolicy-rollingupdate-maxbatchsize

    【讨论】:

    • UpdatePolicy 仅适用于 CloudFormation 堆栈更新,一般不适用于自动缩放组。
    【解决方案2】:

    您可以创建一个 Lambda 函数来修改使用 CustomResource 创建的 ASG。这也需要一个 IAM::Role,因为 Lambda 函数需要一个引用作为其定义的一部分。

    大部分内容归功于https://gist.github.com/atward/9573b9fbd3bfd6c453158c28356bec05

    ASG:
      Type: AWS::AutoScaling::AutoScalingGroup
      Properties:
        DesiredCapacity: 1
        MinSize: 1
        MaxSize: 2
        LaunchConfigurationName: !Ref LaunchConfigurationName
    
    AsgProcessModificationRole:
      Type: AWS::IAM::Role
      Properties:
        AssumeRolePolicyDocument:
          Version: '2012-10-17'
          Statement:
          - Action:
            - sts:AssumeRole
            Effect: Allow
            Principal:
              Service:
              - lambda.amazonaws.com
        Policies:
          - PolicyName: AsgProcessModification
            PolicyDocument:
              Version: '2012-10-17'
              Statement:
              - Effect: Allow
                Action:
                - autoscaling:ResumeProcesses
                - autoscaling:SuspendProcesses
                Resource: "*"
              - Effect: Allow
                Action:
                - logs:CreateLogGroup
                - logs:CreateLogStream
                - logs:PutLogEvents
                Resource: arn:aws:logs:*:*:*
    
    AsgProcessModifierFunction:
      Type: AWS::Lambda::Function
      Properties:
        Description: Modifies ASG processes during CF stack creation
        Code:
          ZipFile: |
            import cfnresponse
            import boto3
            def handler(event, context):
              props = event['ResourceProperties']
              client = boto3.client('autoscaling')
              try:
                response = client.suspend_processes(AutoScalingGroupName=props['AutoScalingGroupName'], 'ReplaceUnhealthy'])
                cfnresponse.send(event, context, cfnresponse.SUCCESS, {})
              except Exception as e:
                cfnresponse.send(event, context, cfnresponse.FAILED, {})
        Handler: index.handler
        Role:
          Fn::GetAtt:
          - AsgProcessModificationRole
          - Arn
        Runtime: python2.7
    
    ModifyAsg:
      Type: AWS::CloudFormation::CustomResource
      Version: 1
      Properties:
        ServiceToken:
          Fn::GetAtt:
          - AsgProcessModifierFunction
          - Arn
        AutoScalingGroupName:
          Ref: ASG
        ScalingProcesses:
        - ReplaceUnhealthy
    

    【讨论】:

      【解决方案3】:

      如果您使用的是 aws CDK,以下应该可以工作

      const autoscaling = require('@aws-cdk/aws-autoscaling');
      const custom_resource = require('@aws-cdk/custom-resources');
      
      function stopAsgScaling(stack, asgName) {
        return new custom_resource.AwsCustomResource(stack, 'MyAwsCustomResource', {
          policy: custom_resource.AwsCustomResourcePolicy.fromSdkCalls({
            resources: custom_resource.AwsCustomResourcePolicy.ANY_RESOURCE
          }),
          onCreate: {
            service: 'AutoScaling',
            action: 'suspendProcesses',
            parameters: {
              AutoScalingGroupName: asgName,
            },
            physicalResourceId: custom_resource.PhysicalResourceId.of(
              'InvokeLambdaResourceId1234'),
          },
          onDelete: {
            service: 'AutoScaling',
            action: 'resumeProcesses',
            parameters: {
              AutoScalingGroupName: asgName,
            },
            physicalResourceId: custom_resource.PhysicalResourceId.of(
              'InvokeLambdaResourceId1234'),
          },
        })
      };
      
      class MainStack extends cdk.Stack {
        constructor(scope, id, props) {
          super(scope, id, props);
          const autoScalingGroupName = "my-asg"
          const myAsg = new autoscaling.AutoScalingGroup(
            this,
            autoScalingGroupName,
            {autoScalingGroupName: autoScalingGroupName})
      
          const acr = stopAsgScaling(this, autoScalingGroupName);
          acr.node.addDependency(myAsg);
        }
      };
      

      【讨论】:

        猜你喜欢
        • 2020-10-26
        • 1970-01-01
        • 2019-12-20
        • 2018-06-17
        • 2018-02-10
        • 2013-10-09
        • 2021-09-07
        • 2012-04-28
        • 1970-01-01
        相关资源
        最近更新 更多