Expert’s Guide to Create and Deploy a Micronaut Gateway Function to Oracle Cloud Infrastructure

This guide describes how to use the Graal Development Kit for Micronaut (GDK) to create a Micronaut® Gateway Function, deploy it as a container image, using Oracle Cloud Infrastructure Functions, and access it via Oracle Cloud Infrastructure API Gateway.

A Micronaut Gateway Function acts a a serverless cloud (HTTP) API gateway function.

Oracle Cloud Infrastructure Functions is a fully managed, multitenant, highly scalable, on-demand, Functions-as-a-Service platform.

The guide consists of the following steps:

  1. Use the GDK Launcher to create a Micronaut Gateway Function
  2. Create the code to implement the gateway function
  3. Deploy the gateway function as a container image to a Container Repository in Oracle Cloud Infrastructure Registry
  4. Create and deploy an Oracle Function based on the container image of the gateway function
  5. Create and deploy an Oracle Cloud Infrastructure API Gateway to access the Oracle Function

This guide assumes you are familiar with the Oracle Cloud Console.

Prerequisites #

Follow the steps below to create a Micronaut gateway function from scratch. However, you can also download the completed example:

A note regarding your development environment

Consider using Visual Studio Code, which provides native support for developing applications with the Graal Development Kit extension.

If you use IntelliJ IDEA, enable annotation processing.

Windows platform: The GDK guides are compatible with Gradle only. Maven support is coming soon.

1. Create the Gateway Function #

This section describes how to create a Micronaut gateway function for a simple online store. The store provides information about available items and enables the user to order items. An HTTP controller is responsible for the API implementation and a service stores the availability of items.

Create a Micronaut gateway function using the GDK Launcher.

  1. Open the GDK Launcher in advanced mode.

  2. Create a new project using the following selections.
    • Project Type: Gateway Function
    • Project Name: oci-serverless-demo
    • Base Package: com.example (Default)
    • Clouds: OCI
    • Build Tool: Gradle (Groovy) or Maven
    • Language: Java (Default)
    • Test Framework: JUnit (Default)
    • Java Version: 17 (Default)
    • Micronaut Version: (Default)
    • Cloud Services: None
    • Features: GraalVM Native Image and Micronaut Validation
    • Sample Code: No
  3. Click Generate Project, then click Download Zip. The GDK Launcher creates a Micronaut gateway function with the default package com.example in a directory named oci-serverless-demo. The application ZIP file will be downloaded to your default downloads directory. Unzip it, open it in your code editor, and proceed to the next steps.

Alternatively, use the GDK CLI as follows:

gdk create-gateway-function com.example.oci-serverless-demo \
    --clouds=oci \
    --features=graalvm,validation \
    --example-code=false \
    --jdk=17 \
    --build=gradle \
    --lang=java
gdk create-gateway-function com.example.oci-serverless-demo \
    --clouds=oci \
    --features=graalvm,validation \
    --example-code=false \
    --jdk=17 \
    --build=maven \
    --lang=java

For more information, see Using the GDK CLI.

The GDK Launcher creates a multi-module project with two subprojects: oci for Oracle Cloud Infrastructure, and lib. You develop the gateway function logic in the oci subproject. If your gateway function is to be deployed to multiple cloud providers, use the lib subproject to create classes that can be shared between the providers. This enables you to separate the code that is different between cloud providers, while keeping most of the implementation in the common lib subproject.

1.1. StoreItem #

Create a StoreItem model to represent an item in the store in the file lib/src/main/java/com/example/StoreItem.java with the following contents:

package com.example;

import io.micronaut.core.annotation.Introspected;
import io.micronaut.core.annotation.NonNull;

import io.micronaut.serde.annotation.Serdeable;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotBlank;

@Serdeable
@Introspected // <1>
public class StoreItem {

    @NotBlank
    @NonNull
    private final String name;

    private final String description;

    @Min(0)
    private int numberInStorage;

    public StoreItem(String name, String description, int numberInStorage) { // <2>
        this.name = name;
        this.description = description;
        this.numberInStorage = numberInStorage;
    }

