Terraform to create multiple Resource Schedule in Oracle Cloud

Karthic
2 min readJul 30, 2024

--

Resource scheduler in Oracle Cloud Infrastructure(OCI) is used to start /stop resources in OCI . This is very helpful to save cost by stopping the resources which are not in use based on schedule .

Like any other service you can use OCI console to create the resources. If you have a requirement to create or manage multiple resource schedule you can use the API/Terraform for the task.

In this blog we will see an example using Terraform to create a static scheduler and a scheduler based on dynamic values.

terraform {
required_providers {
oci = {
source = "oracle/oci"
version = ">= 5.0.0"
}
}
required_version = ">= 1.0.0"
}

resource "oci_resource_scheduler_schedule" "schedule" {
for_each = var.schedules

action = each.value.action
compartment_id = each.value.compartment_id
recurrence_details = each.value.recurrence_details
recurrence_type = each.value.recurrence_type

defined_tags = each.value.defined_tags
description = each.value.description
display_name = each.value.display_name
freeform_tags = each.value.freeform_tags

dynamic "resource_filters" {
for_each = each.value.resource_filters == null ? [] : each.value.resource_filters
content {

attribute = resource_filters.value.attribute
condition = resource_filters.value.condition
should_include_child_compartments = resource_filters.value.should_include_child_compartments

dynamic "value" {
for_each = resource_filters.value.values
content {
namespace = value.value.namespace
tag_key = value.value.tag_key
value = value.value.value
}
}
}
}

dynamic "resources" {
for_each = each.value.resources == null ? [] : each.value.resources
content {
id = resources.value.id
metadata = resources.value.metadata
}
}

time_ends = each.value.time_ends
time_starts = each.value.time_starts
}

Create a variables.tf file

variable "schedules" {
type = map(object({
action = string
compartment_id = string
recurrence_details = string
recurrence_type = string


defined_tags = optional(map(string))
description = optional(string)
display_name = string
freeform_tags = optional(map(string))

resource_filters = optional(list(object({
attribute = optional(string)

condition = optional(string)
should_include_child_compartments = optional(bool)

values = list(object({
namespace = optional(string)
tag_key = optional(string)
value = optional(string)
}))
})))

resources = optional(list(object({
id = string
metadata = optional(map(string))
})))

time_ends = string
time_starts = string
}))
}

Create a file named terraform.tfvars

schedules = {
schedule1 = {
action = "START_RESOURCE"
compartment_id = "<tenancy_ocid>"
recurrence_details = "30 19 * * *"
recurrence_type = "CRON"
description = "creating schedule via tf"

display_name = "tf_dynamic_schedule"

resource_filters = [
{
attribute = "LIFECYCLE_STATE"
values = [
{
value = "RUNNING"
}
]
},
{
attribute = "RESOURCE_TYPE"
values = [
{
value = "Instance"
}
]
}
]

time_ends = "2024-07-31T20:00:00Z"
time_starts = "2024-07-30T19:00:00Z"
}
schedule2 = {
action = "STOP_RESOURCE"
compartment_id = "<tenancy_ocid>"
recurrence_details = "30 19 * * *"
recurrence_type = "CRON"
description = "creating schedule via tf"

display_name = "tf_static_schedule"

resources = [
{
id = "<resource_ocid>"
metadata = null
}
]

time_ends = "2024-07-31T20:00:00Z"
time_starts = "2024-07-30T19:00:00Z"
}

}

You need to create the provider.tf file as well based on the authentication you use.

The code is also available in github to download. Let me know if you face any issues.

--

--

No responses yet