aws-cloudformation - 使用 Cloudformation 将挂起的进程添加到自动缩放组

标签 aws-cloudformation

我需要将挂起的进程添加到 Cloudformation。

我尝试添加 SuspendedProcesses 属性。

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

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

最佳答案

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

记入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

关于aws-cloudformation - 使用 Cloudformation 将挂起的进程添加到自动缩放组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54204110/

相关文章:

amazon-web-services - 我想从参数部分传递多个存储桶名称,并允许它们一次性进入 iam 资源部分 - 云形成

amazon-web-services - 使用云组建为不同区域创建Ubuntu 16.04 EC2实例?

amazon-web-services - 如何根据CloudFormation参数部分中的VPC过滤子网?

aws-cloudformation - 如何在 yaml 中引用我定义的队列

amazon-web-services - 如何在堆栈中存储和重用来自 CloudFormation 自定义资源 Lambda 函数的响应数据?

amazon-web-services - 由于安全组不同,无法创建 aurora RDS 数据库集群

aws-cloudformation - Cloud Formation 模板在创建实例时出现无效的 KeyName 错误。如何修复该问题?

aws-cloudformation - 在 userdata 脚本中使用 boto3 时出现 MissingServiceIdError

aws-cloudformation - 如何在 CloudFormation 模板中为 Athena NamedQueries 指定工作组?

authentication - 使用 CloudFormation 在计算机启动期间添加用户