Create and use an Oracle Cloud Compute instance from the Command Line
Tags: autonomous, database, cloud, tools
Published: 2020-06-25 13:51:19

---
title: Create and use an Oracle Cloud Compute instance from the Command Line
published: true
date: 2020-06-18 21:44:56 UTC
tags: Autonomous,Database,Cloud,Tools
canonical_url: https://learncodeshare.net/2020/06/18/create-and-use-an-oracle-cloud-compute-instance-from-the-command-line/
series: Oracle Cloud CLI
---

In [a previous post](https://learncodeshare.net/2020/04/06/create-and-use-an-oracle-autonomous-cloud-database-from-the-command-line/), 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](https://docs.cloud.oracle.com/en-us/iaas/Content/API/Concepts/cloudshellintro.htm) 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](https://learncodeshare.net/2020/03/06/install-and-configure-the-oracle-cloud-command-line-interface/) 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](https://docs.cloud.oracle.com/en-us/iaas/Content/Compute/References/computeshapes.htm) 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.

```
export COMPARTMENT\_NAME='Test'export COMPUTE\_NAME='TestCompute'export COMPUTE\_SHAPE='VM.Standard.E2.1.Micro'export USER\_HOME=$(eval echo ~)
```
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:

```
export NEW\_OCID=$(the OCI command you would run to create the object and return the OCID)
```

##### Compartment OCID

[The previous post](https://learncodeshare.net/2020/04/06/create-and-use-an-oracle-autonomous-cloud-database-from-the-command-line/#GetCompartment) demonstrates how to use the --query  parameter to get the OCID for the Compartment.

```
export COMPARTMENT\_ID=$(oci iam compartment list --query "data[?name=='${COMPARTMENT\_NAME}'].id | [0]" --raw-output)
```

##### Availability Domain

You’ll need to define which [Availability Domain](https://docs.cloud.oracle.com/en-us/iaas/Content/General/Concepts/regions.htm) 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](https://docs.cloud.oracle.com/en-us/iaas/tools/oci-cli/latest/oci_cli_docs/cmdref/iam/availability-domain/list.html) 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.

```
export AVAILABILITY\_DOMAIN=$(oci iam availability-domain list --query "(data[?ends\_with(name, '-3')] | [0].name) || data[0].name" --raw-output)
```

### Create a Virtual Cloud Network

Your Compute instance will need a [VCN](https://docs.cloud.oracle.com/en-us/iaas/Content/Network/Tasks/managingVCNs.htm) 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](https://docs.cloud.oracle.com/en-us/iaas/tools/oci-cli/latest/oci_cli_docs/cmdref/network/vcn/create.html).

##### Create a new VCN

```
export VCN\_ID=$(oci network vcn create -c ${COMPARTMENT\_ID} --cidr-block "10.0.0.0/16" --query "data.id" --raw-output)
```

##### Add a Subnet to the VCN

[Create a new subnet](https://docs.cloud.oracle.com/en-us/iaas/tools/oci-cli/latest/oci_cli_docs/cmdref/network/subnet/create.html) that your compute instance will use for connections.

```
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)
```

##### Add an Internet Gateway

[Create](https://docs.cloud.oracle.com/en-us/iaas/tools/oci-cli/latest/oci_cli_docs/cmdref/network/internet-gateway/create.html) an [Internet Gateway](https://docs.cloud.oracle.com/en-us/iaas/Content/Network/Tasks/managingIGs.htm) that will be used to connect from your VCN to the internet.

```
export IG\_ID=$(oci network internet-gateway create -c ${COMPARTMENT\_ID} --is-enabled true --vcn-id ${VCN\_ID} --query "data.id" --raw-output)
```

##### Add a Route Table

[Create](https://docs.cloud.oracle.com/en-us/iaas/tools/oci-cli/latest/oci_cli_docs/cmdref/network/route-table/create.html) a [Route Table](https://docs.cloud.oracle.com/en-us/iaas/Content/Network/Tasks/managingroutetables.htm) which is a collection of rules used to route packets to the correct network entity.

```
export RT\_ID=$(oci network route-table list -c ${COMPARTMENT\_ID} --vcn-id ${VCN\_ID} --query "data[0].id" --raw-output)
```

##### Add a Route Rule for the Internet Gateway

[Update](https://docs.cloud.oracle.com/en-us/iaas/tools/oci-cli/latest/oci_cli_docs/cmdref/network/route-table/update.html) your Route Table and add a rule granting internet access to your compute instance through your Internet Gateway.

```
oci network route-table update --rt-id ${RT\_ID} --route-rules '[{"cidrBlock":"0.0.0.0/0","networkEntityId":"'${IG\_ID}'"}]' --force
```

### RSA Key

In order to connect to your compute instance you’ll need an [RSA key pair](https://docs.cloud.oracle.com/en-us/iaas/Content/Compute/Tasks/managingkeypairs.htm).

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.

```
ssh-keygen -t rsa -N "" -b 2048 -C "CiCd-Compute-Instance" -f ${USER\_HOME}/.ssh/id\_rsa
```

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/id\_rsa.pub , you will need to modify the --ssh-authorized-keys-filevalue below when you [launch](https://docs.cloud.oracle.com/en-us/iaas/tools/oci-cli/latest/oci_cli_docs/cmdref/compute/instance/launch.html) your new instance.

```
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)
```

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.

```
export COMPUTE\_IP=$(oci compute instance list-vnics \ --instance-id "${COMPUTE\_OCID}" \ --query 'data[0]."public-ip"' \ --raw-output)echo $COMPUTE\_IP
```

### Connect

Use ssh to connect to the new instance.

```
ssh opc@${COMPUTE\_IP}
```

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

```
ssh -i /path/to/your/key/privateKeyName opc@${COMPUTE\_IP}
```

### Enjoy

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.