Manage schedules using Infrastructure as Code (IaC) - Instance Scheduler on AWS

Manage schedules using Infrastructure as Code (IaC)

Important

Deploy schedules using a separate template after the hub stack deployment is complete.

Instance Scheduler on AWS provides a custom resource (ServiceInstanceSchedule) that you can use to configure and manage schedules through AWS CloudFormation. The custom resource uses PascalCase keys for the same data as the Instance Scheduler config table in Amazon DynamoDB (see template below for examples). For more information on the fields for schedules, refer to Schedule Definitions. For more information on the fields for periods, refer to Period Definitions.

When you use the custom resource to create a schedule, the name of that schedule is the logical resource name of the custom resource by default. To specify a different name, use the Name property of the custom resource. The solution also adds the stack name to the schedule name as a prefix by default. If you do not want to add the stack name as a prefix, use the NoStackPrefix property.

When you use the Name and NoStackPrefix properties, make sure you choose unique schedule names. If a schedule with the same name already exists, the resource will not be created or updated.

To get started managing schedules using IaC, copy and paste the following sample template and customize as many or as few schedules as you like. Save the file as a .template file (for example: my-schedules.template), and then deploy your new template using AWS CloudFormation. For examples of completed schedule templates, refer to Sample Schedules.

AWSTemplateFormatVersion: 2010-09-09 Parameters: ServiceInstanceScheduleServiceTokenARN: Type: String Description: (Required) service token arn taken from InstanceScheduler outputs Metadata: 'AWS::CloudFormation::Designer': {} Resources: SampleSchedule1: Type: 'Custom::ServiceInstanceSchedule' Properties: ServiceToken: !Ref ServiceInstanceScheduleServiceTokenARN #do not edit this line NoStackPrefix: 'False' Name: my-renamed-sample-schedule Description: a full sample template for creating cfn schedules showing all possible values Timezone: America/New_York Enforced: 'True' Hibernate: 'True' RetainRunning: 'True' StopNewInstances: 'True' UseMaintenanceWindow: 'True' SsmMaintenanceWindow: 'my_window_name' Periods: - Description: run from 9-5 on the first 3 days of March BeginTime: '9:00' EndTime: '17:00' InstanceType: 't2.micro' MonthDays: '1-3' Months: '3' - Description: run from 2pm-5pm on the weekends BeginTime: '14:00' EndTime: '17:00' InstanceType: 't2.micro' WeekDays: 'Sat-Sun' SampleSchedule2: Type: 'Custom::ServiceInstanceSchedule' Properties: ServiceToken: !Ref ServiceInstanceScheduleServiceTokenARN #do not edit this line NoStackPrefix: 'True' Description: a sample template for creating simple cfn schedules Timezone: Europe/Amsterdam Periods: - Description: stop at 5pm every day EndTime: '17:00'

When deploying the template, you must provide ServiceTokenARN for your deployment of Instance Scheduler on AWS. This ARN can be found within CloudFormation by navigating to your deployed Instance Scheduler stack, selecting Outputs, and looking for ServiceInstanceScheduleServiceToken.

Important

Do not use the DynamoDB console or scheduler CLI to delete or modify schedules and periods that were configured using the custom resource. If you do, you will create a conflict between the stored parameters in the stack and the values in the table. Also, do not use periods configured using the custom resource in schedules created using the DynamoDB console or the scheduler CLI.

Before you delete the main Instance Scheduler stack, you must delete all additional stacks that contain schedules and periods created using the custom resource because the custom resource stacks contain dependencies on the main stack's DynamoDB table.

In the configuration DynamoDB table, schedules and periods that were configured with the custom resource can be identified by the configured_in_stack attribute. The attribute contains the Amazon Resource Name of the stack that was used to create the item.