Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Azure function HTTP triggered on linux container - function not working 404

I am stuck and maybe you can help me out. (Update: After investigation, I do know that it is not working because the wwwroot folder for the Azure function after build/release is empty. The main question is why)

I am trying to publish Azure Function using Azure Container Registry (Linux) This step I think I am successful. I did create CI/CD pipeline and everything is being published correctly.

When i enter my azure function main page like: https://myAzureFuncion.azurewebsites.net/ (it is only example)

i do see:

enter image description here

At the begining the function itself. It is nothing fancy, as i just wanted to test the CI/CD so it is the AF template. I just did changed the Auhtorization to Anonymous (to exlude issue with the authentication):

        [FunctionName("MyAzureFunctionName")]
        public static async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            log.LogInformation("C# HTTP trigger function processed a request. Runned!");

            string name = req.Query["name"];

            string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
            dynamic data = JsonConvert.DeserializeObject(requestBody);
            name = name ?? data?.name;

            return name != null
                ? (ActionResult)new OkObjectResult($"Hello, {name}")
                : new BadRequestObjectResult("Please pass a name on the query string or in the request body");
        }

So i good to go - right? No, as it is not working. I am trying to trigget this function by HTTP request:

https://myAzureFuncion.azurewebsites.net/api/MyAzureFunctionName?name=Mark

And I keep getting 404 Not Found. I did check it with Postman: The same (previously with function authorization i did try with ?code=(code_in_host) and with headers x-functions-key. All the time the same info --> 404 Not Found)

It is as this function was not existing.

Of course, when I run it locally (using docker for windows) everything is working correctly. I am running:

https://localhost:some_port/api/MyAzureFunctionName?name=Mark

And I am receiving proper answer.

Can you point me what to check and how to diagnose the issue here? I've entered the App Insights Live Metrics --> but no request is being noted.

How to diagnose the issue here?

[UPDATE 04.05.2020 01:35]

Here is how my release pipeline looks like for this component in YAML

steps:
- task: AzureFunctionAppContainer@1
  displayName: 'Azure Function App on Container Deploy: MyFunctionName'
  inputs:
    azureSubscription: MySubscription
    appName: MyFunctionName
    imageName: 'myAcrContainer.azurecr.io/mobile/MyFunctionName:$(Build.BuildNumber)'

I saw also one weird thing in Azure Portal for this Azure function. When i go to functions menu - there is information, that there is none:

enter image description here

The same shows up, when i use the "new Azure Function management experience"

enter image description here

[UPDATE 04.05.2020 11:40]

Putting more info on the case. Here is how docker image looks like:

#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging.

FROM mcr.microsoft.com/azure-functions/dotnet:3.0 AS base
WORKDIR /app
EXPOSE 80

FROM mcr.microsoft.com/dotnet/core/sdk:3.0-buster AS build
WORKDIR /src
COPY ["BuildChat/BuildChat.csproj", "BuildChat/"]
COPY ["MyFunctionName/MyFunctionName.csproj", "MyFunctionName/"]
RUN dotnet restore "MyFunctionName/MyFunctionName.csproj"
COPY . .
WORKDIR "/src/MyFunctionName"
RUN dotnet build "MyFunctionName.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "MyFunctionName.csproj" -c Release -o /app/publish

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENV AzureWebJobsScriptRoot=/app

And here is the build pipeline that i am using:

- stage: BuildMyAzureFunction
  displayName: Build and push MyAzureFunction stage
  jobs:  
  - job: Build
    displayName: Build
    pool:
      vmImage: $(vmImageName)
    steps:
    - task: Docker@2
      displayName: Build and push image to container registry
      inputs:
        containerRegistry: $(dockerRegistryServiceConnection)
        repository:  $(imageRepositoryMyAzureFunction)
        command: 'buildAndPush'
        Dockerfile: $(dockerfilePathAzureMyAzureFunction)
        tags: |
          $(tag)

I do not know what more i can give you :)

Maybe container settings in Azure Portal:

enter image description here

And logs from latest docker release:

