Azure function HTTP triggered on linux container - function not working 404
Asked Answered
F

2

7

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 ?;)

Fullfaced answered 2/5, 2020 at 23:31 Comment(12)
from the looks of it, you Azure Function is created at the Azure level BUT there is no associated source code to it.Izabel
that much I did suspect from the look of it. The question is "why"? As there is no error in building/release pipeline and it is working on my local docker when I hit F5 on VSFullfaced
Have a look at the deployment center tab, where you can see the source of the code as well as try to resend its content. That should give you more insight as to why there isn't any code associated to the azure Function.Izabel
@Izabel - you give me thinging whether "the right thing" is being published to the app. I suppose, that in the end, the release pipeline is wrongly configured. I will update the question with current release pipeline (and stages)Fullfaced
But after checking the thing - i suppose everthing should work. Build pipeline is saving to proper repository. Release pipeline is fetching correct image. I don't see where there could be an issueFullfaced
yeah it seems that the Deployment Center tab shows something, what shows up when you clean on the show source code ? I personally never used AzureDevOps with Azure Function, but for the time I used AzureFuction, the issues I had when it had 0 insight on the Function page was due to a broken/missing source code.Izabel
the fact that this doesn't load anything: i.sstatic.net/e14jL.png is what we will have the answer... I unfortunately don't know where to get the logs for when Azure cannot load a az function properly. I figured the Deployment Center would be a place...Izabel
@Izabel - when I click "Source Version [hash]" I am redirected to my Azure Devops commit. Everything works fine. So right now - We know "why" it is not working - we simply don't know what is the root cause of it. To compare it to the real-life scenario "It is as if I would go to the shop, bought a cake, have it packaged, paid for it, and then when I go home I am opening it just to realize, that there is no cookie there":)Fullfaced
Curious what type of app service plan you are using? If it's a consumption plan I am not sure it is supported. I believe you would need to use a Linux app service plan for a Linux based custom container.Schrader
@EricSmith - it is B1 App service plan. It was possible to use it while creating, so i suppose, if it is possible to create - it should work - right? Of course i can try to recreate the Azure function with some higher service plan (prod not dev) but then what is the point? I just want to test the solution and the traffic will be very low. So why i have to choose production plan? And what do you mean by "consumption plan" Is B1 a "consumption plan"?Fullfaced
A B1 is a Basic App service plan is dedicated so it should be good there. Just wanted to rule it out. From the docs "Linux hosting for custom functions containers are supported on Dedicated (App Service) plans and Premium plans." learn.microsoft.com/en-us/azure/azure-functions/…Schrader
I am more and more convinced, that it is Azure portal bug. But cannot ask for help any of the Azure support guys - as i don't have support plan. This is crazy (not being able to report a bug:D) I did twit the message to them but till now no response.Fullfaced
F
7

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

Fullfaced answered 10/5, 2020 at 11:31 Comment(0)
C
2

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.

Chickweed answered 11/5, 2020 at 3:3 Comment(1)
That is the case. My folder structure is different. I have docker file one level higher than the procjet folder (because i have to build also referenced project) You can see my other SOF purely dedicated for this issue with docker and description: #61712256 And in this SOF i've described, that i did change the docker file and it is very like the one you've posted. But it is still not working. You can have a look there.Fullfaced

© 2022 - 2024 — McMap. All rights reserved.