    public String getName() {
        return name;
    }

    public String getDescription() {
        return description;
    }

    public Integer getNumberInStorage() {
        return numberInStorage;
    }

    public void setNumberInStorage(Integer numberInStorage) {
        this.numberInStorage = numberInStorage;
    }
}

1 The @Introspected annotation enables Micronaut to serialize and deserialize the model from different formats including JSON. This provides the ability to use the type inside HTTP requests or responses.

2 The model has fields to store the item’s name, description, and number available.

1.2. StoreController #

Create an HTTP controller in the file lib/src/main/java/com/example/StoreController.java, as follows:

package com.example;

import io.micronaut.http.HttpResponse;
import io.micronaut.http.annotation.Controller;
import io.micronaut.http.annotation.Get;
import io.micronaut.http.annotation.PathVariable;
import io.micronaut.http.annotation.Post;
import io.micronaut.http.exceptions.HttpStatusException;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;

import java.util.Collection;
import java.util.stream.Collectors;

import static io.micronaut.http.HttpStatus.BAD_REQUEST;
import static io.micronaut.http.HttpStatus.NOT_FOUND;

@Controller("/store") // <1>
class StoreController {

    private final StorageService storageService;

    StoreController(StorageService storageService) { // <2>
        this.storageService = storageService;
    }

    @Get("/all") // <3>
    Collection<StoreItem> listAllItems() {
        return storageService.getItems();
    }

    @Get("/available") // <4>
    Collection<StoreItem> listAvailableItems() {
        return storageService.getItems().stream()
                .filter(i -> i.getNumberInStorage() > 0)
                .collect(Collectors.toList());
    }

    @Post(uri = "/order/{name}/{amount}", consumes = "*/*") // <5>
    HttpResponse<StoreItem> orderItem(@NotBlank @PathVariable String name, @Min(1) int amount) {
        if (storageService.findItem(name).isEmpty()) {
            throw new HttpStatusException(NOT_FOUND, "Item '" + name + "' not found");
        }
        try {
            storageService.orderItem(name, amount);
        } catch (StorageService.StorageException e) {
            throw new HttpStatusException(BAD_REQUEST, "Could not order item '" + name + "'. " + e.getMessage());
        }
        return HttpResponse.ok(storageService.findItem(name).orElse(null));
    }
}

1 The class is defined as a controller with the @Controller annotation mapped to the path /store.

2 Use Micronaut argument injection to inject a StorageService bean by defining it as the constructor argument. You will create the StorageService in next section.

3 The @Get annotation maps the listAllItems method to an HTTP GET request on /store/all.

4 The @Get annotation maps the listAvailableItems method to an HTTP GET request on /store/available.

5 The @Post annotation maps the orderItem method to an HTTP POST request on /store/order/{name}/{amount}. Use the consumes argument to specify which content-types are allowed in the request. Throwing HttpStatusException will set the corresponding HTTP status in the response.