2020-05-04 09:32:32.693 INFO  - Recycling container because of AppSettingsChange and isMainSite = True
2020-05-04 09:32:32.777 INFO  - Pulling image: myAcrContainer.azurecr.io/mobile/myAzureFunction:20200503.11
2020-05-04 09:32:33.829 INFO  - 20200503.11 Pulling from mobile/myAzureFunction
2020-05-04 09:32:33.832 INFO  -  Digest: sha256:688090984dbc5d257b7d4eefff886affa451c59407edd46792dfc81726f393ec
2020-05-04 09:32:33.832 INFO  -  Status: Image is up to date for myAcrContainer.azurecr.io/mobile/myAzureFunction:20200503.11
2020-05-04 09:32:33.835 INFO  - Pull Image successful, Time taken: 0 Minutes and 1 Seconds
2020-05-04 09:32:33.959 INFO  - Starting container for site
2020-05-04 09:32:33.961 INFO  - docker run -d -p 7287:80 --name myAzureFunction_1_84dd4d10 -e WEBSITE_CORS_ALLOWED_ORIGINS=https://functions.azure.com,https://functions-staging.azure.com,https://functions-next.azure.com -e WEBSITE_CORS_SUPPORT_CREDENTIALS=False -e WEBSITES_ENABLE_APP_SERVICE_STORAGE=false -e WEBSITE_SITE_NAME=myAzureFunction -e WEBSITE_AUTH_ENABLED=False -e PORT=80 -e WEBSITE_ROLE_INSTANCE_ID=0 -e WEBSITE_HOSTNAME=myAzureFunction.azurewebsites.net -e WEBSITE_INSTANCE_ID=8da8a02a13a3cdde53ad6aafcd4eb717ca00dd6bc65ff07378d74a0cd859e1c0 -e HTTP_LOGGING_ENABLED=1 myAcrContainer.azurecr.io/mobile/myAzureFunction:20200503.11  

2020-05-04 09:32:39.421 INFO  - Starting container for site
2020-05-04 09:32:39.422 INFO  - docker run -d -p 1500:8081 --name myAzureFunction_1_84dd4d10_middleware -e WEBSITE_CORS_ALLOWED_ORIGINS=https://functions.azure.com,https://functions-staging.azure.com,https://functions-next.azure.com -e WEBSITE_CORS_SUPPORT_CREDENTIALS=False -e WEBSITES_ENABLE_APP_SERVICE_STORAGE=false -e WEBSITE_SITE_NAME=myAzureFunction-e WEBSITE_AUTH_ENABLED=False -e WEBSITE_ROLE_INSTANCE_ID=0 -e WEBSITE_HOSTNAME=myAzureFunction.azurewebsites.net -e WEBSITE_INSTANCE_ID=8da8a02a13a3cdde53ad6aafcd4eb717ca00dd6bc65ff07378d74a0cd859e1c0 -e HTTP_LOGGING_ENABLED=1 appsvc/middleware:2001061754 /Host.ListenUrl=http://0.0.0.0:8081 /Host.DestinationHostUrl=http://172.16.2.6:80 /Host.UseFileLogging=true 

2020-05-04 09:32:44.658 INFO  - Initiating warmup request to container myAzureFunction_1_84dd4d10 for site myAzureFunction
2020-05-04 09:33:00.400 INFO  - Waiting for response to warmup request for container myAzureFunction_1_84dd4d10. Elapsed time = 15.7420671 sec
2020-05-04 09:33:16.088 INFO  - Waiting for response to warmup request for container myAzureFunction_1_84dd4d10. Elapsed time = 31.4308809 sec
2020-05-04 09:33:33.284 INFO  - Waiting for response to warmup request for container myAzureFunction_1_84dd4d10. Elapsed time = 48.6269148 sec
2020-05-04 09:33:56.001 INFO  - Container myAzureFunction_1_84dd4d10 for site myAzureFunctioninitialized successfully and is ready to serve requests.
2020-05-04 09:33:56.004 INFO  - Initiating warmup request to container myAzureFunction_1_84dd4d10_middleware for site myAzureFunction
2020-05-04 09:34:04.506 INFO  - Container myAzureFunction_1_84dd4d10_middleware for site myAzureFunction initialized successfully and is ready to serve requests.
2020_05_04_RD501AC582A899_easyauth_docker.log:
2020_05_04_RD501AC582A899_default_docker.log:
2020-05-04T09:21:14.187664147Z Hosting environment: Production
2020-05-04T09:21:14.187744046Z Content root path: /app
2020-05-04T09:21:14.187750446Z Now listening on: http://[::]:80
2020-05-04T09:21:14.187754446Z Application started. Press Ctrl+C to shut down.

