<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Blaine Carter</title>
    <description>The latest articles on DEV Community by Blaine Carter (@orablaineos).</description>
    <link>https://dev.to/orablaineos</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F409663%2F9ab391dd-6c55-4ff1-8953-de108b3b60bb.jpg</url>
      <title>DEV Community: Blaine Carter</title>
      <link>https://dev.to/orablaineos</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/orablaineos"/>
    <language>en</language>
    <item>
      <title>Create and use an Oracle Autonomous Cloud Database from the Command Line</title>
      <dc:creator>Blaine Carter</dc:creator>
      <pubDate>Wed, 24 Jun 2020 13:28:00 +0000</pubDate>
      <link>https://dev.to/orablaineos/create-and-use-an-oracle-autonomous-cloud-database-from-the-command-line-jm0</link>
      <guid>https://dev.to/orablaineos/create-and-use-an-oracle-autonomous-cloud-database-from-the-command-line-jm0</guid>
      <description>&lt;p&gt;In this post I’ll cover how to create an Oracle Autonomous Cloud Database download the Wallet credentials and connect to the database all from the command line using the OCI-CLI.&lt;/p&gt;

&lt;p&gt;After that I'll include an example of a shell script that I use to setup my Demo environment.&lt;/p&gt;

&lt;h3&gt;Prerequisites&lt;/h3&gt;

&lt;ul&gt;
    &lt;li&gt;&lt;a href="https://learncodeshare.net/2020/03/06/install-and-configure-the-oracle-cloud-command-line-interface/" rel="noopener noreferrer"&gt;OCI-CLI installed and configured&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;
&lt;a href="https://www.oracle.com/database/technologies/appdev/sqlcl.html" rel="noopener noreferrer"&gt;SQLcl&lt;/a&gt; installed&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id="GetCompartment"&gt;Get the Compartment OCID&lt;/h3&gt;

&lt;p&gt;I like to keep all of my work compartmentalized so that I don't run into conflicts between my (and potentially other people's) projects.  This means I'll need to get the OCID of the compartment I want to work in.&lt;/p&gt;

&lt;p&gt;Rather than use the Web Console you can run this command to get a list of your available compartments.&lt;/p&gt;

&lt;pre class="lang:sh decode:true"&gt;oci iam compartment list&lt;/pre&gt;

&lt;p&gt;Assuming that you already know which compartment you want to work with you can use the &lt;span class="lang:default decode:true crayon-inline "&gt;--query&lt;/span&gt;  parameter to retrieve the ID of that compartment.&lt;/p&gt;

&lt;p&gt;The above command returns an array called data that I will use to query an object.&lt;/p&gt;

&lt;pre class="lang:sh decode:true "&gt;--query "data[]"&lt;/pre&gt;

&lt;p&gt;I'd like to retrieve only the object with a name of 'Demo'.&lt;/p&gt;

&lt;pre class="lang:sh decode:true"&gt;--query "data[?name=='Demo']"&lt;/pre&gt;

&lt;pre class="lang:sh decode:true"&gt;[
  {
    "compartment-id": "ocid1.tenancy.oc1..aaaaaaaauqbiglongguid",
    "defined-tags": {},
    "description": "For Demonstrations",
    "freeform-tags": {},
    "id": "ocid1.compartment.oc1..aaaaaaaa7a6biglongguid",
    "inactive-status": null,
    "is-accessible": null,
    "lifecycle-state": "ACTIVE",
    "name": "Demo",
    "time-created": "2019-09-26T21:14:01.870000+00:00"
  }
]&lt;/pre&gt;

&lt;p&gt;Now that I have the full object, I can get the id value.&lt;/p&gt;

&lt;pre class="lang:default decode:true"&gt;--query "data[?name=='Demo'].id"&lt;/pre&gt;

&lt;pre class="lang:sh decode:true"&gt;[
"ocid1.compartment.oc1..aaaaaaaa7a6biglongguid"
]&lt;/pre&gt;

&lt;p&gt;The list command will return all objects that match the query criteria in an array.  Even when there is only a single object it will be returned in an array.&lt;/p&gt;

&lt;p&gt;Next, I pipe out the first (and only) value from the array.&lt;/p&gt;

&lt;pre class="lang:sh decode:true"&gt;--query "data[?name=='Demo'].id | [0]"&lt;/pre&gt;

&lt;pre class="lang:sh decode:true"&gt;"ocid1.compartment.oc1..aaaaaaaa7a6biglongguid"&lt;/pre&gt;

&lt;p&gt;Using the &lt;span class="lang:default decode:true crayon-inline "&gt;--raw-output&lt;/span&gt;  parameter, I can get the raw value without the double quotes.&lt;/p&gt;

&lt;pre class="lang:sh decode:true "&gt;oci iam compartment list --query "data[?name=='Demo'].id | [0]" --raw-output&lt;/pre&gt;

&lt;pre class="lang:sh decode:true "&gt;ocid1.compartment.oc1..aaaaaaaa7a6biglongguid&lt;/pre&gt;

&lt;p&gt;I can use this command to set an environment variable.&lt;/p&gt;

&lt;pre class="lang:sh decode:true"&gt;export COMPARTMENT_ID=$(oci iam compartment list --query "data[?name=='Demo'].id | [0]" --raw-output)&lt;/pre&gt;

&lt;p&gt;The OCI-CLI query parameter uses the &lt;a href="https://jmespath.org/" rel="noopener noreferrer"&gt;JMESPath query language&lt;/a&gt;.&lt;/p&gt;