1.3. StorageService #

  1. Create an interface for a service that represents the store’s inventory in lib/src/main/java/com/example/StorageService.java:

     package com.example;
    
     import io.micronaut.core.annotation.NonNull;
    
     import jakarta.validation.constraints.Min;
     import jakarta.validation.constraints.NotBlank;
     import java.util.Collection;
     import java.util.Optional;
    
     public interface StorageService { // <1>
         Collection<StoreItem> getItems();
         Optional<StoreItem> findItem(@NonNull @NotBlank String name);
         void orderItem(@NonNull @NotBlank String name, @Min(1) int amount);
    
         class StorageException extends RuntimeException { // <2>
             StorageException(String message) {
                 super(message);
             }
         }
     }
    

    1 The storage service provides information about all the items, finds an items by its name, and can place an order for an item.

    2 The class includes a custom exception that thrown in case of invalid requests to storage.

  2. Create an implementation of the service interface in lib/src/main/java/com/example/DefaultStorageService.java:

     package com.example;
    
     import io.micronaut.context.annotation.Requires;
     import io.micronaut.core.annotation.NonNull;
     import jakarta.inject.Singleton;
    
     import java.util.ArrayList;
     import java.util.Collection;
     import java.util.List;
     import java.util.Optional;
    
     @Singleton // <1>
     @Requires(missingBeans = StorageService.class) // <2>
     class DefaultStorageService implements StorageService {
    
         protected List<StoreItem> items = List.of( // <3>
             new StoreItem("chair", "A black chair with 4 legs", 10),
             new StoreItem("table", "A quality dining table", 6),
             new StoreItem("sofa", "A gray sofa", 2),
             new StoreItem("bookshelf", "A futuristic-looking bookshelf", 0)
         );
    
         @Override
         public Collection<StoreItem> getItems() {
             return items;
         }
    
         @Override
         public Optional<StoreItem> findItem(@NonNull String name) {
             return items.stream().filter(item -> item.getName().equals(name)).findFirst();
         }
    
         @Override
         public void orderItem(@NonNull String name, int amount) {
             findItem(name).ifPresentOrElse(item -> {
                 if (item.getNumberInStorage() >= amount) {
                     item.setNumberInStorage(item.getNumberInStorage() - amount);
                 } else {
                     throw new StorageException("Insufficient amount in storage");
                 }
             }, () -> { throw new StorageException("Item not found in storage"); });
         }
     }
    

    1 Use jakarta.inject.Singleton to designate a class as a singleton.

    2 The @Requires(missingBeans = StorageService.class) annotation specifies that this implementation should only be used if no other implementations could be found.

    3 The implementation stores the items in a List and populates some sample items in the list.

If you wish to implement a more advanced StorageService to be used instead of this one, annotate your implementation with @Singleton as shown above. Use the @Requires(env = "oraclecloud") annotation to make it specific to Oracle Cloud Infrastructure. Visit the Database Module for details about how to store and manipulate data in a database.

1.4. Tests to Verify Gateway Function Logic #

Create a test class for the controller in oci/src/test/java/com/example/StoreControllerTest.java, as follows:

package com.example;

import io.micronaut.context.annotation.Requires;
import io.micronaut.http.HttpResponse;
import io.micronaut.http.annotation.Get;
import io.micronaut.http.annotation.Post;
import io.micronaut.http.client.annotation.Client;
import io.micronaut.http.client.exceptions.HttpClientResponseException;
import io.micronaut.test.extensions.junit5.annotation.MicronautTest;
import jakarta.inject.Inject;
import jakarta.inject.Singleton;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.List;

import static io.micronaut.http.HttpStatus.BAD_REQUEST;
import static io.micronaut.http.HttpStatus.NOT_FOUND;
import static io.micronaut.http.HttpStatus.OK;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;

@MicronautTest(environments = "test-storage-service") // <1>
class StoreControllerTest {

   @Inject
   StoreClient client;

   @Test
   void testAvailableItems() {
      List<StoreItem> availableItems = client.getAvailable();

      assertEquals(2, availableItems.size());
      assertEquals("pot", availableItems.get(1).getName());
      assertEquals(10, availableItems.get(1).getNumberInStorage());
      assertNotNull(availableItems.get(1).getDescription());
   }

   @Test
   void testNotFoundException() {
      HttpResponse<?> response = client.order("lamp", 1);

      assertEquals(NOT_FOUND, response.getStatus());
   }

   @Test
   void testNotSufficientException() {
      HttpClientResponseException e = assertThrows(HttpClientResponseException.class, () -> {
         client.order("pot", 100);
      });

      assertEquals(BAD_REQUEST, e.getStatus());
   }

   @Test
   void testOrderRequest() {
      StoreItem plate = client.getAll().stream()
              .filter(i -> i.getName().equals("plate"))
              .findFirst().orElse(null);
      assertNotNull(plate);
      assertEquals(100, plate.getNumberInStorage());

      HttpResponse<StoreItem> response = client.order("plate", 10);
      assertEquals(OK, response.getStatus());
      assertNotNull(response.body());
      assertEquals("plate", response.body().getName());
      assertEquals(90, response.body().getNumberInStorage());
   }