2020-05-04T09:33:49.229983024Z Hosting environment: Production
2020-05-04T09:33:49.232401696Z Content root path: /app
2020-05-04T09:33:49.232413096Z Now listening on: http://[::]:80
2020-05-04T09:33:49.232417396Z Application started. Press Ctrl+C to shut down.

[UPDATE 04.05.2020 15:55]

I've checked the deployment center, and its look like it is ok:

enter image description here

But because i am publishing 3 thigns (1 SignalR hub and 2 azure functions - of course both "not working") i am thinking - maybe it is getting somehow mixed (and on this azure function app - in fact SignalR is being published?

I did checked this release task log (which suppose to publish MyAzureFunction) and everything looks correctly (everywhere is my MyAzureFunction in the log):

enter image description here

Here is how my Release pipeline looks like:

enter image description here

And here is Continous deployment trigger configuration:

enter image description here

And here is the stage 2 (for publishing azure function) predeployment settings:

enter image description here

And finally, the release step itself (although I did already posted the YAML version of it. To be hones - everything checks out. There is no missmatch in image/app name)

enter image description here

[UPDATE 04.05.2020 16:15]

So right now for 100% i do know what is going on. After successful release without any errors/warnings, the azure function is simply empty :)

I've checked with advanced tools the content of wwwroot (after going to https://myAzureFunction.scm.azurewebsites.net/wwwroot/)

Here is the result:

enter image description here

So it is quite understandable why it is not working :) The main question is "What the hell happened"?:)

I did try in deployment center to Sync the code (as @djsly suggested) but it is not working (probably because it is being managed by containers) But i am adding a message when I try to sync:

enter image description here

[UPDATE 05.05.2020 00:18]

I did connect to Azure container registry and downloaded the image, that suppose to be the image running behing this Azure function in Azure.

So i did runned:

docker login myAcrContainer.azurecr.io

docker pull myAcrContainer.azurecr.io/mobile/myAzureFunction:20200503.11

docker run -it --rm -p 31234:80 myAcrContainer.azurecr.io/mobile/myAzureFunction:20200503.11

and i did hit http://localhost:31234/api/MyAzureFuncionFuns

and everything works correctly. So it confirm my suspisions, that the image is not being properly "runned" on Azure function on Azure portal.

The question is: Am i doing something wrong - or is it an Azure bug?

There was a question regarding my App Service Plan. Here how it looks like (during Azure function creation):

enter image description here

And here are the plan details:

enter image description here

It is a free trial right now (as they proposed it while I was creating it) Could that be the reason? Shouldn't they disallow something instead of allowing and then breaking things ?;)

like image 615
Piotr Avatar asked May 02 '20 23:05

Piotr


People also ask

Can you trigger an Azure function using an HTTP request?

The HTTP trigger lets you invoke a function with an HTTP request. You can use an HTTP trigger to build serverless APIs and respond to webhooks. The default return value for an HTTP-triggered function is: HTTP 204 No Content with an empty body in Functions 2.

Why is my Azure not triggering?

The most common cause for Azure Functions not getting triggered is that triggers are not synced properly. You can sync triggers in one of three ways: Restart your function app in the Azure portal.

Can Azure function be run in a container?

You can also use a default Azure App Service container as described in Create your first function hosted on Linux.

Can Azure functions run on Linux?

Azure Functions lets you host your functions on Linux in a default Azure App Service container. This article walks you through how to use the Azure portal to create a Linux-hosted function app that runs in an App Service plan. You can also bring your own custom container.


2 Answers

For anyone having similar issues

I got more or less information "what is the root cause" of the problem. MS Support guy contacted me and pointed me out, that this is an issue with the docker file.

He pointed out two things: I am changing AzureWebJobsScriptRoot to /app. He claimed that this could be the reason why it is working on the local machine and not on Azure Portal where AzureWebJobsScriptRoot is /home/site/wwwroot