&lt;h5&gt;Create Compartment&lt;/h5&gt;

&lt;p&gt;If the compartment doesn't exist you can use OCI to create one.  For this command you will need the OCID of an existing compartment.  This will be the parent compartment.&lt;/p&gt;

&lt;p&gt;If you want to use an existing compartment as a parent, you can use the above command to get the OCID.  Or, if you want to add the new compartment to the ROOT compartment, you can use the Tenancy OCID.&lt;/p&gt;

&lt;p&gt;You can get the Tenancy OCID from:&lt;/p&gt;

&lt;ul&gt;
    &lt;li&gt;Your OCI Config file &lt;span class="lang:sh decode:true crayon-inline"&gt;cat ~/.oci/config&lt;/span&gt;.&lt;/li&gt;
    &lt;li&gt;The OCID of an existing compartments parent.
This time I'm using the &lt;span class="lang:default decode:true crayon-inline "&gt;?contains()&lt;/span&gt;  function to check compartment-id for the string '.tenancy.'.  Notice that the compartment-id must be double quoted because it contains a '-' and those double quotes are escaped.  &lt;span class="lang:sh decode:true crayon-inline"&gt;\"compartment-id\"&lt;/span&gt;
&lt;span class="lang:sh decode:true crayon-inline"&gt;oci iam compartment list --query "data[?contains(\"compartment-id\",'.tenancy.')].\"compartment-id\" | [0]" --raw-output&lt;/span&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Once you have the parent compartment OCID the command to create a new compartment is:&lt;/p&gt;

&lt;pre class="lang:sh decode:true"&gt;oci iam compartment create \
   --compartment-id ocid1.tenancy.oc1..aaaaaaaauqbiglongguid \
   --name "Demo" \
   --description "For Demonstrations"&lt;/pre&gt;

&lt;h3&gt;Database&lt;/h3&gt;

&lt;p&gt;I can check to see if the database already exists by using a query similar to the one I used for compartments.&lt;/p&gt;

&lt;pre class="lang:default decode:true"&gt;export DB_ID=$(oci db autonomous-database list -c $COMPARTMENT_ID --query "data[?\"db-name\"=='demo'].id | [0]" --raw-output)&lt;/pre&gt;

&lt;p&gt;If the demo database doesn't exist I can create a new Always-Free Autonomous Cloud Database with the OCI-CLI.&lt;/p&gt;

&lt;p&gt;The data-storage-size-in-tbs is set to 1TB which is larger than the free tier supports.  Setting &lt;span class="lang:default decode:true crayon-inline "&gt;--is-free-tier True&lt;/span&gt;  will cause the system to automatically scale it to the correct size.&lt;/p&gt;

&lt;p&gt;The default value for 'is-free-tier' is False, if you do not include this parameter you will create a standard Autonomous Cloud Database.  You should check the &lt;a href="https://www.oracle.com/cloud/pricing.html" rel="noopener noreferrer"&gt;Cost Estimator&lt;/a&gt; to ensure that you're OK with the cost.&lt;/p&gt;

&lt;p&gt;Setting &lt;span class="lang:default decode:true crayon-inline "&gt;--db-workload "OLTP"&lt;/span&gt;  will create an Autonomous Transaction Processing database, using "DW" will create a Data Warehouse.&lt;/p&gt;

&lt;p&gt;Make sure you use a strong &lt;span class="lang:default decode:true crayon-inline "&gt;--admin-password&lt;/span&gt; , this will be the admin password for the new database.&lt;/p&gt;



&lt;pre class="lang:sh decode:true"&gt;oci db autonomous-database create \
    --compartment-id $COMPARTMENT_ID \
    --cpu-core-count 1 \
    --data-storage-size-in-tbs 1 \
    --db-name "$DB_NAME" \
    --display-name "$DB_DISPLAY_NAME" \
    --db-workload "OLTP" \
    --admin-password "T3stertester" \
    --is-free-tier True&lt;/pre&gt;

&lt;p&gt;This command will return a JSON object with the properties of the new database.&lt;/p&gt;

&lt;p&gt;I'll add the query and raw-output parameters to extract the ID and assign it to an environment variable.&lt;/p&gt;

&lt;pre class="lang:sh decode:true"&gt;export DB_ID=$(oci db autonomous-database create \
    --compartment-id $COMPARTMENT_ID \
    --cpu-core-count 1 \
    --data-storage-size-in-tbs 1 \
    --db-name "$DB_NAME" \
    --display-name "$DB_DISPLAY_NAME" \
    --db-workload "OLTP" \
    --admin-password "T3stertester" \
    --is-free-tier  True \
    --query "data.id" --raw-output)&lt;/pre&gt;

&lt;p&gt;In order to connect to my new database I will need to&lt;/p&gt;

&lt;h3&gt;Download the Wallet&lt;/h3&gt;

&lt;p&gt;Wait for your database to be in an AVAILABLE state before attempting to download the wallet.&lt;/p&gt;

&lt;p&gt;This command will download your wallet credentials in a .zip file, just like you'd get from the web console.&lt;/p&gt;

&lt;p&gt;The $DB_ID variable was set above for the demo database.  The --file parameter accepts the location and file name where you want to download the .zip file.  The new file will have the password set by --password.&lt;/p&gt;

&lt;pre class="lang:sh decode:true"&gt;oci db autonomous-database generate-wallet --autonomous-database-id $DB_ID --password Pw4ZipFile --file /home/bcarter/wallets/Wallet_demo.zip&lt;/pre&gt;

