Scheduling AWS Autoscaling to create Instance and run your tasks


      Introduction
      The purpose of this exercise is to not have a instance running for one off tasks but create the        
      instance dynamically using a pre configured AMI image.
      We use this insance to create reports and upload the reports to S3 bucket. The below details are           provided for configuring the instance and creating autoscaling groups which will fire up an AMI               image and run at a specified time to achieve the goal.
    Download and Configure Auto Scaling Command Line Tool 
     
      Google or use (http://ec2-downloads.s3.amazonaws.com/AutoScaling-2011-01-01.zip)
      Follow instruction in the Auto scaling tool "Readme.txt" which indicates to set the,
               Environment variables:
                 AWS_AUTO_SCALING_HOME - The directory where the deployment files were copied to
                 JAVA_HOME - Java Installation home directory
                 Add ${AWS_AUTO_SCALING_HOME}/bin
                   (in Windows: "%AWS_AUTO_SCALING_HOME%\bin") to your path
        Add your AWS Keys as indicated by the Readme.txt to a file
                ${AWS_AUTO_SCALING_HOME}/credential-file-path.template -> credential-file-path.test
        There are several ways to provide your credential information:
            a. Set the following environment variable:
                         AWS_CREDENTIAL_FILE=<the file created in above>
            b. Alternatively, provide the following option with every command
                         --aws-credential-file <the file created in above>
            c. Explicitly specify credentials on the command line: --I ACCESS_KEY --S SECRET_KEY

      Verify if the setup is working properly by running
              $ as-cmd --help
              $ as-describe-auto-scaling-groups --headers
              (You should see a header line. If you have auto scaling groups already configured, you will 
                see a description line for each auto scaling group)

     Using EC2 console Launch Instance and Configure
       Create your instance in EC2 and install software necessary for the task and create crontabs as
        necessary for schedules that need to be run. Make sure the crontabs are for "su" user or give rights for           "ec2-user" and setup the envirnment to create the AMI
 
       Note: The time on the EC2 instance will be UTC unless you configure it to EST or anyother and          
                 remember the Autoscaling schedules will be in UTC on amazon infrastructure maybe we can                         change that as well but in here we are not changing it.

    Create the AMI from AWS console for the instance configured -
        Right click on instance and choose "Create Image" and add name for created image,
          set "No reboot" true, and "delete on termination" true and create the image.

    Creating AutoScaling Group to Schedule creation of the Instance 
      Use the image id - AMI ID in the configuration of Autoscaling groups to create a schedule to run the   
      instance as necessary

Create Launch Configurations
as-create-launch-config <LaunchConfigName>
          --key <KeyToUseToConnectToInstance>
          --group <SecurityGroupForInstance>
          --region <RegionForAutoScale>
          --instance-type <InstanceType>
          --image-id <AMI_ID>
          --launch-config "<LaunchConfigName>"

       LaunchConfigName= REPORTSCHEDULELAUNCHCONFIG
       KeyToUseToConnectToInstance=
       SecurityGroupForInstance=
       AMI_ID=
       InstanceType= t1.micro
       RegionForAutoScale=

Create Auto Scaling Group
as-create-auto-scaling-group 
         --region <RegionForAutoScale>
         --auto-scaling-group "<AutoScalingGroupName>"
         --launch-configuration "<LaunchConfigName>"
         --availability-zones <AvailabilityZones>
         --min-size 0
         --max-size 0

AutoScalingGroupName=REPORTSCHEDULESCALINGGROUPNAME
AvailabilityZones=ap-southeast-2a

Create Suspend Process
as-suspend-processes 
         --region <RegionForAutoScale>
         --auto-scaling-group <AutoScalingGroupName>
         --processes ReplaceUnhealthy

Create Start and Stop Schedules
as-put-scheduled-update-group-action 
         --region ap-southeast-2
         --name "<StartScheduleName>"
         --auto-scaling-group "<AutoScalingGroupName>"
         --min-size 1
         --max-size 1
         --recurrence "0 1 * * *"

StartScheduleName=startReportScheduleInstance

The schedule creates an instance at 1 AM everyday (UTC time)

as-put-scheduled-update-group-action 
         --region <RegionForAutoScale>
         --name "<StopScheduleName>"
         --auto-scaling-group "<AutoScalingGroupName>"
         --min-size 0
         --max-size 0
         --recurrence "57 1 * * *"

StopScheduleName=stopReportScheduleInstance

The schedule terminates the instance at 1:57AM everyday (UTC time)




Comments

Popular posts from this blog

API design best practices

DB Connection Issues

Reading Excel Sheet on client side and processing it