   @Singleton
   @Requires(env = "test-storage-service")
   static class TestStorageService extends DefaultStorageService {
      TestStorageService() { // <2>
         items = List.of(
                 new StoreItem("plate", "A large plate", 100),
                 new StoreItem("pot", "A cooking pot", 10),
                 new StoreItem("pan", "A large pan", 0)
         );
      }
   }

   @Client("/store") // <3>
   interface StoreClient {
      @Get("/all") // <4>
      List<StoreItem> getAll();

      @Get("/available") // <4>
      List<StoreItem> getAvailable();

      @Post("/order/{name}/{amount}") // <4>
      HttpResponse<StoreItem> order(String name, Integer amount);
   }
}

1 Annotate the class with @MicronautTest so the Micronaut framework will initialize the application context. This enables you to inject beans using the Jakarta @Inject annotation and to send requests to the StoreController defined in the application. Configure this test to use an identified environment using the @MicronautTest(environments="test-storage-service") annotation.

2 Create a mock implementation of StorageService so that the test is independent of the current state of the storage. The @Requires(env="test-storage-service") annotation specifies that the bean should only be available in the identified environment. (In this case it matches the one identified in the @MicronautTest annotation.)

3 Create a Micronaut Declarative Client with the same /store path to send requests to the controller.

4 Create three tests using the defined client and assuming that TestStorageService is used.

2. Run the Tests #

Run the tests using the following command:

./gradlew test

Then open the file oci/build/reports/tests/test/index.html in a browser to view the results.

./mvnw test

Although you created this gateway function to run on Oracle Cloud Infrastructure, the tests should run successfully on your local machine.

Furthermore, Micronaut has a test implementation that simulates the Oracle Cloud Infrastructure Functions environment. Since sequential requests to a function may be processed by different instances, Micronaut creates a separate environment for each request in a test.

3. Set Up Oracle Cloud Infrastructure #

This guide requires the following Oracle Cloud Infrastructure resources:

This section describes how to create a container image from the function and publish it to Oracle Cloud Infrastructure Registry (also known as Container Registry).

3.1. Authenticate with Container Registry #

Container Registry is an Oracle-managed registry to store, share, and manage container images (such as Docker images).

Before you can publish a container image to Container Registry, you must first authenticate with it. For this, create an authentication token and retrieve a few more properties.

  1. Find out your Object storage namespace by viewing your Tenancy details page. Make a note of it.

    Oracle Cloud Infrastructure Tenancy Details Page

  2. An authentication token is bound to a user, so first select a user. In the Oracle Cloud Console, open the navigation menu, click Identity & Security. Under Identity click Users. Click the name of the user.

  3. Scroll to the bottom of the User Details page, and select Auth tokens in the Resources section. Click Generate token:

    Oracle Cloud Infrastructure Auth Tokens

    • Create a description for the token and click Generate token:

      Oracle Cloud Infrastructure Create Auth Token

    • Click Copy to copy the generated token:

      Oracle Cloud Infrastructure Copy Auth Token

  4. Find out your region identifier. You can see your region on the right of the header in the Oracle Cloud Console. Look for your region identifier in the Oracle Cloud Infrastructure regions documentation page. For example, ca-toronto-1 is for Canada Southeast (Toronto).

  5. Authenticate with docker to Container Registry with your region identifier:

    docker login <region-identifier>.ocir.io
    
    • When asked for a username, provide <object storage namespace>/<username>, for example aaaaaaaaaaaa/example@example.com.

    • When asked for a password, provide the Auth token.

    The command should complete by printing Login Succeeded. (It may take some time before the authentication token activates.)

3.2. Publish a Container Image of the Gateway Function to Container Registry #

To publish a container image to Container Registry, provide the path to a container repository in the build file.

Modify the oci/build.gradle file. Make sure it contains the following contents and that nothing overrides the set properties:


tasks {
    dockerfileNative {
      args("-XX:MaximumHeapSizePercent=80")
    }
    dockerBuild {
        images = ["[REGION].ocir.io/[TENANCY]/gdk-function-demo"]
    }

    dockerBuildNative {
        images = ["[REGION].ocir.io/[TENANCY]/gdk-function-demo"]
    }
}
  • Modify the dockerBuild.images property: enter your region identifier (REGION), and your object storage namespace (TENANCY) correctly. The value of the property should be similar to ca-toronto-1.ocir.io/aaaaaaaaaaaa/gdk-function-demo.
  • Additionally, note that the value of micronaut.runtime is oracle_function.

Save the build file, then run the following command to create and publish a container image to Container Registry:

./gradlew oci:dockerPush

Modify the oci/pom.xml file as follows (and as shown below):

  • Insert the <regionIdentifier>, <objectStorageNamespace>, <jib.docker.image>, and <jib.docker.tag> elements to the <properties> element
  • Replace [REGION IDENTIFIER] and [OBJECT STORAGE NAMESPACE] with their corresponding values from the previous step
    <properties>
        <packaging>jar</packaging>
        ...
        <micronaut.runtime>oracle_function</micronaut.runtime>
        ...
        <regionIdentifier>[REGION IDENTIFIER]</regionIdentifier>
        <objectStorageNamespace>[OBJECT STORAGE NAMESPACE]</objectStorageNamespace>
        <jib.docker.image>${regionIdentifier}.ocir.io/${objectStorageNamespace}/gdk-function-demo</jib.docker.image>
        <jib.docker.tag>${project.version}</jib.docker.tag>
        ...
    </properties>

Note: micronaut.runtime property is already set to oracle_function.

Save the build file, then run the following commands to create and publish a container image to Container Registry:

./mvnw install -pl lib -am
./mvnw deploy -Dpackaging=docker -pl oci

3.3. Create a Virtual Cloud Network #

Create a Virtual Cloud Network for the function.

  1. In the Oracle Cloud Console, open the navigation menu, click Networking. Click Virtual Cloud Networks.

  2. Click Start VCN Wizard.

    Oracle Cloud Infrastructure Virtual Cloud Networks

  3. Select Create VCN with Internet connectivity, click Start VCN Wizard.

    Oracle Cloud Infrastructure VCN Wizard 1

  4. Enter a name for the VCN (such as “gdk-serverless-demo-vcn”) and select the desired compartment from the drop-down list. Click Next.

    Oracle Cloud Infrastructure VCN Wizard 2

  5. Review the information and click Create.

    Oracle Cloud Infrastructure VCN Wizard 3

When the VCN has been successfully created, click View Virtual Cloud Network. The next step is to add an ingress rule to allow HTTPS connections to the public subnet of the VCN.

  1. Select Security Lists from the list of Resources and then select the Default Security List.

    Oracle Cloud Infrastructure VCN Security Lists

  2. Click Add Ingress Rules.

    Oracle Cloud Infrastructure Security List Ingress Rules

  3. Use the following properties:

    • Source Type: CIDR
    • Source CIDR: 0.0.0.0/0
    • IP Protocol: TCP
    • Destination Port Range: 443

    Click Add Ingress Rules.

    Oracle Cloud Infrastructure Add Ingress Rules

3.4. Create an Application and a Function #

This section describes how to create an Oracle function.

  1. In the Oracle Cloud Console, open the navigation menu, click Developer Services. Under Functions, click Applications.

  2. Click Create application. Use the following properties, then click Create.
    • Name: gdk-serverless-demo
    • VCN: gdk-serverless-demo-vcn
    • Subnets: Public Subnet-gdk-serverless-demo-vcn (Default)
    • Shape: Select GENERIC_X86 (if your local environment is X86) or GENERIC_ARM (if your local environment is AArch64)
  3. On the Application Details page, click Functions under Resources. Click Create function and select Create from existing image. (If required, change compartment to select your published container image.)

    Oracle Cloud Infrastructure Functions

  4. Name the function as gdk-serverless-func, select a repository and a container image. Increase the memory to 512 and click Create.

    Oracle Cloud Infrastructure Create Function

