1. Getting Started with Oracle Cloud Infrastructure

Jeff Davies
Aug 1, 2019 · 8 min read
Image for post
Image for post

Getting started with Oracle Cloud Infrastructure (OCI) can be daunting. It has a ton of functionality, allowing you to create anything you want on the cloud. However, all of that power and functionality can be intimidating when you are just starting on the OCI learning curve.

In this series of articles, I will walk you through some common uses for OCI. In each article, we will have a specific goal in mind, and then achieve that goal by following the detailed steps. Before long, you will be able to solve all sorts of programming/networking/web challenges using OCI.

But I don’t have access to OCI!

No Powerpoint Here

Dude, Just Get To It!

The main OCI dashboard in your browser.
The main OCI dashboard in your browser.

Click on the Create VM Instance box. You will be prompted to select the compartment for the compute instance. You will only have one at this point: the root compartment. I’ll go over compartments in the future, for now, just select the one and only compartment available to you and press the Confirm button.

Select the one and only compartment available to you.
Select the one and only compartment available to you.

Now you will fill in the additional details needed to create the compute instance. First you need to name your compute instance. I named mine “webserver1” to capture what that instance does. By default, an availability domain will be selected for you and the default image of the server will be Oracle Linux 7.6 (or later, depending on when you’re reading this article). The default instance type will be a Virtual Machine.

Image for post
Image for post

Scroll down and then accept the default shape. The “shape” is the computing power you want to use. By default, the smallest is selected and that’s exactly what we want. Remember, you can select a beefier shape, but it will be more expensive to run. Since we are in a trial account, we want to minimize costs and not waste our trial credits on computing power that wont ever be used.

Also, don’t make any changes to the boot volume for the purposes of this article.

Select the shape of the compute instance.
Select the shape of the compute instance.

Scrolling further down, you need to select your SSH key so that you can later SSH into the compute instance. If you don’t have an SSH key that you can use, just search for “SSH Key Generation [PLATFORM]” and follow the instructions specific to your platform. Keep track of the keys you create! You will need it to connect to your instance via SSH.

In the final steps, you will have to provide a name for a new Virtual Cloud Network (VCN). Think of a VCN as a container for your compute instances and other resources. Your VCN will ultimately contain a variety of things, but let’s keep it simple for now. Enter any name you like. I used the name trial_vcn for this series of articles.

Name your new virtual cloud network
Name your new virtual cloud network

Click the Show Advanced Options if you’re curious about what else you can specify. I won’t bother explaining all of the advanced options in this article. When you’re ready, press the Create button to create your compute instance.

This will display the dashboard for your compute instance. You can see by the orange square in the top left corner of the page that the instance is still in the Provisioning state. You can also see some other useful information about the instance.

Image for post
Image for post

It will take approximately 5 minutes or less for the instance to finish provisioning. When the status changes to the green Running icon, the instance is ready for use.

Image for post
Image for post

Now we need to deploy some software onto our instance to make it useful. To do that we will need to SSH into our instance using the public IP address of the compute instance (I have redacted it in the screenshot above). Using your favorite SSH tool (I’m using the command line on my Mac) run the following command:

ssh -i ~/oci_key opc@[Public IP]

Be sure to substitute in the Public IP Address of your compute instance. Also, as you can see from the command, I have the private SSH key located in my home directory and we are logging in as the opc user. This is the main user account on your compute instance and essentially has root privileges.

Let’s Install Node.js

At the time of this writing the latest version of Node is v12.7.0. So the command I used to download it onto my compute instance was:

curl https://nodejs.org/dist/latest/node-v12.7.0-linux-x64.tar.xz > nodejs.tar.xz

This image of Node is only about 13MB and the download is almost instantaneous.

Image for post
Image for post

Now that we have Node on our compute instance, we install it with the following command.

tar -xvf nodejs.tar.xz

We add Node to our PATH variable by opening our .bash_profile file and modifying the line that defines the PATH variable. I use vi as my editor since it’s built into linux.

