DEV Community

Cover image for How to stop and start EC2 using the AWS Instance Scheduler
Thu Kha Kyawe
Thu Kha Kyawe

Posted on

How to stop and start EC2 using the AWS Instance Scheduler

  • Create EC2

  • I left VPC, Subnet, Security Group information in here. Please set up with yours.

  • To use the scheduler CLI, you must have credentials for the AWS CLI.

Your credentials must have the following permissions:

lambda:InvokeFunction – To invoke the InstanceSchedulerMain function in the scheduler stack, and to update the schedule and period information in the scheduler configuration database from the command line

cloudformation:DescribeStackResource – To retrieve the physical resource ID of the AWS Lambda function from the stack to handle the CLI request

  • The CLI in this solution requires Python 3.8+

  • Go To IAM from AWS Console

  • Click Roles

  • Click Create role

  • Select EC2 and Click Next

  • Choose AWSLambdaInvocation-DynamoDB

  • Choose AWSCloudFormationReadOnlyAccess

  • Write EC2-Instance-Scheduler-Role

  • Click Create role

  • Attach IAM role to EC2

  • Select EC2-Instance-Scheduler-Role and Click Update IAM role

  • Select EC2 and Click Connect

  • Click Connect

  • Type
python3 --version
Enter fullscreen mode Exit fullscreen mode

and click enter

  • Type
sudo yum install python3.x86_64 python3-pip.noarch
Enter fullscreen mode Exit fullscreen mode

  • Type
python3 --version
Enter fullscreen mode Exit fullscreen mode

  • Type
wget https://s3.amazonaws.com/solutions-reference/instance-scheduler-on-aws/latest/instance_scheduler_cli-1.5.3-py3-none-any.whl
Enter fullscreen mode Exit fullscreen mode

  • Type
pip install instance_scheduler_cli-1.5.3-py3-none-any.whl
Enter fullscreen mode Exit fullscreen mode

  • Type
scheduler-cli --version
Enter fullscreen mode Exit fullscreen mode

  • Type
scheduler-cli -h
Enter fullscreen mode Exit fullscreen mode

  • Download the CloudFormation for InstanceScheduler , Click Here

  • or you can directly go by Click Here
    .This will go to CloudFormation Page directly.

  • Go to CloudFormation

  • Click With new resources (standard)

  • Click Choose file

  • Click Next

Enter InstanceScheduler as a stack name

Scroll down and click on next

Scroll down and click on next

Scroll down and click on submit

  • Wait the CloudFormation stack is deployed successfully.

  • Type
scheduler-cli describe-schedules --region ap-southeast-1 --stack InstanceScheduler
Enter fullscreen mode Exit fullscreen mode
  • You will see the out like this but timezone is different with our local time
{
   "Schedules": [
      {
         "Description": "Instances running",
         "Name": "running",
         "UseMetrics": false,
         "Type": "schedule",
         "OverrideStatus": "running"
      },
      {
         "Timezone": "UTC",
         "Description": "Vertical scaling on weekdays, based on UTC time",
         "Periods": [
            "working-days@t2.micro",
            "weekends@t2.nano"
         ],
         "Name": "scale-up-down",
         "Type": "schedule"
      },
      {
         "Timezone": "US/Pacific",
         "Description": "Office hours in Seattle (Pacific)",
         "Periods": [
            "office-hours"
         ],
         "Name": "seattle-office-hours",
         "Type": "schedule"
      },
      {
         "Description": "Instances stopped",
         "Name": "stopped",
         "UseMetrics": false,
         "Type": "schedule",
         "OverrideStatus": "stopped"
      },
      {
         "Timezone": "Europe/London",
         "Description": "Office hours in UK",
         "Periods": [
            "office-hours"
         ],
         "Name": "uk-office-hours",
         "Type": "schedule"
      }
   ]
}
Enter fullscreen mode Exit fullscreen mode
  • Type
