X

Best Practices from Oracle Development's A‑Team

Jumpstart your Functions-as-a-Service journey in OCI with Cloud Shell

Oracle Functions is a comprehensive Functions-as-a-Service platform based on the open source Fn Project. Although Oracle Functions provides a rich feature set, setting up a development environment involves multiple configuration steps. These include OCI CLI, Fn Project CLI, and Docker installation, which can be time consuming and challenging for a user new to function development and looking to quickly bootstrap.

Enter Cloud Shell! Cloud Shell provides a Linux terminal pre-loaded with lots of goodies including a pre-authenticated OCI CLI, Git, Python, etc. However, until now it did not include integration with Oracle Functions. OCI now has rolled in support for Oracle Functions into Cloud Shell making onboarding of a new user into function development a breeze. The blog details the steps in using Cloud Shell for deploying and invoking functions.

Along with the Functions support in Cloud Shell, sample functions are also made available in GitHub. A user can easily download these samples, build, deploy and invoke these samples, all within the Cloud Shell environment.

Trying out the samples

Ok, so you want to try out the “Hello World” Python sample from the GitHub using Cloud Shell. To start with, create an application, say Oracle_Functions_Sample which will hold your functions. 

Next, deploy the function from CloudShell according to these steps. The output on the Cloud Shell is the following:

Broadly speaking, deployment has the following steps:

  1.  Fn Project CLI "fn deploy" starts the process.
  2. Builds a Docker image (along with some meta data)
  3. Pushes the Docker image to the Docker Registry.
  4. Adds helloworld function to Oracle_Functions_Sample application.

Refer to this documentation for more information.

In the above steps, neither the Fn Project CLI nor Docker were required to be installed. They are bundled in the Cloud Shell thus speeding up the development. After deployment, the Application page will now list the helloworld function:

 

That’s good news! Next, you invoke the function and get the following:

The function outputs “Hello World”. You are delighted! You now want to try out the sample oci-list-instances-python. This function lists all instances in your compartment. Switching to oci-list-instances-python directory in Cloud Shell, deploying and invoking the function results in the following:

In the above example, the compartment has only one instance whose ocid is listed. The function is now listed under the application:

Next, you would like to go beyond the samples. You decide to modify the function: list instances with the regions where they are hosted. You make changes to the func.py using Vim editor, adding region as one of the attributes to be listed:

Deploy the function and invoke it again:

Check out that the region - ca-montreal-1 - gets listed along with instance’s ocid.

What's Next

So far, so good! You started the journey with the “Hello World” function, then ventured out trying the list-instances function, modifying it along the way. You would like to do more, possibly build on the changes you made namely, adding the region attribute to the list. You work on other samples, tuning them suitably for your needs. You realize the potential of using these samples enhanced with your modifications, for some real use cases in your organization. However, you soon experience a hindrance; advance development is not easy in Cloud Shell. Vim is a good editor but is not an IDE. Also, you may need other dev tools not available in Cloud Shell. And as part of the development, you would like to hook into your organization's CI/CD process. In other words, there arises a necessity to move this source code out of the OCI realm and into your organization’s domain. You may be required to place this source code into your company’s private repository.

As much as the Cloud Shell helps in quick onboarding into the FaaS world, it is not a substitute for formal development. You still require a setup in your local dev environment. But how do you move your source code from Cloud Shell to your local dev environment? Use Object Storage! Upload your source code into an Object Storage bucket and download the same into your dev environment. To upload the file, use OCI CLI from the Cloud Shell:

In the above command, the file func.py is uploaded onto the bucket Functions. This file had the changes about including region in the list of instances. 

In the above figure, the bucket shows the uploaded file func.py. Download it into your local dev environment to continue further development. However, before downloading func.py you need to set up your dev environment for Functions development. The steps are given here. And then (git) clone the samples directory from GitHub onto your local env. For example, the following figure shows a local environment after Fn CLI setup and cloning of the samples.

 As shown in the above figure, we are in the oci-list-instances-python directory. If we check the contents of func.py, we will not find our change:  region added to the list of attributes. That's hardly surprising since our changes are lying in the Object Storage. Downloading func.py, you will now see your change as shown below.

You can continue further development:  modifying the files, integrating with other dev tools, pushing the source into your repository, etc. 

Using OCI Object Storage is not the only way to transfer files from Cloud Shell to your local dev environment. One possibility could be using GitHub. But that could require traversing the internet, which may or may not be acceptable to you or your organization.

Summary

In this blog we saw how easy it is to start working with functions in OCI using Cloud Shell. We checked few samples in GitHub, modified and enhanced them, and finally looked into a solution to transfer the changes into our local dev environment. FaaS is exciting and OCI Cloud Shell greatly simplifies onboarding. So, what are you waiting for? Get started today and enjoy the journey into the new world of FaaS with OCI Functions!

Be the first to comment

Comments ( 0 )
Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.Captcha