Functions: Get Started using the CLI
In this tutorial, you use an Oracle Cloud Infrastructure account to set up Oracle Functions development. Then, you create a function application and a function.
Key tasks include how to:
- Set up an authentication token.
- Gather required information.
- Set up a VCN.
- Set up the OCI Registry (OCIR).
- Set up the CLI to deploy functions
- Configure your Fn context.
- Create an app for your Oracle function.
- Create a function.
- Deploy your function.
- Test your function.
For additional information, see:
Before You Begin
To successfully perform this tutorial, you must have the following:
- A paid Oracle Cloud Infrastructure account. See Signing Up for Oracle Cloud Infrastructure.
- Your OCI account configured to support Oracle Functions development. See Create Policy for Oracle Functions.
- Python 3.6+ and pip3.
- Docker Engine: A Linux computer or Linux VM. See Docker engine requirements for versions, and distros supported.
- Docker Desktop: Available for MacOS or Windows 10.
- Windows 10: Windows 10 update 2004 with WSL 2 and Ubuntu or other distro installed.
- See Windows Subsystem for Linux Installation Guide for Windows 10.
- Install Docker Desktop for Windows 10.
Note
Docker includes special Linux support for WSL 2 on Windows 10 update 2004. - MacOS: See Install Docker Desktop for MacOS.
- Windows 10: Windows 10 update 2004 with WSL 2 and Ubuntu or other distro installed.
1. Gather Required Information
Collect all the information you need to complete the tutorial.
Prepare the information you need from the OCI Console.
- Find your region identifier and region key from Regions and Availability Domains.
Example:
us-ashburn-1
andiad
for Ashburn. - Create a registry project name to store your function images in OCI Registry (OCIR).
When you publish a function, a Docker image is created in OCIR. Your OCIR project name is prepended to your function images to make them easy to find. For example, given:
- Registry project name:
my-func-prj
- Function name:
node-func
Your function image would be stored on OCIR under:
my-func-prj/node-func
- Registry project name:
To create a compartment see Create a compartment. After your compartment is created, save the compartment OCID.
To get the compartment OCID from an existing compartment:
- Open the navigation menu and click Identity & Security. Under Identity, click Compartments.
- Select your compartment.
- Click the Copy link for the OCID field.
You create an authorization token to log in to the OCI Registry. To create an authorization token:
Collect all the information needed to complete the tutorial. Copy the following information into your notepad.
2. Create your Virtual Cloud Network (VCN)
Set up a VCN to connect your Linux instance to the internet. You configure all the components needed to create your virtual network.
- Click the Oracle Cloud icon to go to the main landing
page.
- Scroll down to Launch Resources.
- Select Set up a network with a wizard.
- In the Start VCN Wizard workflow, select Create VCN with Internet Connectivity and then click Start VCN Wizard .
- In the configuration dialog, fill in the VCN Name for your VCN. Your
Compartment is already set to the last compartment you were working in, or
defaults to your
<your-tenancy> (root)
. - In the Configure VCN and Subnets section, keep the default values for the
CIDR blocks:
- VCN CIDR BLOCK: 10.0.0.0/16
- PUBLIC SUBNET CIDR BLOCK: 10.0.0.0/24
- PRIVATE SUBNET CIDR BLOCK: 10.0.1.0/24
Note
Notice the public and private subnets have different network addresses. - For DNS Resolution, uncheck Use DNS hostnames in this VCN.
- Click Next.
The Create a VCN with Internet Connectivity configuration dialog is displayed (not shown here) confirming all the values you just entered.
- To create your VCN, click Create.
The Creating Resources dialog is displayed (not shown here) showing all VCN components being created.
- Click View Virtual Cloud Network to view your new VCN.
Your new VCN is displayed. Now you need to add a security rule to allow HTTP connections on port 80, the default port for your applications.
- With your new VCN displayed, click your Public subnet link.
The public subnet information is displayed with the Security Lists at the bottom of the page. A link to the Default Security List for your VCN is displayed.
- Click the Default Security List link.
The default Ingress Rules for your VCN are displayed.
- Click Add Ingress Rules.
An Add Ingress Rules dialog is displayed.
- Fill in the ingress rule with the following information. After all the data is
entered, click Add Ingress Rules
Fill in the ingress rule as follows:
- Stateless: Checked
- Source Type: CIDR
- Source CIDR: 0.0.0.0/0
- IP Protocol: TCP
- Source Port Range: (leave-blank)
- Destination Port Range: 80
- Description: VCN for applications
After you click Add Ingress Rule, HTTP connections are allowed to your public subnet.
To open a different port, replace 80 in the last step with the port number.
You have successfully created a VCN that makes your applications available from the internet.
3. Set up OCI Command Line Interface
To develop functions on your local machine, you must set up the OCI Command Line
Interface (CLI). This section assumes you have already installed Docker and Python 3.6+ and
pip3
.
Complete the following three sections to enable Oracle Functions development on your local machine with the CLI. For a detailed explanation of each step, see Set up Oracle Functions in a Local Dev Environment.
Setting up the CLI allows you to deploy your functions and function application to OCI Registry from your machine. First, install the CLI using Python's virtual environment feature.
Set up the CLI so it can connect to OCI services.
To do function development on your local machine, you need to install the Fn client. Fn allows you to create functions, create applications, and deploy functions to the OCI Registry.
4. Prepare the OCI Registry for Functions
Next, you log Docker into the OCI Registry (OCIR).
- Get the information you gathered earlier.
- Open a terminal window.
- Log in to OCIR:
docker login <region-key>.ocir.io
You are prompted for your login name and password.
- Username:
<tenancy-name>/<user-name>
- Password:
<auth-token>
- Username:
You have logged your instance into OCIR.
5. Configure Functions
To use Oracle Functions, you must configure the Fn application context. The context stores the values needed to connect to the Oracle Functions service.
Use the information you gathered previously for your configuration values.
If you use multiple profiles for the CLI, you need to set a
oracle.profile
value.
Example: fn update context oracle.profile <profile-name>
You have configured Fn to connect to the Oracle Functions service.
View/Edit your Context
Your Fn context files are in the ~/.fn/contexts
directory. Each context is stored in a .yaml
file. For example,
your us-phoenix-1.yaml
file might look similar to:
api-url: https://functions.us-phoenix-1.oci.oraclecloud.com
oracle.compartment-id: ocid1.compartment.oc1..aaaaaaa...
provider: oracle
registry: phx.ocir.io/my-tenancy/myproject/repo
You can edit the file directly with an editor if necessary.
For a detailed explanation of each step, see: Oracle Functions in a Local Dev Environment Quickstart
6. Create and Deploy a Function
With your configuration complete, create and deploy a function.
An Application is the main storage container for functions. Each function must have an application for deployment. To create application, follow these steps.
- Open the navigation menu and click Developer Services. Under Functions, click Applications.
- Click Create Application.
Fill in the form data.
- Name:
<your-app-name>
- VCN:
<your-VCN>
- Subnets:
<your-public-subnet>
or<your-private-subnet>
Note
A public or private subnet may be used, select one. - Name:
- Click Create.
Your app is created.
Select one of the following languages to create and deploy a function. If you want, you can do all three.
With your application created, deploy a Java function. Follow these steps to create a Java "Hello World" function.
Ensure Java 8+ is installed to perform these steps.
You have successfully deployed and tested a Java function.
With your application created, deploy a Python function. Follow these steps to create a Python "Hello World" function.
You have successfully deployed and tested a Python function.
With your application created, deploy a Node function. Follow these steps to create a Node "Hello World" function.
Ensure Node.js 10+ is installed to perform these steps.
You have successfully deployed and tested a Node function.
7. Review Function Information
After your functions run, information about your functions is available in the OCI Console.
When you deploy, the function is uploaded and stored in OCIR. You can navigate to OCIR and examine the function images.
- Open the navigation menu and click Developer Services. Under Containers & Artifacts, click Container Registry.
- Search for the
<your-repository-project-name>
. - Under your project name, you see an entry for each function you deployed.
- Click the link of each image you want to see information about.
After you run a function, you can display metrics for that function.
- Open the navigation menu and click Developer Services. Under Functions, click Applications.
Your applications are listed on the page.
- Click the link to the application you created.
- Click the link to the function you want to examine.
Metric information about your function is displayed.
To enable, logging for an application, follow these steps.
- Open the navigation menu and click Developer Services. Under Functions, click Applications.
- Click the link to the application you created.
- On the left side of the application page, click the Logs link.
- Click Disabled to enable logging for your application.
- The Enable Log dialog is displayed. Fill in the following information:
- Compartment: <your-compartment-name>
- Log Group: Take the default value
Auto-Create a Default Log Group
- Log name: <take-default>
- Log Retention: <take-default>
- Click Enable Log
Wait a moment for your log to be created.
To view your log, click the log name link created by the preceding steps.
What's Next
You have successfully created a function and deployed it to Oracle Functions.
To explore more information about development with Oracle products, check out these sites: