Deploying an executable JAR on Azure functions

Boris 1 Reputation point
2022-04-13T12:20:31.333+00:00

Hello!
I'm trying to deploy an executable JAR file, which contains a Jetty server onto Azure functions. Locally, I can run my application with the command java -jar handler.jar. As far as I understand, in order to deploy it to Azure Functions, I need to use a custom handler -- for example, with the following host.json file, everything works fine locally when I run func start:
... "customHandler": { "description": { "defaultExecutablePath": "/usr/bin/java", "arguments": ["-jar", "handler.jar"] }, "enableForwardingHttpRequest": true ...
Now, I am wondering if there is any way to deploy this application to the Azure cloud, which is more efficient than creating a custom image? My handler.jar is very small (a couple of MB), while the base "slim" java image mcr.microsoft.com/azure-functions/java:4-java11-slim has almost 1GB. I tried out this approach and it works, but it seems to me it introduces a lot of overhead (build image, push very large image to dockerhub, ...). If I create a function with java as the runtime stack, and try to push this application to the cloud, I get the message Your Azure Function App has 'FUNCTIONS_WORKER_RUNTIME' set to 'java' while your local project is set to 'custom'.
So the question is: is there a way to use the provided Java runtime stack, and use it to execute my .jar file, or do I necessarily need to create an new image?
Thank you!

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
4,678 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Takahito Iwasa 4,841 Reputation points MVP
    2022-04-13T21:31:38.177+00:00

    Hi, anonymous user-7075

    I understand that you want to execute a function with the default Java handler.

    I think you can configure and execute host.json and function.json with the following configuration, but please give me additional information if there is a part that does not meet your request.

    https://learn.microsoft.com/en-us/azure/azure-functions/functions-reference-java?tabs=bash%2Cconsumption

       {  
         "scriptFile": "azure-functions-example.jar",  
         "entryPoint": "com.example.Function.echo",  
         "bindings": [  
           {  
             "type": "httpTrigger",  
             "name": "req",  
             "direction": "in",  
             "authLevel": "anonymous",  
             "methods": [ "GET","POST" ]  
           },  
           {  
             "type": "http",  
             "name": "$return",  
             "direction": "out"  
           }  
         ]  
       }  
    
    0 comments No comments

  2. Boris 1 Reputation point
    2022-04-14T07:53:26.853+00:00

    Hi @Takahito Iwasa ,
    Thank you for your answer. Let me give you some more detail - probably I did not explain myself clear enough.
    I am trying to replicate the example that is in "Quickstart: Create a Go or Rust function". I set up a couple of methods that respond to REST requests, and I bundeled them as well as a Jetty server, that manages and routes the requests, into the JAR. When I run java -jar handler.jar, an instance of a Jetty server is started, so I can navigate to http://localhost:8080/ping. I can achieve the same when using func start with the following host.json:

    "customHandler": {  
        "description": {  
          "defaultExecutablePath": "java",  
          "arguments": ["-jar", "handler.jar"]  
        },  
        "enableForwardingHttpRequest": true  
      }  
    

    So func will start a functions host that will redirect all its HTTP requests to the my server. This solution uses the JRE I have installed on my computer.
    Now, I would like to deploy this app to Azure Functions. One possible way would be to create a custom image with the following Dockerfile:

    FROM mcr.microsoft.com/azure-functions/java:4-java11-slim  
    ENV AzureWebJobsScriptRoot=/home/site/wwwroot  
    COPY ["./functions", "/home/site/wwwroot"]  
    

    and start a function by using this image - I tried this and it works ok. I was just wondering, if there is a way to avoid this step? For example, push only the JAR and all the .json files to functions and use the default JRE, that is included when I create a Function App with the Java runtime stack, in a similar way as func start?
    Thank you!

    0 comments No comments