SDK Refers to a set of software tools used to create software that allow us to manage a specific platform. These tools can include: libraries, processes, documentation, etc.
The OCI python SDK allows us to write code to manage resources in Oracle cloud
You can download the SDK from:
The easiest way is using the pip install, by executing:
pip install oci
Also, remember to add this line to your python code, so you can be able to use the SDK
>>>import oci
If you have follow until here, you may encounter an error, when trying to use any command on the SDK, and why is that? - well, in order to connect to OCI resources, you need:
- An OCI Account
- A user created in that account, in a group with a policy that grants manage permissions or lower. Examples on policies can be found here
- Configure OCI CLI profile on your local computer
I have some good news, you can create a free account in Oracle Cloud and get 300USD credits to test resources. Instructions HERE
Second, configure the CLI is easy you just need to (This is for MAC):
#Update brew and install cli
brew update && brew install oci-cli
#verify oci installation
oci --version
Take a look into the Homebrew documentation
Before using the CLI, you need to configure the config file that will contain the required credentials and information for working with Oracle Cloud Infrastructure. By default this file is stored in : ~/.oci/config
but you can change it.
So, in order to generate the config file you need to:
#Move to your home directory and create the .oci folder
mkdir .oci
#move to the folder and create the config file
cd .oci
touch config
Now, you can vi this file and enter something similar to this:
[ADMIN_USER]
user=ocid1.user.oc1..<unique_ID>
fingerprint=<your_fingerprint>
key_file=keys/admin_key.pem
tenancy = ocid1.tenancy.oc1..<unique_ID>
region = us-phoenix-1
- [ADMIN_USER] > you can name this anything youu want, but remember the name as you will use it with Python sdk.
- user > here you need to enter the user ocid for the IAM user you created at the beginning. OCID is the unique resource identifier that Oracle cloud infrastructure provide to each resource
- fingerprint > refers to the fingerprint of the public key you configure to your user. All the relevant information related to his can be found here
-
key_file > the .pem file you generated. You should use the complete path if your keys are located in a different directory
/Users/elopez/.ssh/admin_key.pem
. Detail information for How to Generate an API Signing Key - tenancy > your tenancy OCID. Details on how to obtain my tenancy OCID
- region > the region that you are subscribed to (region identifier), Regions and Availability Domains
NOW, WE can start testing some stuff with python SDK.
First you need to establish the connection with OCI, and provide your software with the credentials that will be using.
>>> config = oci.config.from_file(
... "~/.oci/config",
... "ADMIN_USER")
Another approach could be to store this information in a .env file (that you should include into your .gitignore)
and reference the file into your code
import oci
import os
config = oci.config.from_file(os.environ.get("CONFIG_PATH"), os.environ.get("OCI_PROFILE"))
and your .env file can look something like this:
CONFIG_PATH = "~/.oci/config"
OCI_PROFILE = "ADMIN_USER"
This is the minimum required to connect with OCI, and will help you establish connection with other services as for example compute:
# Initialize compute client with default config file
compute_client = oci.core.ComputeClient(config)
Or for the monitoring service
# Initialize compute client with default config file
monitoring_client = oci.monitoring.MonitoringClient(config)
And now, you can use this to get, for example, a list of compute instances inside a container:
compartment_id_selected = os.environ.get("COMPARTMENT_ID")
list_instances_response = compute_client.list_instances(compartment_id=compartment_id_selected, sort_order="DESC", lifecycle_state="RUNNING")
These are the basic steps to start working with OCI python SDK.
Additional Resources
More about OCI Containers
My personal GITHUB OCI REPO
Oracle has develop a complete API reference and also it helps you by providing code examples:
- ORACLE SDK Reference. Look for the API Reference on the left pane menu. Examples can be seen like this one:
# URL > https://docs.oracle.com/en-us/iaas/tools/python-sdk-examples/2.53.1/core/update_instance_configuration.py.html
# This is an automatically generated code sample.
# To make this code sample work in your Oracle Cloud tenancy,
# please replace the values for any parameters whose current values do not fit
# your use case (such as resource IDs, strings containing ‘EXAMPLE’ or ‘unique_id’, and
# boolean, number, and enum parameters with values not fitting your use case).
import oci
# Create a default config using DEFAULT profile in default location
# Refer to
# https://docs.cloud.oracle.com/en-us/iaas/Content/API/Concepts/sdkconfig.htm#SDK_and_CLI_Configuration_File
# for more info
config = oci.config.from_file()
# Initialize service client with default config file
core_client = oci.core.ComputeManagementClient(config)
# Send the request to service, some parameters are not required, see API
# doc for more info
update_instance_configuration_response = core_client.update_instance_configuration(
instance_configuration_id="ocid1.test.oc1..<unique_ID>EXAMPLE-instanceConfigurationId-Value",
update_instance_configuration_details=oci.core.models.UpdateInstanceConfigurationDetails(
defined_tags={
'EXAMPLE_KEY_4bccp': {
'EXAMPLE_KEY_l4nah': 'EXAMPLE--Value'}},
display_name="EXAMPLE-displayName-Value",
freeform_tags={
'EXAMPLE_KEY_s14GL': 'EXAMPLE_VALUE_ZZgDFtoA0GvgolAJlyPw'}),
opc_retry_token="EXAMPLE-opcRetryToken-Value",
if_match="EXAMPLE-ifMatch-Value")
# Get the data from response
print(update_instance_configuration_response.data)
Top comments (0)