Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save icyfire0573/f1ad101b9514cfa2e30d7d1319877329 to your computer and use it in GitHub Desktop.
Save icyfire0573/f1ad101b9514cfa2e30d7d1319877329 to your computer and use it in GitHub Desktop.

Building on this article from gjcampbell about running Azure Functions on IIS, I set about trying to do this myself. I ran into enough issues based on the information provided that I decided to write my own article.

This work was based on standing up a plain Windows Server 20H2 VM.

Setup

  1. Download the "Azure Functions Host" binaries from Github. ( I downloaded 4.2.1 )
  2. Download the Dotnet Core Hosting package ( I downloaded dotnet-hosting-6.0.3-win.exe)
  3. Install IIS on Windows Server.
    1. I didn't know what I needed so I installed everything
Install-WindowsFeature -Name Web-Server -IncludeAllSubFeature -IncludeManagementTools
  1. Unzip the Azure Functions file somewhere. I chose C:\inetpub\AzureFunctions
  2. I removed the default IIS site and created a new one at C:\inetpub\wwwroot
  3. I made this the web.config at C:\inetpub\wwwroot\web.config .
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
	<system.webServer>
		<httpErrors errorMode="Detailed" />
		<handlers>
			<remove name="aspNetCore" />
			<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" />
		</handlers>
			<environmentVariables>
				<environmentVariable name="AzureWebJobsStorage" value="UseDevelopmentStorage=true" />
			</environmentVariables>
    		</aspNetCore>
	</system.webServer>
</configuration>
  1. Finally, You need to grant the IIS_USRS Read and Execute access to the C:\inetpub\AzureFunctions.
    1. I was doing this on Windows Server Core and was having trouble with my icacls commandline-fu I was in a rush so I just gave everyone full access
icacls C:\inetpub\AzureFunctionHost /grant everyone:F /t

Troubleshooting

Other things I installed that I think ended up being unnecessary:

  1. aspNetCore Runtime
  2. dotNet SDK

Commands to Run to check

  1. After updating the web.config, restart IIS.
iisreset
  1. I had issues with this and often had to run it twice.
  2. Checking the Event Log for issues
Get-EventLog -LogName application |select -first 10 |ft -AutoSize -Wrap
  1. Putting it all together and testing the webpage
iisreset; curl localhost ; Get-EventLog -LogName application |select -first 10 |ft -AutoSize -Wrap
  1. See previous comment about having to run iisreset twice; i often ran this twice as well because of that. Probably something silly on my install

  2. After getting this all set and before I had fixed the permissions I was getting a 503 error. This turned out to be because the IIS account couldn't access the Azure Functions files. I created a user; added it to the IIS_USRS group and then tested it's access with the following:

net user iis_test Super1_Secure@_Password- /Add
runas /user:iis_test cmd.exe
REM # This Will open a new CMD window
REM # In that window
whoami 
REM # this should return IIS_TEST
C:\inetpub\AzureFunctionHost\4.2.1\bit\Microsoft.Azure.WebJobs.Script.WebHost.exe
REM # if this runs you should be good to go. If it doesn't run you need to resolve whatever errors it throws. If the error is file not found refer to the above icacls command where I gave "everyone" full control of the directory. 
@AverageCakeSlice
Copy link

AverageCakeSlice commented Apr 7, 2022

Hey, I'm trying to get this up and running myself. I'm like 95% of the way there. I can hit the functions splash page at the root path http://example.com, but I get a 404 for any other routes (such as http://example.com/api/test) Have you run into this at all?

@icyfire0573
Copy link
Author

I haven't had a chance to verify this, but chances are your have your IIS root directory set wrong. To troubleshoot open cmd.exe and CD into the directory you THINK your website is running from and then run the C:\inetpub\AzureFunctionHost\4.2.1\bit\Microsoft.Azure.WebJobs.Script.WebHost.exe command. That will launch the azureFunctions app on port... 7070 or something (tells you in the prompt). Use that to test and see if it is working. If it is, then double check that your IIS directory matches that directory. ( That will let you know if it is an IIS think or an Azure Functions thing. ).

@radiothom
Copy link

Hello, I did everything as per your suggestions and I manage to call an httptrigger, which is also written in the log. But I don't know if the timertrigger starts, since it is not written to the log. I think the trigger doesn't fire. Is there anything i can do to get out of this situation?

@ccasalicchio
Copy link

I also followed the tutorial, and yet I can't reach the HTTP triggers (/api/xxx) not even from the command prompt. There are only 2 folders inside the Azure Functions build: .azurefunctions and runtimes
all the DLLs and exe are inside the main folder, all permissions have been set, and I can browse the default azure functions page, but not the endpoints. I've compiled/dotnet publish the project using v4 and dotnet 8
<TargetFramework>net8.0</TargetFramework> <AzureFunctionsVersion>v4</AzureFunctionsVersion> <OutputType>Exe</OutputType> <ImplicitUsings>enable</ImplicitUsings> <Nullable>enable</Nullable>

Any suggestions on how to fix this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment