An introduction to OCI CLI
Tags: oracle, cloud, devops
Published: 2023-06-15 02:07:51

_Using Oracle Cloud's CLI to manage OCI resources_

You probably are aware of the Oracle Cloud console, which is the online web interface on cloud.oracle.com that lets you create and manage resources on the Oracle Cloud Infrastructure through the GUI. 

But let's say we want a standardized, predictable way of managing those resources. You know precisely the CPU and memory capacity of a Compute Instance that you want to deploy, along with its other details, and you want to describe that deployment to someone else. 

Meet the OCI command line interface (CLI).

![Screenshot - OCI CLI in action querying all regions](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/be5deszupqk9nclwvwor.png)

## What is the CLI?
The CLI is a small-footprint tool that you can use on its own or with the Console to complete Oracle Cloud Infrastructure tasks. The CLI provides the same core functionality as the Console, plus additional commands. Some of these, such as the ability to run scripts, extend Console functionality. [(Oracle)](https://docs.oracle.com/en-us/iaas/Content/API/Concepts/cliconcepts.htm)

## Installing the CLI

These steps are specific to Mac. If you're on another OS, please check out the [official Quickstart](https://docs.oracle.com/en-us/iaas/Content/API/SDKDocs/cliinstall.htm).


### 1. Installing using Homebrew
On Mac, you can install the CLI using [Homebrew](https://brew.sh/):
```sh
brew install oci-cli
```

Verify installation using `oci -v`.

### 2. Set up the OCI Configuration using API Keys

Follow the steps in [Setting up the OCI Configuration File using API Keys](https://dev.to/farisdurrani/setting-up-the-oci-configuration-file-using-api-keys-96c)

### 3. Verify

Try:

```sh
oci iam compartment list
```

to show a list of all your compartments in your tenancy:

![A list of all compartments through OCI CLI](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f5g53yc5cq2jjgi61759.png)

And now you have the power of OCI CLI! If you need to find a command, simply Google it, e.g., `oci cli create compartment`.

## More ways to use the CLI

### Interactive mode

Use `oci -i` to use the interactive mode if you don't want to Google the right command. The tool will show all the possible keywords following the command

![Example of using interactive mode](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j0eniiqnyphq10j9d6pc.png)



## Generate JSON parameter inputs

Sometimes the input to the CLI can be a complex JSON type and you want to know the structure of the input. Use `--generate-param-json-input` to see the structure.

Or `--generate-full-command-json-input` to see all attributes' formats:

![Examples using generate json inputs](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i7ni5n2hurxrtd65iisj.png)


### Multiple profiles

If you have multiple profiles, you can select the profile you want to use using `--profile MYPROFILE`. 

Here, we are [creating a new compartment](https://docs.oracle.com/en-us/iaas/tools/oci-cli/3.29.0/oci_cli_docs/cmdref/iam/compartment/create.html):

```sh
oci iam compartment create --compartment-id ocid1.compartment.oc1..aaaaaaaakbr7t5yqjirrmwm3yourparentocidhereeejfnkrebnklnfefer --name Compartment2 --description "new compartment" --profile PROFILE2
```

![Creating a new compartment on a different profile](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0jkd0yaxb5rhg3eefzun.png)

New compartment:

![New compartment created](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ouwo8q9o0ztzzz3eyjsa.png)

If you leave out `--profile PROFILE2`, it will default to the `[DEFAULT]` profile.

### Using a JSON file input

To [create a new Identity Domain](https://docs.oracle.com/en-us/iaas/tools/oci-cli/3.29.0/oci_cli_docs/cmdref/iam/domain/create.html), let's create a new JSON file called `myFile.json`:

```json
{
	"compartment_id": "ocid1.tenancy.oc1..aaaaaaaashf677bcolbea4yblgu5jgyourcompartmentocidherefendskjjkewndsjkn",
	"description": "test identity domain",
	"display_name": "cli-id-december",
	"home_region" : "us-ashburn-1",
	"license_type" : "free"
}
```

Run

```sh
oci iam domain create --from-json file://myFile.json
```

### Destroy a resource

Besides creating, the CLI can destroy deployed resources as well. For example:

```sh
oci iam compartment delete -c "ocid1.compartment.oc1..aaaaaaaagefmzdurtphobehgivez36nr3rq55wupta4bl4ljygcvaovxspia" --profile PROFILE2
```

![Deleting a compartment using CLI](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1fggpk693h99hg942oo1.png)

---

## More resources:
1. [Quickstart -- Installing the CLI](https://docs.oracle.com/en-us/iaas/Content/API/SDKDocs/cliinstall.htm)
2. [Oracle Cloud Infrastructure CLI Command Reference](https://docs.oracle.com/en-us/iaas/tools/oci-cli/3.29.0/oci_cli_docs/index.html)

***Safe harbor statement***
The information provided on this channel/article/story is solely intended for informational purposes and cannot be used as a part of any contractual agreement. The content does not guarantee the delivery of any material, code, or functionality, and should not be the sole basis for making purchasing decisions. The postings on this site are my own and do not necessarily reflect the views or work of Oracle or Mythics, LLC.

*This work is licensed under a [Creative Commons Attribution 4.0 International License](http://creativecommons.org/licenses/by/4.0/).*