javamacosmavenintellij-ideaazure-functions

Java Azure function in MacOS M1 does not work when running locally with Maven or IntelliJ and does not show logs but it does work with C#


I'm working with a MacOS M1 and I'm trying to create an Azure function using Java and Maven.

I've followed all the steps here: https://learn.microsoft.com/en-us/azure/azure-functions/functions-create-maven-intellij

And also here: https://learn.microsoft.com/en-us/azure/azure-functions/create-first-function-cli-java?tabs=macos%2Cbash%2Cazure-cli%2Cbrowser

That is to say, I've configured the JAVA_HOME environment to point to my JDK 11 version, etc. and tried to run the Azure function both in IntelliJ and with mvn azure-functions:run command.

In both cases I don't get any error but the only logs I can see are the following:

Extension bundle specified, skip install extension

Azure Functions Core Tools
Core Tools Version:       4.0.6280 Commit hash: N/A +421f0144b42047aa289ce691dc6db4fc8b6143e6 (64-bit)
Function Runtime Version: 4.834.3.22875

It's very weird because it doesn't seem to be running the func start command or something like that. It's not detecting my Function which is correctly annotated with @FunctionName("HttpTriggerJava") and all other details that the Microsoft Azure documentation states.

On the other hand, if I follow the docs for creating an Azure Function with C# everything works as expected. Following is the link: https://learn.microsoft.com/en-us/azure/azure-functions/create-first-function-cli-csharp?tabs=macos%2Cazure-cli

Below are the logs I'm getting after running func start:

Determining projects to restore...
  All projects are up-to-date for restore.
  Determining projects to restore...
  Restored /Users/xxx/xxx/xxx/xxx/LocalFunctionProj/obj/Debug/net8.0/WorkerExtensions/WorkerExtensions.csproj (in 1.91 sec).
  WorkerExtensions -> /Users/xxx/xxx/xxx/xxx/LocalFunctionProj/obj/Debug/net8.0/WorkerExtensions/buildout/Microsoft.Azure.Functions.Worker.Extensions.dll
  LocalFunctionProj -> /Users/xxx/xxx/xxx/xxx/LocalFunctionProj/bin/output/LocalFunctionProj.dll

Build succeeded.
    0 Warning(s)
    0 Error(s)

Time Elapsed 00:00:13.89



Azure Functions Core Tools
Core Tools Version:       4.0.6280 Commit hash: N/A +421f0144b42047aa289ce691dc6db4fc8b6143e6 (64-bit)
Function Runtime Version: 4.834.3.22875

[2024-10-13T19:55:52.679Z] Found /Users/xxx/xxx/xxx/xxx/LocalFunctionProj/LocalFunctionProj.csproj. Using for user secrets file configuration.
[2024-10-13T19:56:00.382Z] Worker process started and initialized.

Functions:

    HttpExample: [GET,POST] http://localhost:7071/api/HttpExample

For detailed output, run func with --verbose flag.
[2024-10-13T19:56:05.581Z] Host lock lease acquired by instance ID 

Finally, if I hit the endpoint http://localhost:7071/api/HttpExample I'm getting the expected response.

Did anyone experience this issue? I'm stuck with this, I've spent almost the whole day struggling with this and didn't find any solution. I wouldn't want to work with C#.

Thanks in advance


Solution

  • The issue could be due to the way the function core tools handling the Azure function.

    As I have mentioned in the comments, change authLevel = AuthorizationLevel.Anonymous in function code to resolve the issue.

    Code Snippet:

    @FunctionName("HttpExample")
        public HttpResponseMessage run(
                @HttpTrigger(
                    name = "req",
                    methods = {HttpMethod.GET, HttpMethod.POST},
                    authLevel = AuthorizationLevel.ANONYMOUS)
                    HttpRequestMessage<Optional<String>> request,
                final ExecutionContext context) {
            context.getLogger().info("Java HTTP trigger processed a request.");
    
            // Parse query parameter
            final String query = request.getQueryParameters().get("name");
            final String name = request.getBody().orElse(query);
    
            if (name == null) {
                return request.createResponseBuilder(HttpStatus.BAD_REQUEST).body("Please pass a name on the query string or in the request body").build();
            } else {
                return request.createResponseBuilder(HttpStatus.OK).body("Hello, " + name).build();
            }
        }
    

    Output:

    [INFO] Azure Functions Core Tools found.
    
    Azure Functions Core Tools
    Core Tools Version:       4.0.6280 Commit hash: N/A +421f0144b42047aa289ce691dc6db4fc8b6143e6 (64-bit)       
    Function Runtime Version: 4.834.3.22875
    
    [2024-10-17T14:40:02.210Z] OpenJDK 64-Bit Server VM warning: Options -Xverify:none and -noverify were deprecated in JDK 13 and will likely be removed in a future release.
    
    Functions:
    
            HttpExample: [GET,POST] http://localhost:7071/api/HttpExample
    
    For detailed output, run func with --verbose flag.
    [2024-10-17T14:40:03.163Z] Worker process started and initialized.
    [2024-10-17T14:40:07.153Z] Host lock lease acquired by instance ID '000000000000000000000000F72731CC'.
    [2024-10-17T14:40:14.300Z] Executing 'Functions.HttpExample' (Reason='This function was programmatically called via the host APIs.', Id=3c8dfd9c-92f0-489e-a174-2657eba904d1)
    [2024-10-17T14:40:14.449Z] Function "HttpExample" (Id: 3c8dfd9c-92f0-489e-a174-2657eba904d1) invoked by Java Worker
    [2024-10-17T14:40:14.449Z] Java HTTP trigger processed a request.
    [2024-10-17T14:40:14.585Z] Executed 'Functions.HttpExample' (Succeeded, Id=3c8dfd9c-92f0-489e-a174-2657eba904d1, Duration=316ms)