Create and use an Oracle Cloud Compute instance from the Command Line

In a previous post, 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.

You can use these commands in your Oracle Cloud Shell from your Cloud Dashboard where the OCI-CLI is already setup and ready to go.

If you’d rather use your own environment you can follow these instructions to install and configure the OCI-CLI.

Environment Variables

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.

Preset Values

Create environment variables for the following:

  • The name of the Compartment you want to create your new Compute instance in.
  • The name for your new Compute instance.
  • The shape you want to use.  VM.Standard.E2.1.Micro is used for an Always-Fee Compute instance.
  • The absolute path for your user’s home directory.
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.

For example:

Compartment OCID

The previous post demonstrates how to use the --query  parameter to get the OCID for the Compartment.

Availability Domain

You’ll need to define which Availability Domain you want to use.  The above Compute Shape is typically available in your third sub-domain, ‘xxxx:US-ASHBURN-AD-3’.

The following --query  parameter for the list command 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.

Create a Virtual Cloud Network

Your Compute instance will need a VCN 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 create a new one.

Create a new VCN
Add a Subnet to the VCN

Create a new subnet that your compute instance will use for connections.

Add an Internet Gateway

Create an Internet Gateway that will be used to connect from your VCN to the internet.

Add a Route Table

Create a Route Table which is a collection of rules used to route packets to the correct network entity.

Add a Route Rule for the Internet Gateway

Update your Route Table and add a rule granting internet access to your compute instance through your Internet Gateway.


In order to connect to your compute instance you’ll need an RSA key pair.

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.  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.

Now that you have created a network and a key pair you can

Create the Compute Instance

If you created your key pair with a different name or in a different location than ${USER_HOME}/.ssh/ , you will need to modify the --ssh-authorized-keys-filevalue below when you launch your new instance.

Most of the parameters use the values we defined above.

The parameter --image-id is the OCID for the Oracle Linux 7.8 image.

The parameter --wait-for-state RUNNING  will pause at the launch command until your instance is fully up and running.

Get the Public IP

You will need the public ip for your instance in order to establish an ssh connection.


Use ssh to connect to the new instance.

If you changed the location or the name of your private key you may need to include the private key.


At this point you can start configuring your new Compute instance however you’d like.

Leave a comment if something goes wrong or if you have any questions.

Leave a Reply

Your email address will not be published. Required fields are marked *