&lt;pre class="lang:default decode:true "&gt;Downloading file  [####################################]  100%&lt;/pre&gt;

&lt;p&gt;Now that everything is in place I can&lt;/p&gt;

&lt;h3&gt;Test the Connection&lt;/h3&gt;

&lt;ul&gt;
    &lt;li&gt;I'll start SQLcl without making a connection (/nolog).&lt;/li&gt;
    &lt;li&gt;Set cloudconfig to the location of the wallet credentials.&lt;/li&gt;
    &lt;li&gt;Connect with the admin password and one of the service names contained in the tnsnames.ora file included in the wallet zip file.
The predefined service names will be in the form of &lt;span class="lang:default decode:true crayon-inline"&gt;&amp;lt;name of the database&amp;gt;_&amp;lt;performance level&amp;gt;&lt;/span&gt; .  You can find more &lt;a href="https://docs.oracle.com/en/cloud/paas/atp-cloud/atpug/connect-predefined.html#GUID-9747539B-FD46-44F1-8FF8-F5AC650F15BE" rel="noopener noreferrer"&gt;information here&lt;/a&gt;.&lt;/li&gt;
    &lt;li&gt;Run a test query.&lt;/li&gt;
&lt;/ul&gt;

&lt;pre class="lang:plsql decode:true"&gt;sql /nolog
set cloudconfig $PRESENTATION_DIRECTORY/Active/LiquibaseAlwaysFree/code/wallet/Wallet_demo.zip
connect admin/T3stertester@demo_TP
select 'Yes! I connected to my Always Free ATP database!' did_it_work from dual;
exit;&lt;/pre&gt;

&lt;h3&gt;Use a Shell Script to Automate&lt;/h3&gt;

&lt;p&gt;The following is an example of a setup script I use for my demos.&lt;/p&gt;

&lt;pre class="lang:sh decode:true"&gt;#!/bin/bash

# Set Variables
COMPARTMENT_NAME="Demo"
DB_NAME="demo"
DB_DISPLAY_NAME="DemoDb"
DB_PW="T3stertester"
WALLET_PW="Pw4ZipFile"
WALLET_ZIP="/home/bcarter/tempWallet/Wallet_${DB_NAME}.zip"

# Create an Free-Tier Autonomous Database
create_db() {
    DB_ID=$(oci db autonomous-database create \
        --compartment-id ${COMPARTMENT_ID} \
        --cpu-core-count 1 \
        --data-storage-size-in-tbs 1 \
        --db-name "${DB_NAME}" \
        --display-name "${DB_DISPLAY_NAME}" \
        --db-workload "OLTP" \
        --admin-password "${DB_PW}" \
        --is-free-tier  True \
        --wait-for-state AVAILABLE \
        --query "data.id" --raw-output)
}

# Download the Wallet .zip file
download_wallet() {
    oci db autonomous-database generate-wallet --autonomous-database-id ${DB_ID} --password ${WALLET_PW} --file ${WALLET_ZIP}
}

# Get the Compartment OCID
COMPARTMENT_ID=$(oci iam compartment list --query "data[?name=='${COMPARTMENT_NAME}'].id | [0]" --raw-output)

# Get the Database OCID
DB_ID=$(oci db autonomous-database list -c ${COMPARTMENT_ID} --query "data[?\"db-name\"=='${DB_NAME}'].id | [0]" --raw-output)

# If the Database does not exist ask to create it.
if [[ -z "${DB_ID}" ]]; then
    echo "No ${DB_NAME} Database found."
    while true; do
        read -p "Do you wish to create the ${DB_NAME} Database? " yn
        case $yn in
            [Yy]* ) create_db; break;;
            [Nn]* ) exit;;
            * ) echo "Please answer y or n.";;
        esac
    done
fi

# Download the wallet
download_wallet

# Create an SQL script to test the connection
echo "set cloudconfig ${WALLET_ZIP}
connect admin/${DB_PW}@${DB_NAME}_TP
select 'Yes! I connected to my Always Free ATP database!' did_it_work from dual;
exit;" &amp;gt; testConnection.sql

# Test the connection
sql /nolog @testConnection.sql

# Delete the test script
rm testConnection.sql&lt;/pre&gt;

&lt;p&gt;Notice that in the create database method I added a new parameter to the OCI call &lt;span class="lang:default decode:true crayon-inline"&gt;--wait-for-state AVAILABLE&lt;/span&gt;.  Since I won't be able to download the wallet until the database is available, I use this parameter to pause at the create step until the new Database is fully up and running.&lt;/p&gt;

&lt;p&gt;When I run the script I get&lt;/p&gt;

