Automated Start and Stop Oracle Integration instance using OCI Scheduling Functions

Lavanya Siliveri
4 min readNov 8, 2024

--

In this post, I’d like to discuss how we can leverage OCI Scheduling Functions feature to automate the start and stop a given Oracle Integration instance.

Scheduling Functions is a relatively new service that has been introduced recently in OCI, it is enabled by the OCI Resource Scheduler service.

For a long time, customers have been requesting to have an automated way to stop and start their non prod Oracle Integration instances on a schedule which will help them limit the usage of the instance when not required.

In order to achieve this, I have created a function called startstopOICInstance, which does not accept any parameters.

Note that to use Resource Scheduler, you must have been granted permission to manage resource schedules in the tenancy. Note also that before the function can be invoked according to the resource schedule, you have to create a dynamic group that includes the resource schedule, and a policy statement that grants the dynamic group access to the function. For more information, see Creating Schedules.

I am using OCI Vault Secret to store the OCID of my Oracle Integration instance. I am hard-coding the Secret OCID in my python. With the OCI Vault Secret OCID, I can keep updating its value with the OCID of my OIC instances, without having to write separate functions for each of them.

The excerpt code of the function looks like below. The basic premise is to check the status of the instance before either starting or stopping it. The function is invoked on the scheduled time as per the configuration.


def get_integrationinstance_client():
try:
signer = oci.auth.signers.get_resource_principals_signer()
return oci.integration.IntegrationInstanceClient(config={}, signer=signer)
except Exception as e:
config = oci.config.from_file("~/.oci/config","<REGION-ID>")
return oci.integration.IntegrationInstanceClient(config)

def get_secrets_client():
try:
signer = oci.auth.signers.get_resource_principals_signer()
return oci.secrets.SecretsClient(config={}, signer=signer)
except Exception as e:
config = oci.config.from_file("~/.oci/config","<REGION-ID>")
return oci.secrets.SecretsClient(config)

def get_secret_value(secret_id):
try:
secrets_client = get_secrets_client()
secret_bundle = secrets_client.get_secret_bundle(secret_id)
secret_content = base64.b64decode(secret_bundle.data.secret_bundle_content.content)
return secret_content.decode('utf-8')
except Exception as e:
logger.error(f"Failed to retrieve secret value: {e}")
return None

def get_instance_status(ocid):
try:
integration_instance_client = get_integrationinstance_client()
integration_instance = integration_instance_client.get_integration_instance(ocid)
return integration_instance.data.lifecycle_state
except Exception as e:
logger.error(f"Failed to get instance status: {e}")
return None

def start_instance(ocid):
try:
integration_instance_client = get_integrationinstance_client()
response = integration_instance_client.start_integration_instance(ocid)
return response
except Exception as e:
logger.error(f"Failed to start instance: {e}")
return None

def stop_instance(ocid):
try:
integration_instance_client = get_integrationinstance_client()
response = integration_instance_client.stop_integration_instance(ocid)
return response
except Exception as e:
logger.error(f"Failed to stop instance: {e}")
return None

def startstopOICInstance():
secret_id = "ocid1.vaultsecret.oc1.phx.amaaaaaady<secret-ocid>"
ocid = get_secret_value(secret_id)

if not ocid:
outcome = "Failed to retrieve the OCID from the vault secret."
logger.error(outcome)
return outcome

logger.info("Checking instance status...")
status = get_instance_status(ocid)

if status is None:
outcome = "Failed to get instance status."
logger.error(outcome)
return outcome

logger.info(f"Current instance status: {status}")

if status == "ACTIVE":
if stop_instance(ocid) is None:
return "Failed to stop instance."

outcome = "Instance will be stopped in few mins"
logger.info(outcome)
return outcome
elif status == "INACTIVE":
if start_instance(ocid) is None:
return "Failed to start instance."

outcome = "Instance will be started in few mins"
logger.info(outcome)
return outcome
else:
outcome = f"Instance is in {status} state. No action taken."
logger.info(outcome)
return outcome

Navigate to Resource Scheduler to create the Schedule.

I am creating a schedule to Start the Instance every week day at morning 8am and another schedule to Stop the instance every week day at evening 5pm.

Once created, the schedules are enabled by default and you should start seeing the function getting executed at configured times.

With this Scheduling Functions, we can automate execution of multitude of OCI services, I shall discuss one of such service in my next post.

If you’ve found this useful, please share, If you have any questions, please reach out to me on LinkedIn

--

--

No responses yet