EC2 startup on schedule
Asked Answered
H

11

22

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 solution like ylastic's.

The solution at alestic, is the closest so far. The downside of this solution is that the startup time is high because of the time required to install custom software and to move around data.

Is there a way to just boot up an instance instead of creating a new instance each time as shown in this example?

Hamer answered 30/1, 2012 at 19:18 Comment(4)
Just starting up an instance sounds like it would still eat up EC2 resources because it would need to store that image until booted.Bunton
possible duplicate of How to turn on/off cloud instances during office hoursYolandayolande
startInstances needs a running computer to start machines. So, I don't think this is the answer.Hamer
You can get a working copy of code to start instance from https://mcmap.net/q/57104/-auto-shutdown-and-start-amazon-ec2-instanceDorolisa
Y
9

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.

Yolandayolande answered 31/1, 2012 at 8:33 Comment(1)
Cool ... a definitive negative answer. I think that counts! I guess I guess I could leave a micro instance on for ever or use one of these services whenever I need it.Hamer
S
10

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

Setula answered 17/9, 2013 at 1:15 Comment(2)
I think it's important to note, that this feature doesn't work on windows instances.Political
Windows instances are supported for time based scaling according to the documentationLaughing
Y
9

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.

Yolandayolande answered 31/1, 2012 at 8:33 Comment(1)
Cool ... a definitive negative answer. I think that counts! I guess I guess I could leave a micro instance on for ever or use one of these services whenever I need it.Hamer
T
6

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.

Throaty answered 4/12, 2012 at 9:42 Comment(0)
P
3

I'd suggest to Schedule EC2 Startup using AWS Lambda.

Recommendation:
Check the suggestion from D. Svanlund, user with Ace: 2000+ pts, on this AWS Forum Thread.

Advantage:
You don't need anything more than a small script or two that you schedule. No instance to launch, just a quick invocation of the script you have built. Pick the programming language of your choice and use the AWS SDK to perform instance operations. A quite lightweight solution,

Estimated Cost:
The task running twice a day for typically less than 3 seconds with memory consumption up to 128MB typically costs less than $0.0004 USD/month (See Reference)

Scheduling
In January 2016 AWS Lambda scheduled events have been transformed into AWS CloudWatch Events. CloudWatch Events have the same scheduling capabilities as Lambda scheduled events used to have. The AWS Lambda scheduled events limitation of 5 scheduled events per region has been lifted to 50 CloudWatch Events rules.

Method
Setup EC2 types that is suitable for Start/Stop Scheduling. I recommend to use EC2-VPC/EBS. Create IAM policy and role. Trust Relationship of the newly created role (See Reference).
Configure the CloudWatch Events trigger for Lambda via Function Trigger as shown below.

CloudWatch Events - Schedule

Here is the code of the function start-server which Runtime is set to Node.js.
Change YOUR_REGION and YOUR_INSTANCE_ID to yours from Instance Console.

var AWS = require('aws-sdk');
exports.handler = function(event, context) {
 var ec2 = new AWS.EC2({region: 'YOUR_REGION'});
 ec2.startInstances({InstanceIds : ['YOUR_INSTANCE_ID'] },function (err, data) {
 if (err) console.log(err, err.stack); // an error occurred
 else console.log(data); // successful response
 context.done(err,data);
 });
};

Note: Incase you need function stop-server just change ec2.startInstances to ec2.stopInstances. You may even no need the stop function when you use Automatic Shutdown Per EC2 Boot

Logging
If the IAM role is created with necessary permissions, then Lambda function will create AWS CloudWatch Log Stream for each of its run.

AWS CloudWatch Log Stream

Parnell answered 14/7, 2016 at 10:33 Comment(0)
L
2

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).

Lubber answered 14/3, 2013 at 13:24 Comment(0)
O
1

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.

Offal answered 4/1, 2014 at 19:33 Comment(0)
R
1

The AWS Data Pipeline is uniquely suited to this task. Data Pipeline uses AWS technologies and can be configured to run AWS CLI commands on a set schedule with no external dependencies. Data Pipeline can write logs to S3 and runs in the context of an IAM role, which eliminates key management requirements. Data Pipeline is also cost effective; for example, the Data Pipeline free tier can be used to stop and start instances once per day.

https://aws.amazon.com/premiumsupport/knowledge-center/stop-start-ec2-instances/

Roquelaure answered 4/6, 2016 at 9:14 Comment(0)
P
0

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.

Procrastinate answered 27/3, 2012 at 19:23 Comment(0)
D
0

Amazon now has Scheduled Reserved Instances

Scheduled Reserved Instances: These instances are available to launch within the time windows you reserve. This option allows you to match your capacity reservation to a predictable recurring schedule that only requires a fraction of a day, a week, or a month. For example, if you have a predictable workload such as a monthly financial risk analysis, you can schedule it to run on the first five days of the month. Another example would be scheduling nightly bill processing from 4pm-12am each weekday.

but also

Term: Scheduled Reserved Instances have a 1 year term commitment.

Payment Option: Scheduled Reserved Instances accrue charges hourly, billed in monthly increments over the term.

Read more https://aws.amazon.com/ec2/purchasing-options/reserved-instances/

Darsey answered 29/3, 2016 at 22:54 Comment(0)
A
0
  • Same here! Shocked to see Amazon does not provide a built-in way to simply schedule your EC2 to start & stop at a certain time. (Well, not that shocked as they probably don't want you to be able to just turn it off, right? :-)

  • ANSWER: I found the Auto-Scaling method the best way to automate this and for free, w/o the need for third party apps or a separate server running 24/7. A good understanding of AS, AMI's and EC2's is needed here.

    Goto URL link below to see how you can add "SCHEDULED ACTIONS" to your Auto-scaling groups. Works great!

    I can now have my EC2 server spun up at 12noon and spin down (Really it gets terminated) @ 8PM. Very cool. Good luck!

  • Below is screenshot of my settings. These setting will create and launch an EC2 everyday for one hour a day. CHEERS!

http://docs.aws.amazon.com/autoscaling/latest/userguide/schedule_time.html#sch-actions_rules

Auto-Scaling Scheduled Actions:

Ascertain answered 16/5, 2016 at 0:12 Comment(0)
A
0

Amazon recently announced two new features to achieve this without custom implementations directly as configuration from AWS Web Console EC2 section.

  • Using Scheduled Scaling for Application Auto Scaling (After creating an autoscaling group, there is a tab where you can add additional time based autoscaling rules) enter image description here

  • Reserving Scheduled EC2 instances (In the EC2 console, under Instances, there is the option to reserve Scheduled EC2 instances) enter image description here

Auriol answered 16/11, 2017 at 11:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.