Automatically Start or Stop an OCI Compute Instance Using OCI Resource Scheduler
In cloud environments, compute instances are often created
for development, testing, learning, troubleshooting, and temporary workloads.
These instances may not be required to run all the time. If they continue
running when not needed, they can increase unnecessary compute usage.
Oracle Cloud Infrastructure (OCI) provides Resource
Scheduler, which helps automatically start and stop supported OCI resources
based on a schedule. This can help reduce the cost of compute and database
resources by stopping them when they are not required and restarting them when
they are needed again. In this article,
we will create an OCI Resource Scheduler schedule to automatically stop a
compute instance. We will then validate that the instance state changes based
on the configured schedule.
We will use below OCI services in this article :
OCI Compute
OCI Resource Scheduler
IAM Policy
Resource Scheduler creates an automated start/stop
scheduling function for supported resources across a tenancy based on the
schedules created.
Prerequisites
Before starting this activity, the following items should be
available:
OCI tenancy access
Required compartment
Running compute instance
Permission to create Resource Scheduler schedule
Permission for Resource Scheduler to manage the target compute instance
For this article, we will use an existing compute instance
and create a schedule to stop it automatically.
Resource Scheduler needs permission to perform actions on
the selected resources. If the required policy is not available, schedule
creation or execution may fail. The policy requirement depends on the target
resource and compartment.
A policy can be created based on the access model used in
the tenancy. Below policies are required for resource scheduler.
Allow <Group> to inspect resource-schedule in tenancy
Allow any-user to manage <resource_type (instance,
database, and others)> in compartment id <target_compartment_ocid>
where all
{request.principal.type='resourceschedule',request.principal.id='ocid_of_resourceschedule'}
First, review the compute instance that will be used for
testing.
From the OCI Console, navigate to:
Compute → Instances
Open the target compute instance and confirm that the
instance is in Running state.
Example instance name:
For our exercise we will configure Resource scheduler to
automatically stop this running instance.
Step 2: Open Resource Scheduler
From the OCI Console, navigate to Resource Scheduler.
Governance & Administration -> Resource scheduler
Open the Schedules page and select Create schedule.
Step 3: Create Schedule
Provide a meaningful schedule name.
Example:
stop-compute-instance-schedule
Add a description.
Example:
Automatically stop the demo compute instance for testing
Resource Scheduler.
Select the compartment where the schedule should be created.
Step 4: Select Schedule Action
In the schedule action section, choose the action that
should be performed.
For this article, select:
Action: Stop
Resource Type: Compute Instance
This tells Resource Scheduler to stop the selected compute
instance when the schedule runs.
If the requirement is to start a stopped instance, a
separate schedule can be created with the Start action.
Step 5: Select Target Compute Instance
In the target resource section, select the compute instance
that should be stopped.
Example target resource:
Validate that the correct compartment, region, and resource
are selected. You can see Autonomous database in the screenshot. This tenancy
autonomous database as well and it can he scheduled through Resource scheduler.
It is important to choose the correct instance because the
schedule will perform the selected action on the target resource.
The next step is about Apply Parameters.
That Apply parameters page is mainly used when the scheduled
resource/action needs an additional input payload. For Start or Stop an OCI
Compute Instance, normally no JSON body is required. Resource Scheduler can
start or stop the selected compute instance based on the action and resource
selected in the previous steps.
Step 6: Configure Schedule Time
Now configure the schedule time.
For quick validation, select a time a few minutes ahead of
the current time.
Example:
Current time: 5:43 PM
Schedule action: Stop
Schedule time: 5:46 PM
This allows us to validate the schedule execution quickly.
Depending on the console options, the schedule can be
configured as a one-time schedule or recurring schedule. For a blog validation,
a one-time schedule is easier to test and explain.
Step 7: Review and Create Schedule
Review the complete schedule configuration.
Validate the following details:
Schedule name
Action
Target compute instance
Schedule time
After confirming the details, create the schedule. The
schedule should appear in the Resource Scheduler schedules list.
Step 8: Wait for Schedule Execution
After the schedule is created, wait until the configured
schedule time is reached. Before the schedule execution, the compute instance
should still be in:
Running
After the schedule runs, Resource Scheduler should initiate
the stop operation.The instance state should change from: Running to Stopping and
then Stopped.
Step 9: Validate Compute Instance State
After the scheduled time passes, open the compute instance
details page again.
Validate that the instance state changed to:
Stopped
This confirms that Resource Scheduler successfully stopped
the compute instance automatically.
Step 10: Review Schedule Status
Open the Resource Scheduler schedule details page and review
the work requests page.
This helps confirm that the schedule was executed.
OCI Resource Scheduler provides a simple way to automate
start and stop operations for supported resources. This is useful for
development, testing, and lab environments where compute instances do not need
to run continuously. It helps reduce manual effort and supports basic cost
optimization without custom scripts or external automation tools.
No comments:
Post a Comment