scheduler-cli describe-periods --region ap-southeast-1 --stack InstanceScheduler
Enter fullscreen mode Exit fullscreen mode
  • You will see the out like this but timezone is different with our local time
{
   "Periods": [
      {
         "Months": [
            "jan/3"
         ],
         "Description": "Every first monday of each quarter",
         "Weekdays": [
            "mon#1"
         ],
         "Name": "first-monday-in-quarter",
         "Type": "period"
      },
      {
         "Begintime": "09:00",
         "Description": "Office hours",
         "Endtime": "17:00",
         "Weekdays": [
            "mon-fri"
         ],
         "Name": "office-hours",
         "Type": "period"
      },
      {
         "Description": "Days in weekend",
         "Weekdays": [
            "sat-sun"
         ],
         "Name": "weekends",
         "Type": "period"
      },
      {
         "Description": "Working days",
         "Weekdays": [
            "mon-fri"
         ],
         "Name": "working-days",
         "Type": "period"
      }
   ]
}
Enter fullscreen mode Exit fullscreen mode
  • We have to change information at DynamoDB so go to DynamoDB from AWS Console

  • Click Explore Items

  • Select InstanceScheduler-ConfigTable-EIC9ZOW4EYKI and Edit Item

  • Change name and timezone like this and click Recreate Item

  • Type
scheduler-cli describe-schedules --region ap-southeast-1 --stack InstanceScheduler
Enter fullscreen mode Exit fullscreen mode
  • You will see the out like this and timezone is changed with our local time
{
   "Schedules": [
      {
         "Timezone": "Asia/Yangon",
         "Description": "Office hours in Seattle (Pacific)",
         "Periods": [
            "office-hours"
         ],
         "Name": "myanmar-office-hours",
         "Type": "schedule"
      },
      {
         "Description": "Instances running",
         "Name": "running",
         "UseMetrics": false,
         "Type": "schedule",
         "OverrideStatus": "running"
      },
      {
         "Timezone": "UTC",
         "Description": "Vertical scaling on weekdays, based on UTC time",
         "Periods": [
            "working-days@t2.micro",
            "weekends@t2.nano"
         ],
         "Name": "scale-up-down",
         "Type": "schedule"
      },
      {
         "Description": "Instances stopped",
         "Name": "stopped",
         "UseMetrics": false,
         "Type": "schedule",
         "OverrideStatus": "stopped"
      },
      {
         "Timezone": "Europe/London",
         "Description": "Office hours in UK",
         "Periods": [
            "office-hours"
         ],
         "Name": "uk-office-hours",
         "Type": "schedule"
      }
   ]
}
Enter fullscreen mode Exit fullscreen mode
  • Type
scheduler-cli describe-periods --region ap-southeast-1 --stack InstanceScheduler
Enter fullscreen mode Exit fullscreen mode
  • You will see the out like this and timezone is changed with our local time
{
   "Periods": [
      {
         "Months": [
            "jan/3"
         ],
         "Description": "Every first monday of each quarter",
         "Weekdays": [
            "mon#1"
         ],
         "Name": "first-monday-in-quarter",
         "Type": "period"
      },
      {
         "Begintime": "09:00",
         "Description": "Office hours",
         "Endtime": "17:00",
         "Weekdays": [
            "mon-fri"
         ],
         "Name": "office-hours",
         "Type": "period"
      },
      {
         "Description": "Days in weekend",
         "Weekdays": [
            "sat-sun"
         ],
         "Name": "weekends",
         "Type": "period"
      },
      {
         "Description": "Working days",
         "Weekdays": [
            "mon-fri"
         ],
         "Name": "working-days",
         "Type": "period"
      }
   ]
}
Enter fullscreen mode Exit fullscreen mode

Go to Tag Editor from AWS Console

  • Regions - ap-southeast-1

  • Resource types - All supported resources types

  • Tag key - Environment

  • Optional tag value - Dev

  • Click Search resources and Click Manage tags of selected resources

  • Click Add

  • Tag key - Schedule

  • Optional tag value - myanmar-office-hours

  • Click Review and apply tag changes

  • Click Apply changes to all selected


Resources & Next Steps


Top comments (0)