Setting up the OCI Configuration File using API Keys
Tags: oracle, cloud, devops
Published: 2023-06-19 17:28:57

To manage resources on the Oracle Cloud Infrastructure (OCI) remotely--e.g., using Terraform or the OCI Command Line Interface (CLI) tool--you first need to have an OCI CLI configuration file that contains the credentials (profile) of the user account that you will be using to create and deploy functions. This guide tells you how to do so using API key authentication.

## Setting up OCI Configuration profile

### 1. Create the `~/.oci` directory
Create a new directory `~/.oci` where you will keep your OCI API credentials

### 2. Create an API Key
Create your API key on the Oracle Cloud console.

Go to your **Profile picture** > **My Profile** > **API keys** > **Add API key**

![Screenshot - API keys on Profile page](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cow2hrxq9a3llpbim1fl.png)

Make sure to download the private key. The public key is optional to download. 

The public key can be necessary if you want to use the same private key to access another resource but we'll leave it out here.

![Add API key page](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/70ea88ef0zudx1k67xoy.png)

Click **Add**. This configuration file preview will show up.

![Configuration file preview for OCI API key](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6dws6ely6wzlerkfs2zi.png)

### 3. Put API key into the `config` file
Copy the results and paste into your `~/.oci/config` file. Create the file if it doesn't exist.

![config file preview](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/27tpesxviu75kdrb11h8.png)

where:
- `key_file` here means the location of the private key you downloaded. I put my key inside the `~/.oci` directory

Alternatively, this step can also be completed using `oci setup config` if you have the CLI tool already installed. For more info, see [An introduction to OCI CLI](https://dev.to/farisdurrani/an-introduction-to-oci-cli-pop)

## Bonus: Multiple profiles

If you have multiple tenancies (or multiple users in a same tenancy), you can have multiple profiles and use the one you need in every command.

For that, repeat the same steps above to create a new API key and add the copied configuration to your `config` file but with a different profile name other than `[DEFAULT]`:

![Multiple profiles in OCI CLI](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rcparcyc3rlu1wnveqy2.png)

Note: notice how the two fingerprints and key_file values are the same? That's because I pasted in the public key used to generate the first API key. So now, these two profiles use the same private key. This is optional, of course.

## Verify

This guide isn't extensive enough to cover tools that uses the API, but you may view my two articles here that uses this configuration step:

1. [An introduction to OCI CLI](https://dev.to/farisdurrani/an-introduction-to-oci-cli-pop)
2. [Using Terraform to Deploy an OCI Compute Instance
](https://dev.to/farisdurrani/using-terraform-to-deploy-an-oci-compute-instance-597f)

---

***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/).*