4. Create an API Gateway in Oracle Cloud Infrastructure #

This section describes how to create an API Gateway in Oracle Cloud Infrastructure to provide access to the function.

4.1. Create an API Gateway #

  1. In the Oracle Cloud Console, open the navigation menu, click Developer Services. Under API Management click Gateways.

  2. Click Create Gateway.

    Oracle Cloud Infrastructure Gateways

  3. Enter gdk-serverless-demo-gateway as the name of the new gateway, Public as its type, and select your compartment. Choose your VCN and its public subnet. Click Create Gateway.

    Oracle Cloud Infrastructure Create Gateways

4.2. Create a Deployment #

  1. On the API Gateway Details page, click Deployments under Resources. Click Create deployment.

    Oracle Cloud Infrastructure Gateway Deployments

  2. Enter a name for the deployment, enter /store as the path prefix (as it is the prefix of the controller you created). Select your compartment and click Next.

    Oracle Cloud Infrastructure Create Deployment 1

  3. Select No Authentication and click Next.

    Oracle Cloud Infrastructure Create Deployment 2

  4. Enter /{path*} as the value of path to allow all paths (starting with the /store prefix). Allow all methods by selecting ANY. Select Add single backend, Oracle functions, choose the application and function from their respective drop-down lists. Click Next.

    Oracle Cloud Infrastructure Create Deployment 3

  5. Review the deployment and click Create.

    Oracle Cloud Infrastructure Create Deployment 4

After the deployment is created, on the page describing your new Gateway page, copy the hostname. This is the hostname that you will use to send requests to the function.

4.3. Create a Policy #

You must give the gateway permission to call the function. To do this, create a policy that will apply for all the API gateways in the compartment.

  1. In the Oracle Cloud Console, open the navigation menu, click Identity & Security. Under Identity click Policies.

  2. Select the compartment and click Create Policy.

  3. Provide a name for the policy, select your compartment, and select Show manual editor. Paste the following contents in the policy (filling in your compartment OCID):
    Allow any-user to use functions-family in compartment id [YOUR COMPARTMENT OCID] where ALL {request.principal.type= 'ApiGateway', request.resource.compartment.id = '[YOUR COMPARTMENT OCID]'}
    
  4. Click Create.

5. Test the Oracle Function #

Now the application is deployed to Oracle Cloud Infrastructure, you can test it by sending requests.

  1. Define an environment variable for your gateway hostname:

     export GATEWAY_HOSTNAME=aaaaaaaaaaaaaaaaaaaaaaa.apigateway.ca-toronto-1.oci.customer-oci.com
    

    Note: On Windows, use set GATEWAY_HOSTNAME=<hostname> to set it and %GATEWAY_HOSTNAME% to access it. Or you can skip this step and paste the hostname to each of the following commands manually.

  2. Use curl to retrieve all the items:

     curl https://$GATEWAY_HOSTNAME/store/all
    
     [
       {
         "name": "chair",
         "description": "A black chair with 4 legs",
         "numberInStorage": 10
       },
       {
         "name": "table",
         "description": "A quality dining table",
         "numberInStorage": 6
       },
       {
         "name": "sofa",
         "description": "A gray sofa",
         "numberInStorage": 2
       },
       {
         "name": "bookshelf",
         "description": "A futuristic-looking bookshelf",
         "numberInStorage": 0
       }
     ]
    
  3. Get an error when attempting to order too many items:

     curl -X POST https://$GATEWAY_HOSTNAME/store/order/table/10
    
     {"message": "Bad Request",
       "_embedded": {
         "errors": [{
           "message": "Could not order item 'table'. Insufficient amount in storage"
         }]
       }, ...
     }
    
  4. Order an item and print the response status code:

     curl -X POST -w "\nStatus code: %{http_code}" https://$GATEWAY_HOSTNAME/store/order/table/6
    
     {
       "name":"table",
       "description":"A quality dining table",
       "numberInStorage":0
     }
     Status code: 200
    
  5. Get the available items:

     curl https://$GATEWAY_HOSTNAME/store/available
    
     [
       {
         "name": "chair",
         "description": "A black chair with 4 legs",
         "numberInStorage": 10
       },
       {
         "name": "sofa",
         "description": "A gray sofa",
         "numberInStorage": 2
       }
     ]
    

