31

When hosting an Azure Function in an App Service Plan, are there any significant differences compared with using an App Service (EDIT: for a restful API) associated with the same App Service Plan? I assume the only difference is that the Function offers additional out of the box triggers. Any differences I'm missing that would lead to preferring one over the other?

I'd also like to confirm that hosting Azure Functions in an App Service Plan can actually limit scalability if scaling is not configured on the App Service Plan. As I understand it, Functions automatically scale as needed when using Consumption or Premium hosting without additional configuration.

scottctr
  • 683
  • 2
  • 6
  • 19

3 Answers3

33

When hosting an Azure Function in an App Service Plan, are there any significant differences compared with using an App Service associated with the same App Service Plan? I assume the only difference is that the Function offers additional out of the box triggers. Any differences I'm missing that would lead to preferring one over the other?

Well, an Azure Function is a different beast than an App Service. An Azure function is triggered by an external event or a timer. It then executes the code of the function. When hosted on a consumption plan this execution is allowed to run for 5 or 10 minutes max. When you need a longer execution time you need to run it on an App Service Plan.

An App Service can host any app you've created. Like a website (that runs continuously and doesn't need to be triggered before it starts doing something) or an api for example.

I'd also like to confirm that hosting Azure Functions in an App Service Plan can actually limit scalability if scaling is not configured on the App Service Plan. As I understand it, Functions automatically scale as needed when using Consumption or Premium hosting without additional configuration.

Correct, when hosting Azure Functions in an App Service Plan you are responsible for making sure the app service is scaled to allow the function to perform well under load. Thats why the consumption plan is designed to handle this so the developer can focus on the functionality and does not need to worry about the infrastructure.

So, for integration scenario's azure functions are a very natural fit. For web sites an App Service might be the best solution.

To address your comment:

I should have mentioned that this question was in the context of hosting a restful API and not a UI application. In this scenario, I'm not seeing much difference between a Function and App Service, but please correct me if I'm missing something

A couple of things: For one, there is a certain sweet spot. If traffic is heavy enough a consumption plan based azure function might be more costly than having a dedicated app service plan. That depends of course on a lot of factors (CPU usage, request duration etc.). Also, you won't be able to use things like Asp.Net Core Middleware out of the box. Finally, I'd argue that if your api is becoming large enough managing a single asp.net core solution may be easier than having to manage a lot of azure functions with small functions or one azure function project with lots and lots of functions, but hey, that's just my opinion (haven't actually dealt with it to be honest)

Some resources to consider:

Peter Bons
  • 26,826
  • 4
  • 50
  • 74
  • Thanks Peter. I should have mentioned that this question was in the context of hosting a restful API and not a UI application. In this scenario, I'm not seeing much difference between a Function and App Service, but please correct me if I'm missing something. – scottctr May 15 '21 at 17:24
  • @scottctr, I'm curious which way you went and your experience. What did you choose and do you believe you made the right decision? If so, why? If you would have chosen the alternate option, why? – lcj Sep 11 '22 at 13:34
  • @lcj, I've used many Azure Functions and App Services since posting this question, but I've never hosted a Function in my own App Service Plan. I prefer Functions since they are typically quicker to create and cheaper to operate. If a Function can't meet the need because of cold startup, slow scaling, or long execution time, I just use an App Service. The reduced Function development time and faster startup, unlimited execution time, and more responsive scaling for App Service Plans keeps it this a viable option though. – scottctr Sep 14 '22 at 01:55
  • @scottctr, I really appreciate the information. I see there are three options (serverless, premium, and app service). I was building through bicep and the example I had used app service. – lcj Sep 14 '22 at 10:58
  • @scottctr, out of curiosity and related to the topic, how have you dealt with repositories (database). Did you just embed them in the function app or have a separate library? I'm hoping it's the former. I also have a few helper classes and grab info from the header. Have you run into any complexities or are these non-issues? – lcj Sep 14 '22 at 11:01
  • @scottctr, sorry for all the comments. I was watching a youtube video and it looks like premium (not consumption) creates an app service plan automatically. https://www.youtube.com/watch?v=tzgB3cSUdNM&t=1506s – lcj Sep 14 '22 at 11:15
8

The main difference is in how you pay for it:

  • Azure Functions Consumption plan you pay per execution
  • Azure Functions in an App Service (dedicated plan) you pay for the allocated hardware per minute.

You also have control of which VNET your functions run in when you use an app service plan. You may have security requirements that make this important.

You are correct that if you run it in an app service that is not configured to scale, then throughput will be limited by the allocated hardware.

For details see: https://learn.microsoft.com/en-us/azure/azure-functions/functions-scale

Cerberton
  • 376
  • 2
  • 7
  • 16
Shiraz Bhaiji
  • 64,065
  • 34
  • 143
  • 252
  • Good point with the VNet. We use a complete isolated environment so I don't VNet makes a difference for us. – scottctr May 15 '21 at 17:27
0

If you are having limited and predictable workload then deploy az function in AppService plan with supports VNET integration for private compute otherwise go for Premium plan which will provide autoscaling capability of your compute environment.