Getting started with the OCI Python SDK πŸ

I’ve been working with OCI recently and have just started to write some Python scripts that use the OCI SDK. I thought I’d document the process of getting everything setup – mainly because I’ll probably forget how I did this and have to re-learn at some point in the future πŸ˜‚.

This guide was written for Windows 11, however with minor adaptions should work with any OS that supports the OCI SDK for Python.

Step 1 – Install the OCI Python SDK

Run the following command to install the OCI Python SDK.

pip install oci

Step 2 – Create an API Signing Key

The process for this is fully documented within How to Generate an API Signing Key from the Console

Step 3 – Connect to OCI with Python using the API Signing Key

Now that the OCI SDK for Python is installed and an API Signing Key has been created, you can connect to OCI using the following:

import oci

# Reads the config created when the API Signing Key was generated, which should be stored within \.oci\config
config = oci.config.from_file()

Once you’ve done this, you can then use the Python SDK as you wish, in the example below I list all of the compute instances within my root compartment.

# Create client with the default config file (\.oci\config)
computeclient = oci.core.ComputeClient(config)

# List all of the compute instances within the compartment
list_instances_response = computeclient.list_instances(
    compartment_id="Replace with the OCID of the compartment to query")

# List all of the compute instances
print(list_instances_response.data)

Here is the script in action, it lists (a lot of) data about each of the instances found within the compartment queried.

Comments

One response to “Getting started with the OCI Python SDK πŸ”

  1. Automate stopping compute instances in OCI πŸ›‘ – Brendan's Tech Ramblings Avatar

    […] If you haven’t used the OCI Python SDK before, I put together a quick guide on getting started here. […]

    Like

Leave a comment