Oracle Cloud Infrastructure CLI
The Oracle Cloud Infrastructure CLI (oci-cli) is a command-line interface that allows users to interact with Oracle Cloud Infrastructure services. Built on the OCI Python SDK, it provides equivalent functionality to the OCI Console, plus additional commands and scripting capabilities. The current version is 3.78.0, and it is actively maintained by Oracle with frequent updates and new feature releases.
Warnings
- breaking Breaking changes occur frequently in new releases, often involving parameter renames, commands being moved, or parameters becoming required. Always review the GitHub releases page or `CHANGELOG.rst` before upgrading major or minor versions.
- gotcha The `--defaults-file` CLI option is deprecated. Use `--cli-rc-file` instead for specifying CLI-specific configuration files.
- breaking OCI Data Science is deprecating Oracle Identity Cloud Service (IDCS) authentication for ML Applications. After April 11, 2026, deployments using IDCS-based authentication will not be supported, and new ML application instances cannot be created using IDCS.
- gotcha The OCI CLI (and SDK) requires a properly configured `~/.oci/config` file and corresponding API key pair for authentication. Incorrect setup or permissions is a common source of errors.
- gotcha When querying JSON output with `--query`, the argument uses JMESPath syntax. This is powerful but can be unfamiliar and requires understanding JMESPath expressions for effective filtering and transformation. Using `--filter` provides SCIM-based filtering *before* `--query` is applied.
Install
-
pip install oci-cli -
bash -c "$(curl -L https://raw.githubusercontent.com/oracle/oci-cli/master/scripts/install/install.sh)"
Imports
- oci
import oci
Quickstart
# First, ensure your OCI configuration file (~/.oci/config) is set up.
# You can generate one interactively using: oci setup config
#
# Then, use the OCI Python SDK (installed with oci-cli):
import oci
import os
# Load the OCI config from the default location and profile
# (or specify path and profile like oci.config.from_file("/path/to/config", "YOUR_PROFILE"))
try:
config = oci.config.from_file(os.environ.get('OCI_CONFIG_FILE', '~/.oci/config'), os.environ.get('OCI_PROFILE', 'DEFAULT'))
except oci.exceptions.ConfigFileNotFound as e:
print(f"Error: OCI config file not found. Please run 'oci setup config' or set OCI_CONFIG_FILE and OCI_PROFILE environment variables. {e}")
exit(1)
# Create an IdentityClient
identity_client = oci.identity.IdentityClient(config)
# Get the tenancy OCID from the config
tenancy_id = config["tenancy"]
# List compartments in the tenancy
try:
list_compartments_response = identity_client.list_compartments(compartment_id=tenancy_id, compartment_id_in_subtree=True)
print("Listing Compartments:")
for compartment in list_compartments_response.data:
print(f" Name: {compartment.name}, ID: {compartment.id}, Lifecycle State: {compartment.lifecycle_state}")
except oci.exceptions.ServiceError as e:
print(f"Error listing compartments: {e}")
if e.code == 'NotAuthenticated':
print("Please check your API key and fingerprint in ~/.oci/config.")
elif e.code == 'AuthRequired':
print("Authentication failed. Ensure your API key is correctly configured and has necessary permissions.")