BLOGS AND INSIGHTS:
ORACLE CLOUD INFRASTRUCTURE (OCI) RESOURCE SCHEDULER: COMPLETE GUIDE FOR IMPLEMENTATION
What is OCI Resource Scheduler for?
The OCI Resource Scheduler is a cloud-native service provided by Oracle Cloud Infrastructure (OCI) to automate the management of resources such as Compute Instances, InstancePool, Autonomous Databases (ADB), and FunctionsFunction. The primary purpose is to optimize resource utilization, reduce costs, and streamline operational management by scheduling automated actions (e.g., START
, STOP
, SCALE
) on resources based on business requirements.
Key Benefits:
- Automation: Reduces manual intervention for repetitive tasks.
- Cost Efficiency: Stops idle resources during non-peak hours, avoiding unnecessary charges.
- Operational Simplicity: Provides flexible and granular control through schedules and tags.
- Scalability: Manages large deployments effectively with structured schedules.
Key Points:
- Supported Resources: This solution supports compute instances, instancepool, autonomous databases (ADB) and functionsfunction.
- Configuration Flexibility:
- Tag-Based Targeting: Resources are identified and managed using tags. For this project, the tag
FinOps-Tag.scheduler
will determine if a resource is scheduled. - Compartment Scope: All resources within a compartment can be targeted, provided they meet the tagging criteria.
- Tag-Based Targeting: Resources are identified and managed using tags. For this project, the tag
- Scheduling Parameters: Schedules are defined using cron expressions or fixed intervals. Multiple schedules can be created to handle diverse operational needs (e.g., business hours, weekends).
- Limits: Each schedule can manage a maximum of 25 resources. For larger workloads, additional schedules will need to be created.
Critical Application Readiness Requirement
All projects must verify and ensure that applications running on Compute Instances targeted by Resource Scheduler are capable of starting without manual intervention after the instance is powered on. This is a critical prerequisite to ensure uninterrupted operations and maximize the benefits of automated resource scheduling.
Why Is This Important?
- Automation Dependency: Scheduled start/stop actions are automated and do not involve manual oversight.
- Operational Continuity: Applications that cannot start automatically may cause downtime or service disruption.
- Cost Efficiency: If applications do not restart automatically, resources may be running but not performing useful work, leading to wasted costs.
How to Ensure Applications Are Ready
1. Validate Startup Configuration
- Configure the application to start automatically when the compute instance boots.
- Use startup scripts or system services to initialize application processes.
2. Use OCI Features
- Leverage Instance Metadata and Cloud-Init to configure applications to start as part of the instance boot sequence.
3. Test Boot Behavior
- Simulate a scheduled stop and start cycle for the instance.
- Verify that all applications and dependent services initialize correctly without manual intervention.
4. Monitor and Alert
- Integrate with OCI Monitoring to track application health and resource availability after scheduled starts.
- Configure alerts for any failures or delays in application startup.
5. Document Application Requirements
- Maintain a detailed runbook for each compute instance, specifying:
- Services running on the instance.
- Dependencies (e.g., network connections, databases).
- Startup scripts and configurations.
6. Engage DevOps Teams
- Collaborate with DevOps teams to ensure CI/CD pipelines deploy configurations supporting automated startup.
IAM and Permissions on OCI Resource Scheduler
To ensure the Resource Scheduler can manage resources in the desired compartment, the following policies must be configured in the tenancy:
Policy for Resource Scheduler:
Allow any-user to manage <resource type family> in compartment <compartment name> where all {request.principal.type='resourceschedule'}
Notes:
- These permissions ensure that the Resource Scheduler can start, stop, and manage tagged resources.
- Policies should be scoped to specific compartments for better control and governance.
Custom Module OCI Resource Scheduler
The oci-resource-scheduler-module
is a reusable Terraform module designed to create and manage schedules in Oracle Cloud Infrastructure (OCI) using the Resource Scheduler service. This module supports defining recurring actions (e.g., START
, STOP
) on OCI resources, filtered by tags, compartments, or lifecycle states, ensuring
optimized resource usage and cost efficiency.
This module simplifies the configuration of schedules by abstracting the complexity of the OCI Resource Scheduler API while providing flexibility to customize schedules for various use cases.
Features:
1. Supports Recurrence Types:
CRON
format (e.g.,0 8 * * 1-5
for weekdays at 8 AM).ICAL
format (RFC-5545 compliant).
2. Resource Filtering:
- Target resources using Defined Tags, Compartments, or
- Lifecycle State.
3. Flexible Actions:
- Automate actions like code;
START_RESOURCE
orSTOP_RESOURCE
.
4. Tag Integration:
- Freeform and defined tags for resource categorization.
5. Optional Start/End Times:
- Schedule lifecycle with specific start and end times.
Implementation Details:
Main Block ( main.tf
):
Defines the OCI Resource Scheduler logic:
resource "oci_resource_scheduler_schedule" "schedules" {
for_each = { for idx, schedule in var.schedules : idx => schedule }
compartment_id = var.oci_root_compartment_id
action = each.value.action
recurrence_type = each.value.recurrence_type
recurrence_details = each.value.recurrence_details
# Resource filters
dynamic "resource_filters" {
for_each = each.value.resource_filters
content {
attribute = resource_filters.value.attribute
value {
namespace = lookup(resource_filters.value.value, "namespace", null)
tag_key = lookup(resource_filters.value.value, "tag_key", null)
value = lookup(resource_filters.value.value, "value", null)
}
}
}
# Optional attributes
display_name = each.value.display_name
description = each.value.description
time_starts = each.value.time_starts
time_ends = each.value.time_ends
freeform_tags = each.value.freeform_tags
defined_tags = each.value.defined_tags
}
Variable Definitions ( variables.tf
):
Defines the input parameters:
variable "oci_root_compartment_id" {
description = "The OCID of the compartment where the schedules will be created."
type = string
}
variable "schedules" {
description = "List of schedule definitions."
type = list(object({
action = string # e.g., START, STOP
recurrence_type = string # e.g., ICAL, CRON
recurrence_details = string # RFC-5545 or CRON format string
resource_filters = list(object({ # Resource filters
attribute = string # e.g., DEFINED_TAGS, COMPARTMENT_ID
value = map(string) # Map for filter-specific keys (e.g., namespace, tag_key, value)
}))
display_name = optional(string, null) # Optional display name
description = optional(string, null) # Optional description
time_starts = optional(string, null) # RFC-3339 start time
time_ends = optional(string, null) # RFC-3339 end time
freeform_tags = optional(map(string)) # Optional free-form tags
defined_tags = optional(map(string)) # Optional defined tags
}))
}
Outputs ( outputs.tf
):
Outputs the created schedule IDs:
output "schedule_ids" {
description = "The OCIDs of the created Resource Schedulers."
value = [for s in oci_resource_scheduler_schedule.schedules : s.id]
}
Input Variables:
Name | Type | Description | Default |
oci_root_compartment_id |
string |
The OCID of the compartment where schedules are created. |
N/A |
schedules |
list(object({ action, |
A list of schedule definitions. |
N/A |
Resource Filters:
Supported Attributes:
-
DEFINED_TAGS
:- Filters resources based on defined tags.
- Requires
namespace
,tag_key
, andvalue
.
COMPARTMENT_ID
:- Filters resources in a specific compartment.
- Requires
value
(compartment ID).
LIFECYCLE_STATE
:- Filters resources based on lifecycle state.
- Requires
value
(e.g.,ACTIVE
orTERMINATED
).
Recurrence Details:
Specify a standard CRON
expression in recurrence_details
. Example:
recurrence_type = "CRON"
recurrence_details = "0 8 * * 1-5" # Every weekday at 8 AM
ICAL
, specify an RFC-5545 formatted string in recurrence_details
. Example:recurrence_type = "ICAL"
recurrence_details = "FREQ=WEEKLY;BYDAY=MO,TU,WE,TH,FR;BYHOUR=8;INTERVAL=1"
1. CRON Validation:
- Use crontab.guru to validate your
CRON
expressions.
2. Tag Requirements:
- Ensure defined tags and namespaces exist in your OCI tenancy before use.
3. Time Formats:
- Use RFC-3339 format for
time_starts
andtime_ends
.
Sandbox Implementation: OCI Resource Scheduling
This implementation focuses on automating resource management for OCI resources within the sandbox environment. This sample leverages theoci-resource-scheduler-module
to define schedules for starting and stopping resources based on CRON
expressions. The purpose is to optimize resource usage and reduce costs while maintaining operational readiness.
Implementation Details:
Main Block (main.tf
):
This file orchestrates the resource scheduler module to define start and stop schedules for sandbox resources.
module "scheduler" {
source = "../modules/oci-resource-scheduler-module"
schedules = local.sandbox_schedules
oci_root_compartment_id = "ocid1.compartment.oc1..exampleuniqueID"
}
or if you use remote custom modules:
module "scheduler" {
source = "git.yourdomain.org/project/oci-resource-scheduler-module/ociresourceschedulermodule"
version = "1.0.0"
schedules = local.sandbox_schedules
oci_root_compartment_id = "ocid1.compartment.oc1..exampleuniqueID"
}
scheduler.auto.tf
):
Defines the schedules for sandbox resources, including the specific actions, timing, and resource filters.
locals {
sandbox_schedules = [
{
action = "START_RESOURCE"
recurrence_type = "CRON"
recurrence_details = "0 8 * * 1-5" # Every weekday at 8 AM
resource_filters = [
{
attribute = "DEFINED_TAGS"
value = {
namespace = "FinOps-Tags"
tag_key = "scheduler"
value = "YES"
}
},
{
attribute = "COMPARTMENT_ID"
value = {
value = "ocid1.compartment.oc1..exampleuniqueID"
}
}
]
display_name = "Start Resources in Sandbox"
description = "Schedule to start resources in sandbox on weekdays at 8 AM"
time_starts = "2025-01-14T08:00:00Z"
time_ends = "2025-12-31T23:59:00Z"
defined_tags = {
"GitOps-Tags.environment" = "LAB"
}
},
{
action = "STOP_RESOURCE"
recurrence_type = "CRON"
recurrence_details = "0 20 * * 1-5" # Every weekday at 8 PM
resource_filters = [
{
attribute = "DEFINED_TAGS"
value = {
namespace = "FinOps-Tags"
tag_key = "scheduler"
value = "YES"
}
},
{
attribute = "COMPARTMENT_ID"
value = {
value = "ocid1.compartment.oc1..exampleuniqueID"
}
}
]
display_name = "Stop Resources in Sandbox"
description = "Schedule to stop resources in sandbox on weekdays at 8 PM"
time_starts = "2025-01-14T08:00:00Z"
time_ends = "2025-12-31T23:59:00Z"
defined_tags = {
"GitOps-Tags.environment" = "LAB"
}
}
]
}
Steps for Deployment:
- Initialize Terraform:
bash terraform init
- Plan the Deployment:
bash terraform plan
- Apply the Configuration:
bash terraform apply -auto-approve
- Verify Schedule Creation: Use the OCI Console or CLI to confirm the schedules are created successfully.
Conclusion
This article highlights the efficient use of the oci-resource-scheduler-module
to automate resource lifecycle management. By defining start and stop schedules, this approach ensures optimal resource utilization during working hours while minimizing unnecessary costs during downtime. This implementation serves as a template for other environments requiring similar automation.
To explore more about how cloud services can transform your business and enhance operational efficiency, visit our Cloud Services page. There, you’ll find detailed information about our solutions and how they can help optimize your infrastructure.
Authors: – Ivan Nagy – Guilherme Krulikowski
NEWSLETTER
SUBSCRIBE TO OUR NEWSLETTER
Sing up to receive our most recent use cases, blogs and insights.
PROOF OF SUCCESS
Check our
Blogs and News
Ritain.io: Behind the recognition – What makes us one of the best companies to work for
BLOGS AND INSIGHTS:RITAIN.IO: BEHIND THE RECOGNITION – WHAT MAKES US ONE OF THE BEST COMPANIES TO WORK FORRitain.io has proudly been named one of the Best Companies to Work For by EXAME magazine and ManpowerGroup Portugal. This recognition is a testament to our...
The role of cloud-native architecture in business monetization
BLOGS AND INSIGHTS:THE ROLE OF CLOUD-NATIVE ARCHITECTURE IN BUSINESS MONETIZATIONCloud-native architecture refers to a design approach that leverages the full potential of cloud technologies to build and run scalable, resilient, and flexible applications. By utilizing...