Search code examples
jsonamazon-web-servicesaws-cloudformationautoscalingaws-opsworks

configure the autoscaling at specific hours


I want to create an Opsworks stack, with an Autoscaling Group so that the instances can be added dynamically according to the charge. Now, for my environement, there is always high utilization at 00h, 6h, 12h and 18h. I'm looking for a way to configure the autoscaling at these hours: at each of the previous hours, adding 4 instances by the Autoscaling Group, the rest of the time only one instance is online.

I'm also following this Document, so that the instance are deregistering from the layer by a Lambda Function.

Here is the snippet:

  "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
        }],
      }
    },

How can I configure this type of autoscaling? Should I use Time-based ? Any suggestion is very appreciated.


Solution

  • You can use an AWS::AutoScaling::ScheduledAction resource to automatically scale-up your AutoScalingGroup at one hour before your high-utilization periods (and scale-down e.g., 30 minutes after):

    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 * * *