EC2 startup on schedule

后端 未结 11 786
后悔当初
后悔当初 2020-12-01 09:13

I need to start up an EC2 instance at (say) 6am every day. The constraints are that I\'d like to avoid having a computer running all day to do the startup or use a paid solu

相关标签:
11条回答
  • 2020-12-01 09:51

    Autoscaling seems the best solution for your problem and AWS provides such a feature. If you are looking for a third-party solution like ylastic, but you don't want to pay for it, the only alternative I know is Scalr where I work. Scalr is open-source, so you just have to download the source code and install it yourself.

    Other alternatives includes RightScale and enStratus. To my mind RightScale free account does not include auto-scaling, while enStratus "free" plan charges autoscaling on a $0.20/server hour basis.

    0 讨论(0)
  • 2020-12-01 09:53

    Given your constraints, the desired functionality is unfortunately not covered by the two dedicated automation mechanisms available as AWS Products & Services right now:

    • Auto Scaling - is a web service designed to automatically launch or terminate Amazon Elastic Compute Cloud (Amazon EC2) instances based on user-defined policies, schedules, and health checks.
    • AWS CloudFormation - gives developers and systems administrators an easy way to create and manage a collection of related AWS resources, provisioning and updating them in an orderly and predictable fashion.

    While starting/stopping/rebooting an instance conceptually falls into the manage category of the latter, it is not available like so (which incidentally is the reason we provide a separate Task specifically for this functionality within the meanwhile deprecated Bamboo AWS Plugin and its successor Tasks For AWS).

    Consequently the approaches outlined in my answer to How to turn on/off cloud instances during office hours are still applicable, albeit with the additional constraint that you would need to find a provider hosting your script or continuous integration solution for free:

    • Hosting scripts has e.g. been possible for quite a while already by means of those cron job providers.

    • Given the current explosion in Platform as a Service (PaaS) solutions there are quite some providers, that will allow you to do host scripts and/or continuous integration solutions one way or another as well.

    Obviously you'll need to verify, whether using the free tiers available for purposes like this is acceptable according to the respective Terms of Use of a provider in question.

    0 讨论(0)
  • 2020-12-01 09:55

    There is another java based tool EC2 Scheduler that can help you for this problem. For me I wanted to make a server available to my team during office times even it is not being used by anyone. This application helped me to achieve this goal. Hope this is good for you too.

    0 讨论(0)
  • 2020-12-01 09:59

    You can now do this with Amazon OpsWorks. After you've set up the main stuff, just make a new instance and set its scaling type to "time-based" (you can't change this for some reason once the instance has been made):

    enter image description here

    Now, just click on the "Instances > Time-Based" category and setup the schedule:

    enter image description here

    0 讨论(0)
  • 2020-12-01 09:59

    I just faced the same problem and solved it by using Autoscaling just as many of the answers here mentioned. The only thing you need for that is an AMI image that you want to run and the Autoscaling API command-line tools.

    After you downloaded the tools, follow the instructions in the readme to set the environmental variables and add your AWS credentials. Than put the following commands in a batch file (this example is for Windows):

    as-create-launch-config --key "MYLAUNCHCONFIGNAME" --instance-type t1.micro --image-id MYAMI-IMAGEID --launch-config "MYLAUNCHCONFIGNAME"
    as-create-auto-scaling-group --auto-scaling-group "MYSCALINGGROUPNAME" --launch-configuration "MYLAUNCHCONFIGNAME" --availability-zones "us-east-1a,us-east-1b,us-east-1c,us-east-1d" --min-size 0 --max-size 0
    
    rem Don't restart instance after shutdown
    as-suspend-processes "MYSCALINGGROUPNAME" --processes ReplaceUnhealthy
    
    rem Start instance at 22:15
    as-put-scheduled-update-group-action --name "startMyInstance" --auto-scaling-group "MYSCALINGGROUPNAME" --min-size 1 --max-size 1   --recurrence "15 22 * * *"
    
    rem Stop instance at 23:05
    as-put-scheduled-update-group-action --name "stopMyInstance" --auto-scaling-group "MYSCALINGGROUPNAME" --min-size 0 --max-size 0 --recurrence "05 23 * * *"
    

    Replace the capitalized names with some of your choice and set the correct AMI-ID for your AMI image. A new instance based on your AMI image will start at 22:15 and terminate at 23:05. Of course you can also change the instance type and availability zone(s).

    0 讨论(0)
  • 2020-12-01 10:02

    IMHO adding a schedule to an auto scaling group is the best "cloud like" approach as mentioned before.

    But in case you can't terminate your instances and use new ones, for example if you have Elastic IPs associated with etc.

    You could create a Ruby script to start and stop your instances based on a date time range.

    #!/usr/bin/env ruby
    
    # based on https://github.com/phstc/amazon_start_stop
    
    require 'fog'
    require 'tzinfo'
    
    START_HOUR = 6 # Start 6AM
    STOP_HOUR  = 0 # Stop  0AM (midnight)
    
    conn = Fog::Compute::AWS.new(aws_access_key_id:     ENV['AWS_ACCESS_KEY_ID'],
                                 aws_secret_access_key: ENV['AWS_SECRET_ACCESS_KEY'])
    
    server = conn.servers.get('instance-id')
    
    tz = TZInfo::Timezone.get('America/Sao_Paulo')
    
    now = tz.now
    
    stopped_range = (now.hour >= STOP_HOUR && now.hour < START_HOUR)
    running_range = !stopped_range
    
    if stopped_range && server.state != 'stopped'
      server.stop
    end
    
    if running_range && server.state != 'running'
      server.start
    
      # if you need an Elastic IP
      # (everytime you stop an instance Amazon dissociates Elastic IPs)
      #
      # server.wait_for { state == 'running' }
      # conn.associate_address server.id, 127.0.0.0
    end
    

    Have a look at amazon_start_stop to create a scheduler for free using Heroku Scheduler.

    0 讨论(0)
提交回复
热议问题