Auto Shutdown and Start Amazon EC2 Instance

后端 未结 14 1045
执念已碎
执念已碎 2020-12-04 06:49

Can I automatically start and terminate my Amazon instance using Amazon API? Can you please describe how this can be done? I ideally need to start the instance and stop the

相关标签:
14条回答
  • 2020-12-04 07:19

    Just in case somebody stumbles on this ye old question, nowadays you can achieve the same thing by adding a schedule to an auto scaling group: increase the amount of instances in an auto scaling group to 1 at certain times and decrease it back to 0 afterwards.

    And since this answer is getting a lot of views, I thought to link to a very helpful guide about this: Running EC2 Instances on a Recurring Schedule with Auto Scaling

    0 讨论(0)
  • 2020-12-04 07:19

    Even though there are ways to achieve this using auto scaling, it might not suitable for all the occasions as it terminates the instances. Cron jobs will never work for a single instance (although it can perfectly be used for situations like stopping a single instance and scheduling other instances when running many instances). You can use API calls like StartInstancesRequest, and StopInstancesRequest to achieve the same but again you have to rely on a third resource. There are many applications to schedule AWS instances with many features but for a simple solution I would recommend a free app like snapleaf.io

    0 讨论(0)
  • 2020-12-04 07:21

    I recommend you take a look at the EC2 Getting Started Guide, which shows you how to do what you need using the EC2 command line tools. You can easily script this into a cron job (on Linux / UNIX) or scheduled job on Windows to call the start and stop commands at a given time.

    If you want to do this from your own code, you can use the SOAP or REST APIs; see the Developer Guide for details.

    0 讨论(0)
  • 2020-12-04 07:23

    I believe that the initial question was a little bit confusing. It depends on what Pasta needs: 1.launch/terminate (instance store) - Auto Scaling is the right solution( Nakedible's answer) 2.start/stop EBS boot instance - Auto Scaling won't help, I use remote scheduled scripts (i.e., ec2 CLI).

    0 讨论(0)
  • 2020-12-04 07:25

    You can try using the Amazon EC2 API tools directly. There are really only two commands you need: ec2-start-instances and ec2-stop-instances. Make sure that environment variables such as EC2_HOME, AWS_CREDENTIAL_FILE, EC2_CERT, EC2_PRIVATE_KEY, etc. are properly configured and all AWS credentials, certificate and private key files are in proper location - you can find more info in the AWS EC2 API tools documentation.

    You can test the command by hand first and then, when everything works fine, configure Unix crontab or Scheduled Tasks on Windows. You can find the example below for the Linux /etc/crontab file (do not forget that all those environment variables mentioned above need to be present for 'your-account' user.

    /etc/crontab
    0 8     * * *   your-account ec2-start-instances <your_instance_id>
    0 16    * * *   your-account ec2-stop-instances <your_instance_id>
    # Your instance will be started at 8am and shutdown at 4pm.
    

    I am a developer for the BitNami Cloud project, where we package the AWS tools (including the ones I mentioned) in a free, easy to use installer that you may want to try: BitNami CloudTools pack stack

    0 讨论(0)
  • 2020-12-04 07:25

    I wrote code in Python, using the Boto library, to do this. You can adjust this for your own use. Make sure to run this as part of a cron job, and then you will be able to start-up or shut-down as many instances as you need during the cron jobs run.

    #!/usr/bin/python
    #
    # Auto-start and stop EC2 instances
    #
    import boto, datetime, sys
    from time import gmtime, strftime, sleep
    
    # AWS credentials
    aws_key = "AKIAxxx"
    aws_secret = "abcd"
    
    # The instances that we want to auto-start/stop
    instances = [
        # You can have tuples in this format:
        # [instance-id, name/description, startHour, stopHour, ipAddress]
        ["i-12345678", "Description", "00", "12", "1.2.3.4"]
    ]
    
    # --------------------------------------------
    
    # If its the weekend, then quit
    # If you don't care about the weekend, remove these three 
    # lines of code below.
    weekday = datetime.datetime.today().weekday()
    if (weekday == 5) or (weekday == 6):
        sys.exit()
    
    # Connect to EC2
    conn = boto.connect_ec2(aws_key, aws_secret)
    
    # Get current hour
    hh = strftime("%H", gmtime())
    
    # For each instance
    for (instance, description, start, stop, ip) in instances:
        # If this is the hour of starting it...
        if (hh == start):
            # Start the instance
            conn.start_instances(instance_ids=[instance])
            # Sleep for a few seconds to ensure starting
            sleep(10)
            # Associate the Elastic IP with instance
            if ip:
                conn.associate_address(instance, ip)
        # If this is the hour of stopping it...
        if (hh == stop):
            # Stop the instance
            conn.stop_instances(instance_ids=[instance])
    
    0 讨论(0)
提交回复
热议问题