vi .bash_profile

My modified PATH variable in my .bash_profile is

PATH=$PATH:$HOME/.local/bin:$HOME/bin:$HOME/node-v12.7.0-linux-x64/bin

Back in your SSH console, you can activate your changes to your .bash_profile with the following command:

source ~/.bash_profile

Use the following command to make sure your PATH is correct by testing the node command.

node -v

Create the Web Server

mkdir helloserver
cd helloserver
npm init

Accept the defaults for everything for which npm prompts you. Since the default name for the main file is index.js, we need to create that file. Again I used vi and wrote the following code:

const http = require(‘http’);
hostname = ‘0.0.0.0’;
const port = 3000;
const server = http.createServer((req, res) => {
var currentDate = new Date();
var utcDate = currentDate.toUTCString();
res.statusCode = 200;
res.setHeader(‘Content-Type’, ‘text/plain’);
if(req.method === ‘GET’ && req.url === ‘/greeting’) {
// This is the Hello World invocation
res.end(‘Hello World\n’);
console.log(‘Hello World invoked ‘ + utcDate);
} else {
// This is the health check from the load balancer
res.end(‘Healthy\n’);
//console.log(`Health check `+ utcDate);
}
});
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});

Now run the code with the following command:

node index.js

If you typed in everything correctly, the index.js program should be running and listening to port 3000. Let’s test it to be sure. Open another console and create a second SSH connection into your compute instance.

curl http://0.0.0.0:3000

And you should get the response Healthy. However, all is not well yet. If you try the curl command using your Public IP address, it will timeout. Why does it work from 0.0.0.0 and not from the public IP address? Because there is a firewall that doesn’t allow traffic to port 3000 by default. By default, the firewall only allows traffic through for SSH. So we need to configure the firewall to allow for traffic across port 3000.

Configuring the Firewall

sudo firewall-cmd --permanent --add-port=3000/tcp
sudo firewall-cmd --reload

Note the double dashes. However, this isn’t quite enough. If you try to hit port 3000 using the public IP address, it will still time out. There is one more thing we need to do to route traffic to our web server on OCI.

Create a Stateless Ingress Rule

You can navigate to the security lists for your VCN by clicking on the “hamburger” icon in the top left corner of the browser screen and then selecting Networking -> Virtual Cloud Networks from the menu.

Using the menu to navigate to the VCN page
Using the menu to navigate to the VCN page

Click on the name of your VCN (you will likely have only 1). Under the Resources section on the left side of the browser, click on the Security Lists link.

Image for post
Image for post
Navigate to the Security Lists

Click on the default security list (again, you should only have 1 if you are following this article using a trial account). When the page loads, click on the blue Add Ingress Rule button. Fill in the ingress rule as shown in the next image.

Image for post
Image for post
Creating the ingress rule

This rule simply tells the VCN that all TCP requests from any IP address that are directed to port 3000 are allowed. For a more in depth description of Security Rules, read this article.

Summary

We’ve just scratched the surface of OCI. In future articles we will look into scaling this web server across multiple compute instances, load balancing, creating databases to support our web server, and much more. I hope this gives you a taste for OCI and what it can do. You can read much more about OCI at the official site.

Oracle Groundbreakers

Aggregation of articles from Oracle engineers…

Jeff Davies

Written by

Long time software engineer, software architect, technical evangelist and motorcycle enthusiast.

Oracle Groundbreakers

Aggregation of articles from Oracle engineers, Groundbreaker Ambassadors, ACEs, and the developer community on all things Oracle Cloud. The views expressed are those of the authors and not necessarily of Oracle.

Jeff Davies

Written by

Long time software engineer, software architect, technical evangelist and motorcycle enthusiast.

Oracle Groundbreakers

Aggregation of articles from Oracle engineers, Groundbreaker Ambassadors, ACEs, and the developer community on all things Oracle Cloud. The views expressed are those of the authors and not necessarily of Oracle.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store