Two fun facts about this statement:

1) it is not I, who is creating this dockerfile (i am far to rookie) It was the functionality of MS (plugin) which allows you to right-click the project and choose Add->Docker support

This is this functionality: https://learn.microsoft.com/pl-pl/visualstudio/containers/overview?view=vs-2019

2) Who on earth would create such discrepancies in working environments, that something is working on docker locally will not work on docker on Azure Portal - this is crazy :)

He advised me to go with this tutorial: https://learn.microsoft.com/en-us/azure/azure-functions/functions-create-function-linux-custom-image?tabs=bash%2Cportal&pivots=programming-language-csharp

and to use this command:

func init LocalFunctionsProject --worker-runtime dotnet --docker

And it will create such docker file (more or less)

FROM microsoft/dotnet:2.2-sdk AS installer-env

COPY . /src/dotnet-function-app
RUN cd /src/dotnet-function-app && \
    mkdir -p /home/site/wwwroot && \
    dotnet publish *.csproj --output /home/site/wwwroot

# To enable ssh & remote debugging on app service change the base image to the one below
# FROM mcr.microsoft.com/azure-functions/dotnet:2.0-appservice 
FROM mcr.microsoft.com/azure-functions/dotnet:2.0
ENV AzureWebJobsScriptRoot=/home/site/wwwroot \
    AzureFunctionsJobHost__Logging__Console__IsEnabled=true

COPY --from=installer-env ["/home/site/wwwroot", "/home/site/wwwroot"]

The most important thing here is - that it is addressed for Azure Function 2, and I am using Azure Function 3. Nevertheless, I did change images version to fit version 3, and I did extend it to cover the fact, that I have to build the referenced library. But it is still not working :) (meaning, it works locally but doesn't work on Azure Portal)

I will create new SOF simply on the matter of docker. But I leave this comment - maybe it will help someone.

[UPDATE 13.05.2020]

For anyone looking for the root cause of the issue. If you will be looking for the problem with 404 on Azure function - please have a look here: Docker issue with Azure Function on Linux container - function missing leading to 404 on function run

like image 77
Piotr Avatar answered Oct 21 '22 09:10

Piotr


I had this issue, but in part it was visual studio being silly, and in part due to my folder structure and how I expected Visual Studio to work.

This was my folder structure:

/repoRoot/Engines/MyEngine/MyEngine/Dockerfile
/repoRoot/Engines/MyEngine/MyEngine/myengine.csproj
...etc

Visual Studio executes the docker build command (and thus, the Dockerfile context) like this:

docker build -f "G:\repoRoot\enginesFolder\engineIWasWorkingOn\Dockerfile" --force-rm -t imgname:latest --target installer-env --label "com.microsoft.created-by=visual-studio" --label "com.microsoft.visual-studio.project-name=imgname" "G:\repoRoot\enginesFolder\

The last parameter is the important bit - I expected it to be executing from the inner MyEngine folder, where Dockerfile was.

On top of this, I needed to turn on: WEBSITES_ENABLE_APP_SERVICE_STORAGE: true in my azure app service configuration, and I needed to set the Azure Functions version to 2, which is why my host.json targeted.

Ultimately, this is the dockerfile I ended up using:

FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS installer-env
COPY . /src/dotnet-function-app

RUN cd /src/dotnet-function-app/YourInnerFolderHere && \
    mkdir -p /home/site/wwwroot && \
    dotnet publish *.csproj --output /home/site/wwwroot

FROM mcr.microsoft.com/azure-functions/dotnet:2.0
ENV AzureWebJobsScriptRoot=/home/site/wwwroot \
    AzureFunctionsJobHost__Logging__Console__IsEnabled=true

COPY --from=installer-env ["/home/site/wwwroot", "/home/site/wwwroot"]

VOLUME /home/site/wwwroot /home/site/wwwroot

Please note the "/YourInnerFolderHere" in the above -- I needed to step into the inner folder where the csproj was located.

I added the VOLUME map, but I don't think it lead to the success of this deployment. I changed too many things in between attempts to know for sure, but removing it and re-deploying didn't make my function app stop working.

like image 2
Raymond Lehnhoff Avatar answered Oct 21 '22 09:10

Raymond Lehnhoff