Functions QuickStart on Local Host Using Archives (Code-only Functions)

Find out how to get set up and running quickly on a local host by creating, deploying, and invoking a code-only function in OCI Functions.

Note

This QuickStart creates a code-only function using the Fn Project CLI. To create an image-based function by building and publishing a container image, see Creating, Deploying, and Invoking an Image-Based Helloworld Function.

A. Set up your tenancy

1. Create groups and users

If suitable users and groups don't exist already:

  1. Sign in to the Console as a tenancy administrator.
  2. Open the navigation menu  and select Identity & Security. Under Identity, select Domains.
  3. Select a domain, and select the User management tab.
  4. Create a new group by selecting Create group in the Groups section.
  5. Create a new user by selecting Create in the Users section.
  6. Add a user to a group by selecting the name of the group, and then Assign user to group on the Users tab.

See Configuration Notes for more information.

2. Create compartment

If a suitable compartment in which to create network resources and OCI Functions resources doesn't exist already:

  1. Sign in to the Console as a tenancy administrator.
  2. Open the navigation menu  and select Identity & Security. Under Identity, select Compartments.
  3. Select Create Compartment.

See Configuration Notes for more information.

3. Create VCN and subnets

If a suitable VCN in which to create network resources doesn't exist already:

  1. Sign in to the Console as a tenancy administrator.
  2. Open the navigation menu , select Networking, and then select Virtual cloud networks.
  3. Select Start VCN Wizard from the Actions menu to create a new VCN.
  4. In the Start VCN Wizard panel, select Create VCN with Internet Connectivity and select Start VCN Wizard.
  5. Enter a name for the new VCN, select Next, and then select Create to create the VCN along with the related network resources.

See Configuration Notes for more information.

4. Create policy for group and service

If one or more OCI Functions users is not a tenancy administrator:

  1. Sign in to the Console as a tenancy administrator.
  2. Open the navigation menu  and select Identity & Security. Under Identity, select Policies.
  3. Select Create Policy, specify a name and description for the new policy, and select the tenancy's root compartment.

  4. Use the Policy Builder to create the policy. Select Functions from the list of Policy use cases, and base the policy on the policy template Let users create, deploy, and manage functions and applications.

    The policy template includes the following policy statements:

    • Allow group <group-name> to use cloud-shell in tenancy
    • Allow group <group-name> to manage repos in tenancy
    • Allow group <group-name> to read objectstorage-namespaces in tenancy
    • Allow group <group-name> to manage logging-family in tenancy
    • Allow group <group-name> to read metrics in tenancy
    • Allow group <group-name> to manage functions-family in tenancy
    • Allow group <group-name> to use virtual-network-family in tenancy
    • Allow group <group-name> to use apm-domains in tenancy
    • Allow group <group-name> to read vaults in tenancy
    • Allow group <group-name> to use keys in tenancy
    • Allow service faas to use apm-domains in tenancy
    • Allow service faas to {KEY_READ} in tenancy where request.operation='GetKeyVersion'

    If necessary, you can restrict these policy statements by compartment.

    If a required resource, such as an encryption key, is in a different tenancy, use cross-tenancy resource principal policies.

See Configuration Notes for more information.

B. Create application

1. Create your first application
  1. Sign in to the Console as a functions developer.
  2. Open the navigation menu  and select Developer Services. Under Functions, select Applications.
  3. Select the region you're using with OCI Functions.
  4. Select Create application.
  5. Specify:
    • helloworld-app as the name for the new application. You'll deploy your first function in this application, and specify this application when invoking the function.
    • The VCN and subnet in which to run the function. Note that a public subnet requires an internet gateway in the VCN, and a private subnet requires a service gateway in the VCN.
  6. Select Create.

See detailed instructions for more information.

C. Set up your local host dev environment

