Skip to main content
The 2024 Developer Survey results are live! See the results

Can we have huge number of endpoints in same azure function or aws lambda?

Created
Active
Viewed 419 times
13 replies
4

In my organization I am noticing that people are keep on adding different endpoints in same function.cs file provided by azure. I am wondering is this correct approach for exponentially growing code?

For example code is going like below with RunFunction1 & RunFunction2.

public static class Functions
    {
        [FunctionName("Function1")]
        public static IActionResult RunFunction1(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = "function1")] HttpRequest req,
            ILogger log)
        {
            log.LogInformation("C# HTTP trigger function processed a request.");

            return new OkObjectResult("Hello from Function1");
        }

        [FunctionName("Function2")]
        public static IActionResult RunFunction2(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = "function2")] HttpRequest req,
            ILogger log)
        {
            log.LogInformation("C# HTTP trigger function processed a request.");

            return new OkObjectResult("Hello from Function2");
        }
    }

13 replies

Sorted by:
78373404
2

IMHO, I don't see function apps the same way I look at microservices. A microservice should be focused, doing one task and only one task well. From a function app perspective, it may contain a single function for a specific task, or it can be focused on an entire app with many endpoints.

For instance, I support a web application in Azure which makes calls to a function app. The function app has six or seven endpoints to tasks that are specific to the web app.

Now if your function app was initially designed to perform on specific task then, in my opinion, new endpoints for additional tasks should not be added unless those tasks are related to the initial task.

78373709
0

For example, consider we have 10 types of reports. To fetch all these reports they are creating 10 endpoint in reports function. Do you mean this function app is intended for reporting so having 10 endpoint in it is perfect?

78374223
1

Again, in my opinion, if the function app is focused on report data, then I would say it would be OK like that. Others may have better "Best Practices", or develop each endpoint as a single function app. But this is how I would do it.

78392473
0
  • 4.3k
  • 5
  • 43
  • 82

I would definitely recommend splitting functions into several files, folders and eventually even different function apps depending on size. Look into domain driven design on how to best organize your code. Personally I would have only functions in the same file that also hit the same endpoint, so the get/post/put/delete requests for one resource when doing REST.

78404882
0

Yes that's what I expect too. Do you think we need separate function here only just to maintain clean code or performance of system can be considered also?

78407537
0
  • 4.3k
  • 5
  • 43
  • 82

Performance won't change either way since it gets compiled anyways. Purely maintenance (so developer performance, if you will :D)

78487093
0

Another consideration is that all these functions will share the same class constructor and using namespaces. If doing non-static, of course DI can be happening in that constructor. That could be a plus or minus, but something to be mindful of.

To me it's probably more an issue of code maintenance that drives me towards advocating one function for one class file and class definition. I'm not dealing with hundreds but tens of functions. Seems easier to survey the domain. Additionally, since source control is looking at the world on the basis of changed files at a high level, I think it's cleaner to have a each file pretty atomic if possible.

78497537
1

For maintainability readability is better to use single purpose functions that follows Single Responsibility principle as well as defining them in the code! So each function will be in separate class and file

78512825
0

Correct, this makes more sense to me.👍

78553579
0

I wouldn't set this as a blanket rule, 1:1 for file to function is to go the entirely opposite way. It is better to group the functions together. The actual grouping will depend on your domain and the type of functions. If there are so many then you should be able to scan through them and quickly come up with some relevant convention.

For me we, use the resource boundary, so all invoice functions are grouped together for instance. But that is a logical grouping in my domain and there are only 8 invoice functions. Importantly all these functions share the same DI requirements and same configuration, that made the boundary really easy to define.

78518311
0
  • 2.4k
  • 4
  • 38
  • 58

Although it might sound interesting to have multiple endpoints clubed in a single lambda function, and to be transparent - I have done that in certain occession. However, I prefer to follow - Singe Responsibility Principal for my function, a function should do one thing, and do it efficiently and I work to avoid clubbing of multiple features in single function to avoid bloating.

However, there are cases where you can actually run entire backend on Lambda - I had one Django backend running inside a Lambda Function and entire internal routing and everything was handed to Django app for processing.

I hope you have already read this, but, still sharing: https://aws.amazon.com/blogs/compute/best-practices-for-organizing-larger-serverless-applications/

78519595
2

I think there is an "it depends" factor here. The main consideration is scaling--i.e. what happens if one function starts to hog resources. This can change depending on what kind of App Service plan you have behind your functions (e.g. Consumption vs. Premium). For instance, from the documentation:

Consumption: Scales out automatically, even during periods of high load. Functions infrastructure scales CPU and memory resources by adding more instances of the Functions host, based on the number of incoming trigger events.

Premium: Event-driven scaling decisions are calculated on a per-function basis, which provides a more deterministic way of scaling the functions in your app. With the exception of HTTP, Blob storage (Event Grid), and Durable Functions, all other function trigger types in your app scale on independent instances. All HTTP triggers in your app scale together as a group on the same instances, as do all Blob storage (Event Grid) triggers. All Durable Functions triggers also share instances and scale together.

It's also up to you to determine if the service limits are appropriate for the plan you have. "The actual number of function apps that you can host depends on the activity of the apps, the size of the machine instances, and the corresponding resource utilization." I am nodding in agreement with the respondent here who suggested each Function getting its own app, but remember that you also can only have 5,000 Function apps per subscription (as of the date of this answer).

So, is it an anti-pattern? It depends on your use case and the allowable limits of your app. If it makes sense that they'd all scale together, it might not be a problem.

78534597
0

Looks like you are taking question to another direction, point is should we write code like this if code is growing & all functions are in same file.