How to stop/start an EC2 instance on a schedule within an autoscale group without terminating the instance?

前端 未结 2 1875
孤街浪徒
孤街浪徒 2021-01-26 12:29

If you have have an autoscaling group, how do you go about start/stopping the instances (not launch/terminate) ? The default behaviour is only for launching an

2条回答
  •  梦毁少年i
    2021-01-26 12:55

    First and foremost, the AWS autoscaling group is a container for multiple instances that are based on a launch configuration. If we are able to disable the processes that trigger up/down scaling, we are back to the container that just holds instances.

    In order to disable these processes, we need to use the suspend-processes command from AWS-CLI. For this example, I will use powershell, but its just as easy to write in bash:

    # suspend HealthCheck and ReplaceUnhealthy processes, you may find another combination works better for you.
    $asGroup = "nameOfYourAutoScalingGroup" ;
    aws autoscaling suspend-processes `
        --auto-scaling-group-name $asGroup `
        --scaling-processes HealthCheck ReplaceUnhealthy ;
    
    # verify the change
    awsp autoscaling describe-auto-scaling-groups `
        --auto-scaling-group-name $asGroup ;
    

    For my purposes, I wanted the instances to be online between 7am and 7pm to reduce costs. These instances are used between our development and QA teams, and they prefer to keep the state of the server from day to day.

    NOTE: in the circumstance where the EC2 instance becomes corrupt or accidentally terminated, our team is fine with rollbacking back to the latest AMI (they really just want the logs to persist, but if they are lost, it isn't the end of the world)

    Next we'll require a script to start/stop the servers, here I have it as 2 scripts, but you can easily optimize it into one script and pass an argument in:

    # in our case, we want to perform this to all autoscaling groups
    # you'll need Powershell 3.0+ in order to use ConvertFrom-Json
    $asGroups = aws autoscaling describe-auto-scaling-groups --query 'AutoScalingGroups[*].{Name:AutoScalingGroupName,Instances:Instances[*].InstanceId}' ;
    $asGroups = "{ asGroups: $asGroups }" | ConvertFrom-Json ;
    
    # foreach autoscaling group, go through each instance and start
    foreach ($asGroup in $($asGroups.asGroups)) {
    
        echo "AS: $($asGroup.Name)" ;
        foreach ($instance in $asGroup.instances) {
            echo "starting instance: $instance";
            aws ec2 start-instances `
                --instance-ids $instance ;
        }
    }
    
    # in our case, we want to perform this to all autoscaling groups
    # you'll need Powershell 3.0+ in order to use ConvertFrom-Json
    $asGroups = awsp autoscaling describe-auto-scaling-groups --query 'AutoScalingGroups[*].{Name:AutoScalingGroupName,Instances:Instances[*].InstanceId}' ;
    $asGroups = "{ asGroups: $asGroups }" | ConvertFrom-Json ;
    
    # foreach autoscaling group, go through each instance and stop
    foreach ($asGroup in $($asGroups.asGroups)) {
    
        echo "AS: $($asGroup.Name)" ;
        foreach ($instance in $asGroup.instances) {
            echo "stopping instance: $instance";
            awsp ec2 stop-instances `
                --instance-ids $instance ;
        }
    }
    

    The last step would be to add it to the Scheduled Tasks on a control server (I'm currently just using my desktop which never turns off). Attached is an example of the exported Schedule Task, runs weekly Mon, Tue, Wed, Thurs, Friday at 7am.

    
    
      
        2014-08-22T13:13:02.2103946
        localhost\Administrator
      
      
        
          2014-08-22T07:00:00
          true
          
            
              
              
              
              
              
            
            1
          
        
      
      
        
          localhost\Administrator
          InteractiveToken
          LeastPrivilege
        
      
      
        IgnoreNew
        true
        true
        true
        false
        false
        
          true
          false
        
        true
        true
        false
        false
        false
        P3D
        7
      
      
        
          C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
          -ExecutionPolicy ByPass c:\tasks\AWS-Autoscaling-EC2-Start-Morning.ps1
          c:\tasks
        
      
    
    

    You'll need to make the "Stop" version of this task to stop servers at 7pm. Just change your start boundary to 2014-08-22T19:00:00 and update -ExecutionPolicy ByPass c:\tasks\AWS-Autoscaling-EC2-Start-Morning.ps1 to the correct ps1.

提交回复
热议问题