1. Set up API signing key and OCI profile
  1. Sign in to the Console as a functions developer.
  2. In the navigation menu , select the Profile menu Profile menu icon and then select User settings.
  3. Select the Tokens and keys tab, go to the API keys section, and select Add API key.
  4. Select Generate API key pair in the Add API key panel.
  5. Select Download private key and save the private key file (as a .pem file) in the ~/.oci directory. (If the ~/.oci directory doesn't already exist, create it now).
  6. Select Add to add the new API signing key to your user settings.

    The Configuration File Preview dialog is displayed, containing a configuration file snippet with basic authentication information for a profile named DEFAULT (including the fingerprint of the API signing key you just created).

  7. Copy the configuration file snippet shown in the text box, and close the Configuration File Preview dialog.
  8. In a text editor, open the ~/.oci/config file and paste the snippet into the file. (If the ~/.oci/config file doesn't already exist, create it now).
  9. In the text editor, change the profile in the snippet you've just pasted, as follows:
    • Change the name of the profile from [DEFAULT] to a name of your choosing (for example, [functions-developer-profile]). Note that the ~/.oci/config file cannot contain two profiles with the same name.
    • Change the value of the key_file parameter of the profile to specify the path of the private key file (the .pem file) you downloaded earlier.
  10. In the text editor, save the changes you've made to the ~/.oci/config file, and close the text editor.
  11. In a terminal window, change permissions on the private key file (the .pem file) to ensure that only you can read it, by entering:
    chmod go-rwx ~/.oci/<private-key-file-name>.pem

See Configuration Notes for more information about setting up an API signing key and creating a profile.

2. Install Fn Project CLI

In a terminal window in your development environment:

  1. Install the Fn Project CLI using the appropriate instructions below for your environment:
  2. Confirm that the Fn Project CLI has been installed by entering:
    fn version

See Configuration Notes for more information.

3. Set up Fn Project CLI context provider --oracle

In a terminal window in your development environment:

  1. Create a new Fn Project CLI context by entering:
    fn create context <my-context> --provider oracle

    Note that you specify --provider oracle to enable authentication and authorization using Oracle Cloud Infrastructure request signing, private keys, user groups, and policies that grant permissions to those user groups.

  2. Specify that the Fn Project CLI is to use the new context by entering:
    fn use context <my-context>
  3. Configure the new Fn Project CLI context with the name of the OCI profile you've created for use with OCI Functions (for example, [functions-developer-profile]), by entering:
    fn update context oracle.profile <profile-name>

See Configuration Notes for more information.

4. Complete Fn Project CLI context configuration

In a terminal window in your development environment:

  1. Configure the new Fn Project CLI context with the OCID of the compartment you want to own deployed functions
    fn update context oracle.compartment-id <compartment-ocid>
  2. Configure the new context with the api-url endpoint to use when calling the OCI API by entering:
    fn update context api-url <api-endpoint>

    where <api-endpoint> is one of the endpoints in the list of Functions endpoints in the Functions API, in the format https://functions.<region-identifier>.oci.oraclecloud.com. For example:

    fn update context api-url https://functions.us-phoenix-1.oci.oraclecloud.com

See Configuration Notes for more information.

D. Create, deploy, and invoke your code-only function

1. Create your first code-only function

In the terminal window:

  1. Create a helloworld Java function by entering:

    fn init --code-only --runtime-name java21.ol9 --runtime-config-type function-update hello-java

    A directory called hello-java is created, containing:

    • a function definition file called func.yaml
    • a /src directory containing source files and directories for the helloworld function
    • a Maven configuration file called pom.xml that specifies the dependencies required to compile the function

    Java is just one of several supported languages.

    See detailed instructions for more information.

2. Deploy your first code-only function

In the terminal window:

  1. Change directory to the hello-java directory created in the previous step:
    cd hello-java
  2. Enter the following Fn Project command to build the function and its dependencies:
    fn build
  3. Enter the following Fn Project command to deploy the function to OCI Functions in the helloworld-app application that you created earlier:
    fn -v deploy --app helloworld-app
  4. (Optional) Confirm that the function has been deployed to OCI Functions by selecting the Functions tab (on the details page for the helloworld-app application) and noting that the hello-java function now appears.

See detailed instructions for more information.

3. Invoke your first function

In the terminal window:

  1. Invoke the hello-java function by entering:

    fn invoke helloworld-app hello-java

    The 'Hello world!' message is displayed.

  2. Invoke the hello-java function with the parameter 'John' by entering:

    echo -n 'John' | fn invoke helloworld-app hello-java

    The 'Hello John!' message is displayed.

Congratulations! You've just created, deployed, and invoked your first function using OCI Functions!

See detailed instructions for more information.

4. Next steps

Now that you've created, deployed, and invoked a function, learn how to:

You're done!