terminate oci instance using instance_action
I am trying to find the best way to terminate an oci instance using the oci python library. I know that I can stop an instance with
base_compute.instance_action(instance_id, 'STOP')
However, I do not see a TERMINATE
action in their documentation:https://docs.oracle.com/en-us/iaas/tools/oci-cli/2.21.5/oci_cli_docs/cmdref/compute/instance/action.html for the method. Any help with this would be great.
Solution 1:
Please refer this document, it has the Oracle CLI command to terminate the instance.
Command: oci compute instance terminate [OPTIONS]
Solution 2:
You can terminate an instance by calling either compute_client.terminate_instance
or compute_client_composite_operations.terminate_instance_and_wait_for_state
if you want your code wait for it to finish.
Here's an example from the Python SDK:
def terminate_instance(compute_client_composite_operations, instance):
print('Terminating Instance: {}'.format(instance.id))
compute_client_composite_operations.terminate_instance_and_wait_for_state(
instance.id,
wait_for_states=[oci.core.models.Instance.LIFECYCLE_STATE_TERMINATED]
)
print('Terminated Instance: {}'.format(instance.id))
print()