EC2 startup on schedule

后端 未结 11 793
后悔当初
后悔当初 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 10:10

    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.

    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.

    0 讨论(0)
  • 2020-12-01 10:11
    • 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:

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

    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/

    0 讨论(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/

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

    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)

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

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