Launch your first Compute Instance from OCI CLI on Window 10 OS Platform

First of all, We have to install OCI CLI in the windows machine then we will configure OCI CLI and further we will play with compute instance life cycle operations like launch instance, start, stop and other possible actions and at the end, we will terminate the instance from oci cli.

#Install OCI CLI 
Follow the below commands provided in OCI Documentation.

C:\WINDOWS\system32>powershell
Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.

PS C:\WINDOWS\system32> Set-ExecutionPolicy RemoteSigned

PS C:\WINDOWS\system32> powershell -NoProfile -ExecutionPolicy Bypass -Command “iex ((New-Object System.Net.WebClient).DownloadString(‘https://raw.githubusercontent.com/oracle/oci-cli/master/scripts/install/install.ps1’))”

******************************************************************************
You have started the OCI CLI Installer in interactive mode. If you do not wish
to run this in interactive mode, please include the -AcceptAllDefaults option.
If you have the script locally and would like to know more about
input options for this script, then you can run:
help .\install.ps1
If you would like to know more about input options for this script, refer to:
https://github.com/oracle/oci-cli/blob/master/scripts/install/README.rst
******************************************************************************
VERBOSE: Python found in registry: HKCU:\Software\Python\PythonCore
VERBOSE: Downloading install script to C:\Users\oracle-learn\AppData\Local\Temp\tmpDDAF.tmp
VERBOSE: False False
VERBOSE: Using Python executable: C:\Users\oracle-learn\AppData\Local\Programs\Python\Python37\python.exe to run install
script…
VERBOSE: Arguments to python script: “C:\Users\oracle-learn\AppData\Local\Temp\tmpDDAF.tmp”
— Verifying Python version.
— Python version 3.7.5 okay.

===> In what directory would you like to place the install? (leave blank to use ‘C:\Users\oracle-learn\lib\oracle-cli’):
— We will install at ‘C:\Users\oracle-learn\lib\oracle-cli’.

===> In what directory would you like to place the ‘oci.exe’ executable? (leave blank to use ‘C:\Users\oracle-learn\bin’):
— The executable will be in ‘C:\Users\oracle-learn\bin’.

===> In what directory would you like to place the OCI scripts? (leave blank to use ‘C:\Users\oracle-learn\bin\oci-cli-scripts’):
— The scripts will be in ‘C:\Users\oracle-learn\bin\oci-cli-scripts’.

===> Currently supported optional packages are: [‘db (will install cx_Oracle)’]
What optional CLI packages would you like to be installed (comma separated names; press enter if you don’t need any optional packages)?:
— The optional packages installed will be ”.
— Downloading virtualenv package from https://github.com/pypa/virtualenv/archive/15.0.0.tar.gz.
— Downloaded virtualenv package to C:\Users\oracle-learn\AppData\Local\Temp\tmpukxf413h\15.0.0.tar.gz.
— Checksum of C:\Users\oracle-learn\AppData\Local\Temp\tmpukxf413h\15.0.0.tar.gz OK.
— Extracting ‘C:\Users\oracle-learn\AppData\Local\Temp\tmpukxf413h\15.0.0.tar.gz’ to ‘C:\Users\oracle-learn\AppData\Local\Temp\tmpukxf413h’.
— Copying DLLs into virtualenv.
— Copying C:\Users\oracle-learn\AppData\Local\Programs\Python\Python37\python3.dll to C:\Users\oracle-learn\lib\oracle-cli\Scripts
— Copying C:\Users\oracle-learn\AppData\Local\Programs\Python\Python37\python37.dll to C:\Users\oracle-learn\lib\oracle-cli\Scripts
— Copying C:\Users\oracle-learn\AppData\Local\Programs\Python\Python37\vcruntime140.dll to C:\Users\oracle-learn\lib\oracle-cli\Scripts
— Executing: [‘C:\\Users\\oracle-learn\\AppData\\Local\\Programs\\Python\\Python37\\python.exe’, ‘virtualenv.py’, ‘–python’, ‘C:\\Users\\oracle-learn\\AppData\\Local\\Programs\\Python\\Python37\\python.exe’, ‘C:\\Users\\oracle-learn\\lib\\oracle-cli’]
Already using interpreter C:\Users\oracle-learn\AppData\Local\Programs\Python\Python37\python.exe
Using base prefix ‘C:\\Users\\oracle-learn\\AppData\\Local\\Programs\\Python\\Python37’
virtualenv.py:1030: DeprecationWarning: the imp module is deprecated in favour of importlib; see the module’s documentation for alternative uses
import imp
New python executable in C:\Users\oracle-learn\lib\oracle-cli\Scripts\python.exe
Installing setuptools, pip, wheel…done.
— Executing: [‘C:\\Users\\oracle-learn\\lib\\oracle-cli\\Scripts\\pip’, ‘install’, ‘–cache-dir’, ‘C:\\Users\\oracle-learn\\AppData\\Local\\Temp\\tmpukxf413h’, ‘oci_cli’, ‘–upgrade’]
Collecting oci_cli
Downloading https://files.pythonhosted.org/packages/93/b3/6f4d3f257dd9bcddbf1c139aa0b085369ff6534cb94da9355907c7c62ef1/oci_cli-2.7.0-py2.py3-none-any.whl (6.3MB)
|████████████████████████████████| 6.3MB 363kB/s
Collecting jmespath==0.9.3
Downloading https://files.pythonhosted.org/packages/b7/31/05c8d001f7f87f0f07289a5fc0fc3832e9a57f2dbd4d3b0fee70e0d51365/jmespath-0.9.3-py2.py3-none-any.whl
Collecting oci==2.8.0
Downloading https://files.pythonhosted.org/packages/7e/97/55a70ea8b2c2c79c5c39740439c93cd076a48bd19d5dfc75f6ab5ddaa7f2/oci-2.8.0-py2.py3-none-any.whl (3.1MB)
|████████████████████████████████| 3.1MB 409kB/s
Collecting cryptography==2.4.2
Downloading https://files.pythonhosted.org/packages/c7/c7/2b97b9af3b16ea45aeabd5fe7eb07902c0c24c5d596b68f677c1f50ae55e/cryptography-2.4.2-cp37-cp37m-win_amd64.whl (1.3MB)
|████████████████████████████████| 1.3MB 726kB/s
Collecting six==1.11.0
Downloading https://files.pythonhosted.org/packages/67/4b/141a581104b1f6397bfa78ac9d43d8ad29a7ca43ea90a2d863fe3056e86a/six-1.11.0-py2.py3-none-any.whl
Collecting configparser==3.5.0
Downloading https://files.pythonhosted.org/packages/7c/69/c2ce7e91c89dc073eb1aa74c0621c3eefbffe8216b3f9af9d3885265c01c/configparser-3.5.0.tar.gz
Collecting pytz==2016.10
Downloading https://files.pythonhosted.org/packages/f5/fa/4a9aefc206aa49a4b5e0a72f013df1f471b4714cdbe6d78f0134feeeecdb/pytz-2016.10-py2.py3-none-any.whl (483kB)
|████████████████████████████████| 491kB 1.3MB/s
Collecting PyYAML==5.1.2
Downloading https://files.pythonhosted.org/packages/bc/3f/4f733cd0b1b675f34beb290d465a65e0f06b492c00b111d1b75125062de1/PyYAML-5.1.2-cp37-cp37m-win_amd64.whl (215kB)
|████████████████████████████████| 225kB 285kB/s
Collecting pyOpenSSL==18.0.0
Downloading https://files.pythonhosted.org/packages/96/af/9d29e6bd40823061aea2e0574ccb2fcf72bfd6130ce53d32773ec375458c/pyOpenSSL-18.0.0-py2.py3-none-any.whl (53kB)
|████████████████████████████████| 61kB 218kB/s
Collecting click==6.7
Downloading https://files.pythonhosted.org/packages/34/c1/8806f99713ddb993c5366c362b2f908f18269f8d792aff1abfd700775a77/click-6.7-py2.py3-none-any.whl (71kB)
|████████████████████████████████| 71kB 353kB/s
Collecting python-dateutil==2.7.3
Downloading https://files.pythonhosted.org/packages/cf/f5/af2b09c957ace60dcfac112b669c45c8c97e32f94aa8b56da4c6d1682825/python_dateutil-2.7.3-py2.py3-none-any.whl (211kB)
|████████████████████████████████| 215kB 504kB/s
Collecting certifi
Downloading https://files.pythonhosted.org/packages/b9/63/df50cac98ea0d5b006c55a399c3bf1db9da7b5a24de7890bc9cfd5dd9e99/certifi-2019.11.28-py2.py3-none-any.whl (156kB)
|████████████████████████████████| 163kB 409kB/s
Collecting idna<2.7,>=2.5
Downloading https://files.pythonhosted.org/packages/27/cc/6dd9a3869f15c2edfab863b992838277279ce92663d334df9ecf5106f5c6/idna-2.6-py2.py3-none-any.whl (56kB)
|████████████████████████████████| 61kB 563kB/s
Collecting terminaltables==3.1.0
Downloading https://files.pythonhosted.org/packages/9b/c4/4a21174f32f8a7e1104798c445dacdc1d4df86f2f26722767034e4de4bff/terminaltables-3.1.0.tar.gz
Collecting retrying==1.3.3
Downloading https://files.pythonhosted.org/packages/44/ef/beae4b4ef80902f22e3af073397f079c96969c69b2c7d52a57ea9ae61c9d/retrying-1.3.3.tar.gz
Collecting arrow==0.10.0
Downloading https://files.pythonhosted.org/packages/54/db/76459c4dd3561bbe682619a5c576ff30c42e37c2e01900ed30a501957150/arrow-0.10.0.tar.gz (86kB)
|████████████████████████████████| 92kB 368kB/s
Collecting cffi!=1.11.3,>=1.7
Downloading https://files.pythonhosted.org/packages/71/71/6011c17e8a523be0be20149ed7ba7e8671db7ec6fc52868e4f9a49d527f8/cffi-1.13.2-cp37-cp37m-win_amd64.whl (175kB)
|████████████████████████████████| 184kB 1.1MB/s
Collecting asn1crypto>=0.21.0
Downloading https://files.pythonhosted.org/packages/6b/8c/ff300484eca90b397d919408619fae479965bdd8a1df3d6d08d58e491da5/asn1crypto-1.2.0-py2.py3-none-any.whl (103kB)
|████████████████████████████████| 112kB 1.3MB/s
Collecting pycparser
Downloading https://files.pythonhosted.org/packages/68/9e/49196946aee219aead1290e00d1e7fdeab8567783e83e1b9ab5585e6206a/pycparser-2.19.tar.gz (158kB)
|████████████████████████████████| 163kB 328kB/s
Building wheels for collected packages: configparser, terminaltables, retrying, arrow, pycparser
Building wheel for configparser (setup.py) … done
Created wheel for configparser: filename=configparser-3.5.0-cp37-none-any.whl size=20865 sha256=abc942bac573f3e5f612d62de463f504320677dc1a6d2788edfb03ed52de7aed
Stored in directory: C:\Users\oracle-learn\AppData\Local\Temp\tmpukxf413h\wheels\a3\61\79\424ef897a2f3b14684a7de5d89e8600b460b89663e6ce9d17c
Building wheel for terminaltables (setup.py) … done
Created wheel for terminaltables: filename=terminaltables-3.1.0-cp37-none-any.whl size=15362 sha256=53403741f831f93681673c00903de32d313f57c3d539207141a994b5a75026d8
Stored in directory: C:\Users\oracle-learn\AppData\Local\Temp\tmpukxf413h\wheels\30\6b\50\6c75775b681fb36cdfac7f19799888ef9d8813aff9e379663e
Building wheel for retrying (setup.py) … done
Created wheel for retrying: filename=retrying-1.3.3-cp37-none-any.whl size=11435 sha256=662b2240b6aaa349ae3edc39b178cb9bd2cafa4ffd22333154573f356f63434a
Stored in directory: C:\Users\oracle-learn\AppData\Local\Temp\tmpukxf413h\wheels\d7\a9\33\acc7b709e2a35caa7d4cae442f6fe6fb
f2c43f80823d46460c
Building wheel for arrow (setup.py) … done
Created wheel for arrow: filename=arrow-0.10.0-py2.py3-none-any.whl size=32856 sha256=358422ae1a2eaa31691be7ded73643fd
700ee2e1d19d64e1bd42124c1d3cea5c
Stored in directory: C:\Users\oracle-learn\AppData\Local\Temp\tmpukxf413h\wheels\ce\4f\95\64541c7466fd88ffe72fda5164f8323c91d695c9a77072c574
Building wheel for pycparser (setup.py) … done
Created wheel for pycparser: filename=pycparser-2.19-py2.py3-none-any.whl size=111018 sha256=402b3b5fcdf0280d8ca2389825fed90efcdb20185d76b36fab57cc6008fdbf4c
Stored in directory: C:\Users\oracle-learn\AppData\Local\Temp\tmpukxf413h\wheels\f2\9a\90\de94f8556265ddc9d9c8b271b0f63e57b26fb1d67a45564511
Successfully built configparser terminaltables retrying arrow pycparser
Installing collected packages: jmespath, idna, pycparser, cffi, asn1crypto, six, cryptography, configparser, certifi, pytz, pyOpenSSL, python-dateutil, oci, PyYAML, click, terminaltables, retrying, arrow, oci-cli
Successfully installed PyYAML-5.1.2 arrow-0.10.0 asn1crypto-1.2.0 certifi-2019.11.28 cffi-1.13.2 click-6.7 configparser-3.5.0 cryptography-2.4.2 idna-2.6 jmespath-0.9.3 oci-2.8.0 oci-cli-2.7.0 pyOpenSSL-18.0.0 pycparser-2.19 python-dateutil-2.7.3 pytz-2016.10 retrying-1.3.3 six-1.11.0 terminaltables-3.1.0

===> Modify PATH to include the CLI and enable tab completion in PowerShell now? (Y/n): Y

— ** Close and re-open PowerShell to reload changes to your PATH **
— In order to run the autocomplete script, you may also need to set your PowerShell execution policy to allow for running local scripts (as an Administrator run Set-ExecutionPolicy RemoteSigned in a PowerShell prompt)

— Installation successful.
— Run the CLI with C:\Users\oracle-learn\bin\oci.exe –help
VERBOSE: Successfully installed OCI CLI!
PS C:\WINDOWS\system32>

#Configure the OCI CLI

C:\Users\oracle-learn\bin> oci setup config
This command provides a walkthrough of creating a valid CLI config file.

The following links explain where to find the information required by this
script:

User OCID and Tenancy OCID:

https://docs.cloud.oracle.com/Content/API/Concepts/apisigningkey.htm#Other

Region:

https://docs.cloud.oracle.com/Content/General/Concepts/regions.htm

General config documentation:

https://docs.cloud.oracle.com/Content/API/Concepts/sdkconfig.htm

Enter a location for your config [C:\Users\oracle-learn\.oci\config]: C:\Users\oracle-learn\.oci\config
Enter a user OCID: ocid1.user.oc1..fddsfdsfdsfjhgjhjhg54534vcxvcxvcxv
Enter a tenancy OCID: ocid1.tenancy.oc1..dfdsfljfsdjf433sdfsdlfjsfjsdf
Enter a region (e.g. ap-mumbai-1, ap-seoul-1, ap-sydney-1, ap-tokyo-1, ca-toronto-1, eu-frankfurt-1, eu-zurich-1, sa-saopaulo-1, uk-gov-london-1, uk-london-1, us-ashburn-1, us-gov-ashburn-1, us-gov-chicago-1, us-gov-phoenix-1, us-langley-1, us-luke-1, us-phoenix-1): ap-mumbai-1
Do you want to generate a new RSA key pair? (If you decline you will be asked to supply the path to an existing key.) [Y/n]: Y
Enter a directory for your keys to be created [C:\Users\oracle-learn\.oci]:
Enter a name for your key [oci_api_key]:
Public key written to: C:\Users\oracle-learn\.oci\oci_api_key_public.pem
Enter a passphrase for your private key (empty for no passphrase):
File C:\Users\oracle-learn\.oci\oci_api_key.pem already exists, do you want to overwrite? [y/N]: y
Private key written to: C:\Users\oracle-learn\.oci\oci_api_key.pem
Fingerprint: 29:80:a7:d8:4d:5b:a1:54:d1:c6:cc:b3:de:6c:ef:52
Config written to C:\Users\oracle-learn\.oci\config

If you haven’t already uploaded your public key through the console,

follow the instructions on the page linked below in the section ‘How to
upload the public key’:

https://docs.cloud.oracle.com/Content/API/Concepts/apisigningkey.htm#How2
C:\Users\oracle-learn\bin>C:\Users\oracle-learn\.ssh\id_rsa.pub

C:\Users\oracle-learn\bin>oci –help

Once everything is configured correctly you can run a few commands to try out the client. The basic syntax is:
$ oci

$ oci
Some of the available services are:

Core (Networking, Compute, Block Volume, etc.)
Database
IAM
Load Balancing
Object Storage

#Launch Compute instance

When you launch an instance you have to provide the following information, some of which you’ve already obtained:

compartment-id
availability-domain
subnet-id
image-id
shape

Sample commands to find the required information.
oci iam user list –compartment-id
oci iam availability-domain list
oci compute shape list -c
oci compute image list -c

You can use -h option at end of the command to find an help related to specific command option.
oci compute image list -h
oci compute shape list -h

Syntax:
oci compute instance launch –availability-domain “” -c –shape “” –display-name “” –image-id –ssh-authorized-keys-file “” –subnet-id

Example:

oci compute instance launch --availability-domain "AARS:AP-MUMBAI-1-AD-1" -c ocid1.compartment.oc1..ggfdh434332sfsdfsdfsdfsf --shape "VM.Standard.E2.1" --display-name "MyFirstMachineFromCLI" --image-id ocid1.image.oc1.ap-mumbai-1.aaaaaaaaka7f3qhfuobx2s7dqfgbcx5klllh5xlflbgzb5pymqsnuphehk2a --ssh-authorized-keys-file "C:\Users\oracle-learn\.ssh\id_rsa.pub" --subnet-id ocid1.subnet.oc1.ap-mumbai-1.aaaaaaaah44xlfte72zz4yrgrp5pqc3ndqy5atc3kqlfmz4lzwdlsdlhyoya

#List Compute instances
oci compute instance list --compartment-id ocid1.compartment.oc1..ggfdh434332sfsdfsdfsdfsf

Performs one of the following power actions on the specified instance

[1] * **START** – Powers on the instance.

oci compute instance action --instance-id ocid1.instance.oc1.ap-mumbai-1.fdsfdsfsdfsdfdsf343sdfsdfsfsdf --action START --wait-for-state RUNNING

[2] * **STOP** – Powers off the instance.

oci compute instance action --instance-id ocid1.instance.oc1.ap-mumbai-1.fdsfdsfsdfsdfdsf343sdfsdfsfsdf --action STOP --wait-for-state STOPPED

[3] * **SOFTRESET** – Gracefully reboots instance by sending a shutdown
command to the operating system and then powers the instance back
on.

oci compute instance action --instance-id ocid1.instance.oc1.ap-mumbai-1.fdsfdsfsdfsdfdsf343sdfsdfsfsdf --action SOFTRESET --wait-for-state RUNNING

[4] * **SOFTSTOP** – Gracefully shuts down instance by sending a
shutdown command to the operating system.

oci compute instance action --instance-id ocid1.instance.oc1.ap-mumbai-1.fdsfdsfsdfsdfdsf343sdfsdfsfsdf --action SOFTSTOP --wait-for-state STOPPED

[5] * **RESET** – Powers off the instance and then powers it back on.

oci compute instance action --instance-id ocid1.instance.oc1.ap-mumbai-1.fdsfdsfsdfsdfdsf343sdfsdfsfsdf --action RESET --wait-for-state RUNNING

#Terminate Compute instance
oci compute instance terminate --instance-id ocid1.instance.oc1.ap-mumbai-1.fdsfdsfsdfsdfdsf343sdfsdfsfsdf

#OCI Documentation References
https://docs.cloud.oracle.com/iaas/Content/GSG/Tasks/gettingstartedwiththeCLI.htm
https://docs.cloud.oracle.com/iaas/Content/API/SDKDocs/cliinstall.htm
https://docs.cloud.oracle.com/iaas/Content/API/Concepts/apisigningkey.htm

Thank you for visit this blog

Happy Learning 🙂