在特定时间配置自动缩放

configure the autoscaling at specific hours

我想创建一个 Opsworks stack 和一个 Autoscaling Group,以便可以根据费用动态添加实例。 现在,对于我的环境,在 00h、6h、12h 和 18h 总是有很高的利用率。 我正在寻找一种在这些时间配置自动缩放的方法:在之前的每个时间,Autoscaling Group 添加 4 个实例,其余时间只有一个实例在线。

我也在关注这个 Document,以便实例通过 Lambda Function 从层中注销。

这是片段:

  "InstanceRole": {
      "Type": "AWS::IAM::Role",
      "Properties": {
        "AssumeRolePolicyDocument": {
          "Version": "2012-10-17",
          "Statement": [
          {
            "Effect": "Allow",
            "Action": [
              "opsworks:AssignInstance",
              "opsworks:DescribeInstances"
            ],
            "Resource": [ "*" ]
          }
        ]}
      }
    },
    "InstanceProfile": {
      "Type": "AWS::IAM::InstanceProfile",
      "Properties": {
        "Path": "/",
        "Roles": [{ "Ref": "InstanceRole" }]
      }
    },
    "LaunchConfig": {
      "Type" : "AWS::AutoScaling::LaunchConfiguration",
      "Properties" : {
        "AssociatePublicIpAddress" : true,
        "ImageId" : { "Fn::FindInMap": ["AWSRegionToAMI", { "Ref": "AWS::Region" }, "AMIID"] },
        "InstanceType" : {"Ref" : "InstanceType"},
        "IamInstanceProfile": {"Ref": "InstanceProfile"},
        "EbsOptimized" : true,
        "SecurityGroups" : [{ "Ref" : "SG" }],
        "UserData" : {
          "Fn::Base64": {
            "Fn::Join" : ["",
              ["#!/bin/bash",
              "sed -i'' -e 's/.*requiretty.*//' /etc/sudoers",
              "pip install --upgrade awscli",
              "INSTANCE_ID=$(/usr/bin/aws opsworks register --use-instance-profile --infrastructure-class ec2 --region us-east-1 --stack-id ",
              { "Ref": "StackID"}," --override-hostname $(tr -cd 'a-z' < /dev/urandom |head -c8) --local 2>&1 |grep -o 'Instance ID: .*' |cut -d' ' -f3) /usr/bin/aws opsworks wait instance-registered --region us-east-1 --instance-id $INSTANCE_ID
              /usr/bin/aws opsworks assign-instance --region us-east-1 --instance-id $INSTANCE_ID --layer-ids ",{ "Ref": "myLayer" }
              ]]
          }
        }
      }
    },
    "AutoScalingGroup": {
      "Type" : "AWS::AutoScaling::AutoScalingGroup",
      "Properties" : {
        "VPCZoneIdentifier": { "Ref": "subnetIds" },
        "LaunchConfigurationName" : { "Ref" : "LaunchConfig"},
        "LoadBalancerNames" : [{"Ref" : "ELB"}],
        "MaxSize": { "Ref": "MaxSize" },
        "MinSize": 1,
        "Tags":
        [{
          "Key": "Name",
          "Value": "opsworks_stack_id",
          "PropagateAtLaunch": true
        }],
      }
    },

如何配置这种类型的自动缩放?我应该使用 Time-based 吗? 非常感谢任何建议。

您可以使用 AWS::AutoScaling::ScheduledAction 资源在高利用率时段前一小时自动扩展您的 AutoScalingGroup(并在例如 30 分钟后缩小):

ScaleUp: 
  Type: AWS::AutoScaling::ScheduledAction
  Properties:
    AutoScalingGroupName: !Ref AutoScalingGroup
    MinSize: 5
    Recurrence: 0 23,5,11,17 * * *
ScaleDown: 
  Type: AWS::AutoScaling::ScheduledAction
  Properties:
    AutoScalingGroupName: !Ref AutoScalingGroup
    MinSize: 1
    Recurrence: 30 0,6,12,18 * * *