6. Publish a Native Executable to Oracle Cloud Infrastructure #

6.1. Publish the Native Executable to Container Registry #

To create and publish a container image containing a native executable to Container Registry, provide the path to a container repository in the build file.

Modify the oci/build.gradle file. Make sure it contains the following contents and that nothing overrides the set properties:

var regionIdentifier = "[REGION IDENTIFIER]"
var objectStorageNamespace = "[OBJECT STORAGE NAMESPACE]"
tasks {
    dockerBuild {
        images = ["${regionIdentifier}.ocir.io/${objectStorageNamespace}/gdk-function-demo"]
    }
    dockerBuildNative {
        images = ["${regionIdentifier}.ocir.io/${objectStorageNamespace}/gdk-function-demo"]
    }
    dockerfile {
        baseImage('fnproject/fn-java-fdk:jre17-latest')
    }
    dockerfileNative {
        args("-XX:MaximumHeapSizePercent=80")
        baseImage('gcr.io/distroless/cc-debian10')
    }
}
  • Fill in the region identifier and object storage namespace as described in section 3.2.
  • Set the dockerfile.baseImage property to use the Java 17 runtime environment.
  • Use a distroless base container image and limit the heap size to 80% for native executable.

Save the build file, then run the following command to create and publish a container image (of the native executable) to Container Registry:

./gradlew oci:dockerPushNative

Modify the oci/pom.xml file. Add the following profile:

<profiles>
  <profile>
    <id>docker-native</id>
    <activation>
      <property>
        <name>packaging</name>
        <value>docker-native</value>
      </property>
    </activation>
    <properties>
      <jib.docker.tag>native</jib.docker.tag>
      <jib.docker.image>${regionIdentifier}.ocir.io/${objectStorageNamespace}/gdk-function-demo</jib.docker.image>
    </properties>
    <build>
      <plugins>
        <plugin>
          <groupId>io.micronaut.build</groupId>
          <artifactId>micronaut-maven-plugin</artifactId>
          <configuration>
            <baseImageRun>gcr.io/distroless/cc-debian10</baseImageRun>
            <nativeImageBuildArgs>
              <arg>--initialize-at-build-time=com.example</arg>
            </nativeImageBuildArgs>
          </configuration>
        </plugin>
        <plugin>
          <groupId>com.google.cloud.tools</groupId>
          <artifactId>jib-maven-plugin</artifactId>
          <configuration>
            <to>
              <image>${jib.docker.image}</image>
            </to>
          </configuration>
        </plugin>
      </plugins>
    </build>
  </profile>
</profiles>
  • Make sure that the region identifier and object storage namespace properties are set as described in section 3.2.
  • Use <jib.docker.image> to set the name of the container image created by the docker-native build command.
  • Use <baseImageRun> to use a distroless base container image as the base runtime image.

Save the build file, then run the following commands to create and publish a container image (of the native executable) to Container Registry:

./mvnw clean
./mvnw install -pl lib -am
./mvnw deploy -Dpackaging=docker-native -pl oci

6.2. Create the Oracle Cloud Infrastructure Function #

To create a function on Oracle Functions with the native executable, repeat the steps in section 3.2., then set up an API gateway as described in section 4.

Alternatively, you can open the existing application in Oracle Cloud Infrastructure, find the function that you created and click Edit. In the list, choose the native executable from the container registry and click Save. Once the function reloads, use the same API Gateway to access it and test it as described in section 5.

Summary #

This guide demonstrated how to create a Micronaut Gateway Function application, run it on Oracle Functions, and set up an API Gateway to provide access to the function.