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.
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 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 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.
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 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
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 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 an Internet Gateway 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 a Route Table 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 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.
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 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.
Top comments (0)