&lt;pre class="lang:sh decode:true"&gt;&lt;a class="comment-mentioned-user" href="https://dev.to/orablaineos"&gt;@orablaineos&lt;/a&gt;
:OCI-CLI$ ./setup.sh
Query returned empty result, no output to show.
No demo Database found.
Do you wish to create the demo Database? y
Action completed. Waiting until the resource has entered state: ('AVAILABLE',)
Downloading file  [####################################]  100%

SQLcl: Release 18.3 Production on Fri Apr 03 14:28:11 2020

Copyright (c) 1982, 2020, Oracle.  All rights reserved.

Operation is successfully completed.
Operation is successfully completed.
Using temp directory:/tmp/oracle_cloud_config3687856744011425249
Connected.

DID_IT_WORK
------------------------------------------------
Yes! I connected to my Always Free ATP database!


Disconnected from Oracle Database 18c Enterprise Edition Release 18.0.0.0.0 - Production
Version 18.4.0.0.0&lt;/pre&gt;

&lt;h3&gt;Explore&lt;/h3&gt;

&lt;p&gt;This is just a small taste of what you can do with the OCI-CLI.  Check out the &lt;a href="https://docs.cloud.oracle.com/en-us/iaas/tools/oci-cli/2.9.8/oci_cli_docs/index.html" rel="noopener noreferrer"&gt;documentation&lt;/a&gt; for a look at the possibilities.&lt;/p&gt;

&lt;p&gt;Leave a comment if you have a question and I will do my best to find you an answer.&lt;/p&gt;

</description>
      <category>autonomous</category>
      <category>database</category>
      <category>cloud</category>
      <category>tools</category>
    </item>
    <item>
      <title>Install and configure the Oracle Cloud Command Line Interface</title>
      <dc:creator>Blaine Carter</dc:creator>
      <pubDate>Tue, 23 Jun 2020 19:00:03 +0000</pubDate>
      <link>https://dev.to/orablaineos/install-and-configure-the-oracle-cloud-command-line-interface-5472</link>
      <guid>https://dev.to/orablaineos/install-and-configure-the-oracle-cloud-command-line-interface-5472</guid>
      <description>&lt;p&gt;I love working with cloud resources.  I don't have to bog down my laptop and I don't have to maintain all of the back-end stuff.  I don't mean to make it sound as if I think the "back-end stuff" is easy.  It's actually, I understand just enough of that "stuff" to make it go (most of the time) so it's nice to have experts in the cloud taking care of it for me.  &lt;/p&gt;

&lt;p&gt;Some of you may be thinking "but cloud resources are expensive."  That's true sometimes, but it depends on who's cloud you're using.  If you've been following me you should already know how much I like Oracle's &lt;a href="https://www.oracle.com/cloud/free/" rel="noopener noreferrer"&gt;Always Free Services&lt;/a&gt; and you should know that I love to automate anything I can.  If you'd like to follow along but you don't have an Oracle Cloud account, click that link, create an Always Free account then come back.  &lt;/p&gt;

&lt;p&gt;In this post I'll cover how to use the &lt;a href="https://docs.cloud.oracle.com/en-us/iaas/Content/API/Concepts/cliconcepts.htm" rel="noopener noreferrer"&gt;Oracle Cloud Command Line Interface&lt;/a&gt; (OCI-CLI) to access and control your Oracle Cloud resources.&lt;/p&gt;

&lt;h3&gt;CLI Overview&lt;/h3&gt;

&lt;p&gt;Oracle's cloud CLI is a small application you can use to control your Oracle Cloud resources.  It gives you the same core functionality as you'd get using the Web Console, and some extra commands.  It allows you to control your cloud account from your local console application so you can easily automate the control of your resources.&lt;/p&gt;

&lt;h3&gt;Pre-requisits&lt;/h3&gt;

&lt;h5&gt;Python 3.5+&lt;/h5&gt;

&lt;p&gt;The CLI is built with Python so make sure you have &lt;a href="https://www.python.org/" rel="noopener noreferrer"&gt;Python version 3.5&lt;/a&gt; or higher installed.&lt;/p&gt;

&lt;h5&gt;RSA Key Pair&lt;/h5&gt;

&lt;p&gt;You will need an &lt;a href="https://docs.cloud.oracle.com/en-us/iaas/Content/API/Concepts/apisigningkey.htm" rel="noopener noreferrer"&gt;RSA key attached to your cloud user&lt;/a&gt; in order to remotely access your account.  This must be an RSA key pair in PEM format (minimum 2048 bits).&lt;/p&gt;

&lt;p&gt;The easiest way to generate this key par is with &lt;a href="https://www.openssl.org/" rel="noopener noreferrer"&gt;openssl&lt;/a&gt;.  The following commands work in Linux/Mac environments.  For Windows you can use your favorite tool or execute the commands in GitBash, WSL or some other Linux shell environment.&lt;/p&gt;

&lt;pre class="lang:default decode:true"&gt;openssl genrsa -out myPrivateKey.pem 2048
openssl rsa -pubout -in myPrivateKey.pem -out myPublicKey.pem&lt;/pre&gt;
The first command creates a &lt;strong&gt;PRIVATE KEY&lt;/strong&gt; called 'myPrivateKey.pem' (name yours whatever you'd like).  This is the key you will use to access remote systems.  DO NOT share this key, whoever has this key can connect to those systems as you.  Think of it as your admin password.&lt;br&gt;&lt;br&gt;
 
The second command uses your private key to create a &lt;strong&gt;PUBLIC KEY&lt;/strong&gt; called 'myPublicKey.pem' (name yours whatever you'd like).  This is the key you will share with remote systems.  Those systems will add your PUBLIC KEY to their authorized keys list, allowing you to access their system using your private key.&lt;br&gt;&lt;br&gt;

&lt;u&gt;Store these keys in a secure location.&lt;/u&gt;  On Linux, the default location is usually in the ~/.ssh directory.  But, if you're creating separate keys for your projects, you can store them wherever you'd like.  Just remember the location for later.
&lt;h5&gt;Cloud Account&lt;/h5&gt;
You need to have access to an Oracle Cloud account with a user that is &lt;a href="https://docs.cloud.oracle.com/en-us/iaas/Content/API/Concepts/cliconcepts.htm#Requirements" rel="noopener noreferrer"&gt;authorized to preform the tasks you intend to automate&lt;/a&gt;.  A good way to tell if your user has the correct permissions is to log onto your account through the Web Console and create an Always Free database then terminate it.

While you're logged into the Web Console collect some information.
&lt;ol&gt;
    &lt;li&gt;Tenancy
&lt;ol&gt;
    &lt;li&gt;In the menu, under Administration, click Tenancy Details.&lt;/li&gt;
    &lt;li&gt;Locate the OCID and click Copy.
&lt;img class="alignnone wp-image-1735" src="https://res.cloudinary.com/practicaldev/image/fetch/s--3cb0_sas--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://learncodeshare.net/wp-content/uploads/2020/03/tenancyOcid.png" alt="" width="677" height="489"&gt;
&lt;/li&gt;
    &lt;li&gt;Save this value for later.&lt;/li&gt;
&lt;/ol&gt;
&lt;/li&gt;
    &lt;li&gt; User
&lt;ol&gt;
    &lt;li&gt;In the menu under Identity click Users.&lt;/li&gt;
    &lt;li&gt;Select your user.&lt;/li&gt;
    &lt;li&gt;Near the bottom click 'API Keys' under the resource menu.&lt;/li&gt;
    &lt;li&gt;Click the 'Add Public Key' button.&lt;/li&gt;
    &lt;li&gt;Choose the PUBLIC key file you generated earlier.&lt;/li&gt;
    &lt;li&gt;Click the 'Add' button.&lt;/li&gt;
    &lt;li&gt;Your key should now show up in the 'API Keys' list.
&lt;img class="alignnone wp-image-1736" src="https://res.cloudinary.com/practicaldev/image/fetch/s--eQmPYPaI--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://learncodeshare.net/wp-content/uploads/2020/03/userMenu.png" alt="" width="848" height="503"&gt;
&lt;/li&gt;
    &lt;li&gt;Copy the fingerprint of your key and save it for later.&lt;/li&gt;
    &lt;li&gt;Near the top of the user page, locate the OCID and click Copy.  Save this value for later.
&lt;img class="alignnone size-full wp-image-1737" src="https://res.cloudinary.com/practicaldev/image/fetch/s--nuZl304o--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://learncodeshare.net/wp-content/uploads/2020/03/userOcid.png" alt="" width="417" height="181"&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;h3&gt;Quickstart Install&lt;/h3&gt;
You can &lt;a href="https://docs.cloud.oracle.com/en-us/iaas/Content/API/SDKDocs/cliinstall.htm" rel="noopener noreferrer"&gt;download an execute a script&lt;/a&gt; that will ask you typical installation configuration questions, after which it will install and configure the OCI-CLI.&lt;br&gt;&lt;br&gt;

The following is current as of the publish date for this post, but you may want to review the instructions in case things change.
&lt;h5&gt;Linux / Mac&lt;/h5&gt;
Open a terminal and run the following command.
&lt;pre class="lang:sh decode:true "&gt;bash -c "$(curl -L https://raw.githubusercontent.com/oracle/oci-cli/master/scripts/install/install.sh)"&lt;/pre&gt;
&lt;h5&gt;Windows&lt;/h5&gt;
Open PowerShell as Administrator.
&lt;pre class="lang:default decode:true "&gt;Set-ExecutionPolicy RemoteSigned

powershell -NoProfile -ExecutionPolicy Bypass -Command "iex ((New-Object System.Net.WebClient).DownloadString('https://raw.githubusercontent.com/oracle/oci-cli/master/scripts/install/install.ps1'))"&lt;/pre&gt;
In either system, answer the questions to complete the install.
&lt;h3&gt;Install with Python&lt;/h3&gt;
If you don't have the rights on your computer or you'd rather not execute the above scripts, you can &lt;a href="https://docs.cloud.oracle.com/en-us/iaas/Content/API/SDKDocs/climanualinst.htm" rel="noopener noreferrer"&gt;install the OCI-CLI with Python&lt;/a&gt;.&lt;br&gt;&lt;br&gt;

I recommend using a virtual environment when working with python.  It helps keep all of your projects clean and isolated.
&lt;pre class="lang:sh decode:true"&gt;python3 -m venv oci-cli-env

# Linux / Mac
source oci-cli-env/bin/activate

# Windows
oci-cli-env\Scripts\activate.bat&lt;/pre&gt;
Once your virtual environment is active, install the OCI-CLI with pip.
&lt;pre class="lang:default decode:true "&gt;pip install oci-cli&lt;/pre&gt;
You can see if the install was successful by checking the version.
&lt;pre class="lang:default decode:true"&gt;oci -v
&lt;/pre&gt;

&lt;pre class="lang:sh decode:true"&gt;2.9.5&lt;/pre&gt;

&lt;h3&gt;Configure the OCI-CLI&lt;/h3&gt;

&lt;p&gt;Using the values you saved from above, you can &lt;a href="https://docs.cloud.oracle.com/en-us/iaas/Content/API/Concepts/sdkconfig.htm" rel="noopener noreferrer"&gt;create a config file&lt;/a&gt; with this command.&lt;/p&gt;

&lt;pre class="lang:sh decode:true "&gt;oci setup config&lt;/pre&gt;

&lt;p&gt;You will be prompted for the following information.&lt;/p&gt;

&lt;pre class="lang:sh decode:true"&gt;Enter a location for your config [/home/bcarter/.oci/config]:
Enter a user OCID: &amp;lt;User OCID from above&amp;gt;
Enter a tenancy OCID: &amp;lt;Tenancy OCID from above&amp;gt;
Enter a region (e.g. us-ashburn-1, us-phoenix-1): us-ashburn-1
Do you want to generate a new RSA key pair? [Y/n]: n
Enter the location of your private key file: /home/bcarter/.ssh/myPrivateKey.pem
Fingerprint: &amp;lt;saved value&amp;gt;&lt;/pre&gt;

&lt;ul&gt;
    &lt;li&gt;The default location for the config file is typically '~/.oci/config'.  If you use a different location you will need to remember where it is.&lt;/li&gt;
    &lt;li&gt;Enter the user and tenancy OCIDs saved from your account.&lt;/li&gt;
    &lt;li&gt;Enter the region you want to work in.  The system will display some examples you can choose from.&lt;/li&gt;
    &lt;li&gt;You already generated an RSA key pair so enter 'n'.&lt;/li&gt;
    &lt;li&gt;Enter the location of your PRIVATE key.  This will not be uploaded, the CLI will use your PRIVATE key to make the connection to the cloud.&lt;/li&gt;
    &lt;li&gt;Enter the Fingerprint you saved when you uploaded your PUBLIC key.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Once it's complete, you should see a response similar to this.&lt;/p&gt;

&lt;pre class="lang:sh decode:true "&gt;Config written to /home/bcarter/.oci/config&lt;/pre&gt;

&lt;p&gt;Your OCI-CLI should now be configured.  If you open the config file it should look similar to this.&lt;/p&gt;

&lt;pre class="lang:default decode:true"&gt;[DEFAULT]
user=ocid1.user.oc1..aaaaaaaat5nvwcna5j6aqzjcaty5eqbb6qt2jvpkanghtgdaqedqw3rynjq
fingerprint=20:3b:97:13:55:1c:5b:0d:d3:37:d8:50:4e:c5:3a:34
key_file=~/.oci/oci_api_key.pem
tenancy=ocid1.tenancy.oc1..aaaaaaaaba3pv6wkcr4jqae5f15p2b2m2yt2j6rx32uzr5vqstifsfdsq
region=us-ashburn-1&lt;/pre&gt;

&lt;p&gt;You can add other connection profiles manually by following this format or you can use the same command to add a new profile.  If you re-run the command, it will ask you for a name to use for the new profile which will be added to the config file.&lt;/p&gt;

&lt;pre class="lang:default decode:true "&gt;[DEFAULT]
user=ocid1.user.oc1..aaaaaaaat5nvwcna5j6aqzjcaty5eqbb6qt2jvpkanghtgdaqedqw3rynjq
fingerprint=20:3b:97:13:55:1c:5b:0d:d3:37:d8:50:4e:c5:3a:34
key_file=~/.oci/oci_api_key.pem
tenancy=ocid1.tenancy.oc1..aaaaaaaaba3pv6wkcr4jqae5f15p2b2m2yt2j6rx32uzr5vqstifsfdsq
region=us-ashburn-1

[ACCOUNT2]
user=ocid1.user.oc1..aaaaaaaa65vwl7zut55hiavppn4nbfwyccuecuch5tewwm32rgqvm6i34unq
fingerprint=72:00:22:7f:d3:8b:47:a4:58:05:b8:95:84:31:dd:0e
key_file=~/.ssh/other_key.pem
tenancy=ocid1.tenancy.oc1..aaaaaaaaba3pv6wkcr4jqae5f15p2b2m2yt2j6rx32uzr5vqstifsfdsq
region=us-ashburn-1&lt;/pre&gt;

&lt;h5&gt;Quick Test&lt;/h5&gt;

&lt;p&gt;Enter the following command.&lt;/p&gt;

&lt;pre class="lang:default decode:true "&gt;oci iam availability-domain list&lt;/pre&gt;

&lt;p&gt;If everything is working, you should receive a response similar to this.&lt;/p&gt;

&lt;pre class="lang:default decode:true "&gt;{
  "data": [
    {
      "compartment-id": "ocid1.tenancy.oc1..biglongguid",
      "id": "ocid1.availabilitydomain.oc1..biglongguid",
      "name": "qVbG:US-ASHBURN-AD-1"
    },
    {
      "compartment-id": "ocid1.tenancy.oc1..biglongguid",
      "id": "ocid1.availabilitydomain.oc1..biglongguid",
      "name": "qVbG:US-ASHBURN-AD-2"
    }
  ]
}&lt;/pre&gt;

&lt;p&gt;Now that your OCI-CLI is installed and configured, you should familiarize yourself with the &lt;a href="https://docs.cloud.oracle.com/en-us/iaas/tools/oci-cli/latest/oci_cli_docs/" rel="noopener noreferrer"&gt;OCI-CLI documentation&lt;/a&gt; to learn about the many, many commands you can use to automate the control of your Oracle Cloud resources.&lt;/p&gt;

&lt;p&gt;You can use these same commands from any system with the OCI-CLI installed, including Oracle Cloud Compute instances.&lt;/p&gt;

</description>
      <category>autonomous</category>
      <category>database</category>
      <category>cloud</category>
      <category>tools</category>
    </item>
    <item>
      <title>Create and use an Oracle Cloud Compute instance from the Command Line</title>
      <dc:creator>Blaine Carter</dc:creator>
      <pubDate>Thu, 18 Jun 2020 21:44:56 +0000</pubDate>
      <link>https://dev.to/orablaineos/create-and-use-an-oracle-cloud-compute-instance-from-the-command-line-4kom</link>
      <guid>https://dev.to/orablaineos/create-and-use-an-oracle-cloud-compute-instance-from-the-command-line-4kom</guid>
      <description>&lt;p&gt;In &lt;a href="https://learncodeshare.net/2020/04/06/create-and-use-an-oracle-autonomous-cloud-database-from-the-command-line/"&gt;a previous post&lt;/a&gt;, I walked through how to create an Autonomous Database on the Oracle Cloud using the OCI-CLI.  In this post you’ll learn how to create a compute instance.&lt;/p&gt;

&lt;p&gt;You can use these commands in your &lt;a href="https://docs.cloud.oracle.com/en-us/iaas/Content/API/Concepts/cloudshellintro.htm"&gt;Oracle Cloud Shell&lt;/a&gt; from your Cloud Dashboard where the OCI-CLI is already setup and ready to go.&lt;/p&gt;

&lt;p&gt;If you’d rather use your own environment you can follow &lt;a href="https://learncodeshare.net/2020/03/06/install-and-configure-the-oracle-cloud-command-line-interface/"&gt;these instructions&lt;/a&gt; to install and configure the OCI-CLI.&lt;/p&gt;

&lt;h3&gt;
  
  
  Environment Variables
&lt;/h3&gt;

&lt;p&gt;There are some pieces of information you’ll need in order to create the compute instance.  Of course you can look this information up manually, but it’s more fun to automate as much as possible.&lt;/p&gt;

&lt;h5&gt;
  
  
  Preset Values
&lt;/h5&gt;

&lt;p&gt;Create environment variables for the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The name of the Compartment you want to create your new Compute instance in.&lt;/li&gt;
&lt;li&gt;The name for your new Compute instance.&lt;/li&gt;
&lt;li&gt;The &lt;a href="https://docs.cloud.oracle.com/en-us/iaas/Content/Compute/References/computeshapes.htm"&gt;shape&lt;/a&gt; you want to use.  VM.Standard.E2.1.Micro is used for an Always-Fee Compute instance.&lt;/li&gt;
&lt;li&gt;The absolute path for your user’s home directory.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export COMPARTMENT\_NAME='Test'export COMPUTE\_NAME='TestCompute'export COMPUTE\_SHAPE='VM.Standard.E2.1.Micro'export USER\_HOME=$(eval echo ~)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The following commands will create the given object and use the returned OCID to create an environment variable to be used in the other steps.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export NEW\_OCID=$(the OCI command you would run to create the object and return the OCID)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h5&gt;
  
  
  Compartment OCID
&lt;/h5&gt;

&lt;p&gt;&lt;a href="https://learncodeshare.net/2020/04/06/create-and-use-an-oracle-autonomous-cloud-database-from-the-command-line/#GetCompartment"&gt;The previous post&lt;/a&gt; demonstrates how to use the --query  parameter to get the OCID for the Compartment.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export COMPARTMENT\_ID=$(oci iam compartment list --query "data[?name=='${COMPARTMENT\_NAME}'].id | [0]" --raw-output)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h5&gt;
  
  
  Availability Domain
&lt;/h5&gt;

&lt;p&gt;You’ll need to define which &lt;a href="https://docs.cloud.oracle.com/en-us/iaas/Content/General/Concepts/regions.htm"&gt;Availability Domain&lt;/a&gt; you want to use.  The above Compute Shape is typically available in your third sub-domain, ‘xxxx:US-ASHBURN-AD-3’.&lt;/p&gt;

&lt;p&gt;The following --query  parameter for the &lt;a href="https://docs.cloud.oracle.com/en-us/iaas/tools/oci-cli/latest/oci_cli_docs/cmdref/iam/availability-domain/list.html"&gt;list command&lt;/a&gt; will search for the name of a sub-domain ending in ‘-3’, if one is not found it will chose the first sub-domain in the array.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export AVAILABILITY\_DOMAIN=$(oci iam availability-domain list --query "(data[?ends\_with(name, '-3')] | [0].name) || data[0].name" --raw-output)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  Create a Virtual Cloud Network
&lt;/h3&gt;

&lt;p&gt;Your Compute instance will need a &lt;a href="https://docs.cloud.oracle.com/en-us/iaas/Content/Network/Tasks/managingVCNs.htm"&gt;VCN&lt;/a&gt; in order to connect to the outside world.  If you have already created a compute instance in this compartment you can re-use the existing VCN and subnet or follow these instructions to &lt;a href="https://docs.cloud.oracle.com/en-us/iaas/tools/oci-cli/latest/oci_cli_docs/cmdref/network/vcn/create.html"&gt;create a new one&lt;/a&gt;.&lt;/p&gt;

&lt;h5&gt;
  
  
  Create a new VCN
&lt;/h5&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export VCN\_ID=$(oci network vcn create -c ${COMPARTMENT\_ID} --cidr-block "10.0.0.0/16" --query "data.id" --raw-output)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h5&gt;
  
  
  Add a Subnet to the VCN
&lt;/h5&gt;

&lt;p&gt;&lt;a href="https://docs.cloud.oracle.com/en-us/iaas/tools/oci-cli/latest/oci_cli_docs/cmdref/network/subnet/create.html"&gt;Create a new subnet&lt;/a&gt; that your compute instance will use for connections.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export SUBNET\_ID=$(oci network subnet create --vcn-id ${VCN\_ID} -c ${COMPARTMENT\_ID} --cidr-block "10.0.0.0/24" --query "data.id" --raw-output)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h5&gt;
  
  
  Add an Internet Gateway
&lt;/h5&gt;

&lt;p&gt;&lt;a href="https://docs.cloud.oracle.com/en-us/iaas/tools/oci-cli/latest/oci_cli_docs/cmdref/network/internet-gateway/create.html"&gt;Create&lt;/a&gt; an &lt;a href="https://docs.cloud.oracle.com/en-us/iaas/Content/Network/Tasks/managingIGs.htm"&gt;Internet Gateway&lt;/a&gt; that will be used to connect from your VCN to the internet.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export IG\_ID=$(oci network internet-gateway create -c ${COMPARTMENT\_ID} --is-enabled true --vcn-id ${VCN\_ID} --query "data.id" --raw-output)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h5&gt;
  
  
  Add a Route Table
&lt;/h5&gt;

&lt;p&gt;&lt;a href="https://docs.cloud.oracle.com/en-us/iaas/tools/oci-cli/latest/oci_cli_docs/cmdref/network/route-table/create.html"&gt;Create&lt;/a&gt; a &lt;a href="https://docs.cloud.oracle.com/en-us/iaas/Content/Network/Tasks/managingroutetables.htm"&gt;Route Table&lt;/a&gt; which is a collection of rules used to route packets to the correct network entity.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export RT\_ID=$(oci network route-table list -c ${COMPARTMENT\_ID} --vcn-id ${VCN\_ID} --query "data[0].id" --raw-output)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h5&gt;
  
  
  Add a Route Rule for the Internet Gateway
&lt;/h5&gt;

&lt;p&gt;&lt;a href="https://docs.cloud.oracle.com/en-us/iaas/tools/oci-cli/latest/oci_cli_docs/cmdref/network/route-table/update.html"&gt;Update&lt;/a&gt; your Route Table and add a rule granting internet access to your compute instance through your Internet Gateway.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;oci network route-table update --rt-id ${RT\_ID} --route-rules '[{"cidrBlock":"0.0.0.0/0","networkEntityId":"'${IG\_ID}'"}]' --force
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  RSA Key
&lt;/h3&gt;

&lt;p&gt;In order to connect to your compute instance you’ll need an &lt;a href="https://docs.cloud.oracle.com/en-us/iaas/Content/Compute/Tasks/managingkeypairs.htm"&gt;RSA key pair&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;If you don’t already have a key pair, use the following command to generate two new files, your “key pair”.  id_rsa is your private key, do not share this.  id_rsa.pub  is your public key that you will share.  The below command will create these files in the .ssh directory inside your home directory.  You can change the directory or name if you wish.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ssh-keygen -t rsa -N "" -b 2048 -C "CiCd-Compute-Instance" -f ${USER\_HOME}/.ssh/id\_rsa
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now that you have created a network and a key pair you can&lt;/p&gt;

&lt;h3&gt;
  
  
  Create the Compute Instance
&lt;/h3&gt;

&lt;p&gt;If you created your key pair with a different name or in a different location than ${USER_HOME}/.ssh/id_rsa.pub , you will need to modify the --ssh-authorized-keys-filevalue below when you &lt;a href="https://docs.cloud.oracle.com/en-us/iaas/tools/oci-cli/latest/oci_cli_docs/cmdref/compute/instance/launch.html"&gt;launch&lt;/a&gt; your new instance.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export COMPUTE\_OCID=$(oci compute instance launch \ -c ${COMPARTMENT\_ID} \ --shape "${COMPUTE\_SHAPE}" \ --display-name "${COMPUTE\_NAME}" \ --image-id ocid1.image.oc1.iad.aaaaaaaahjkmmew2pjrcpylaf6zdddtom6xjnazwptervti35keqd4fdylca \ --ssh-authorized-keys-file "${USER\_HOME}/.ssh/id\_rsa.pub" \ --subnet-id ${SUBNET\_ID} \ --availability-domain "${AVAILABILITY\_DOMAIN}" \ --wait-for-state RUNNING \ --query "data.id" \ --raw-output)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Most of the parameters use the values we defined above.&lt;/p&gt;

&lt;p&gt;The parameter --image-id is the OCID for the Oracle Linux 7.8 image.&lt;/p&gt;

&lt;p&gt;The parameter --wait-for-state RUNNING  will pause at the launch command until your instance is fully up and running.&lt;/p&gt;

&lt;h5&gt;
  
  
  Get the Public IP
&lt;/h5&gt;

&lt;p&gt;You will need the public ip for your instance in order to establish an ssh connection.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export COMPUTE\_IP=$(oci compute instance list-vnics \ --instance-id "${COMPUTE\_OCID}" \ --query 'data[0]."public-ip"' \ --raw-output)echo $COMPUTE\_IP
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  Connect
&lt;/h3&gt;

&lt;p&gt;Use ssh to connect to the new instance.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ssh opc@${COMPUTE\_IP}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;If you changed the location or the name of your private key you may need to include the &lt;strong&gt;private&lt;/strong&gt; key.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ssh -i /path/to/your/key/privateKeyName opc@${COMPUTE\_IP}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  Enjoy
&lt;/h3&gt;

&lt;p&gt;At this point you can start configuring your new Compute instance however you’d like.&lt;/p&gt;

&lt;p&gt;Leave a comment if something goes wrong or if you have any questions.&lt;/p&gt;

</description>
      <category>autonomous</category>
      <category>database</category>
      <category>cloud</category>
      <category>tools</category>
    </item>
  </channel>
</rss>
