0

I'm still trying to learn about Azure Functions and best practices, but as I understand there are two ways to separate logic by http verb (GET, POST, etc.). Is there a standard or best practice that would prefer either of the two below methods? And more importantly, is there a cost difference between the two methods when running in Azure, assuming the underlying logic stays the same?

Method A:

    public static class Function1
    {
        [FunctionName("Function1")]
        public static async Task<IActionResult> Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req)
        {
            if (req.Method == HttpMethods.Get)
            {
                // do stuff here
            }

            else if (req.Method == HttpMethods.Post)
            {
                // do stuff here
            }
        }
    }

Method B:

    public static class GetFunction
    {
        [FunctionName("GetFunction")]
        public static async Task<IActionResult> Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = null)] HttpRequest req)
        {
            // do stuff here
        }
    }

    public static class PostFunction
    {
        [FunctionName("PostFunction")]
        public static async Task<IActionResult> Run([HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] HttpRequest req)
        {
            // do stuff here
        }
    }
Mike
  • 517
  • 2
  • 5
  • 21

2 Answers2

1

In short the answer to your question about pricing is, IT will be the same for both. It will be equal to the time your logic runs. You should use Consumption plan. If you are looking for cost savings. Microsoft says " Azure Functions consumption plan is billed based on per-second resource consumption and executions. Consumption plan pricing includes a monthly free grant of 1 million requests and 400,000 GB-s of resource consumption per month per subscription in pay-as-you-go pricing across all function apps in that subscription." Here is a link to more details. https://azure.microsoft.com/en-us/pricing/details/functions/ As far as your second question is concerned. IMO you should use separate functions for get and post. In this way you can separate the endpoints and handle both calls differently. But this kind a breaks the RESTful API practice. So its give and take. :)

Waqas ali
  • 258
  • 3
  • 7
0

You had asked if there is a standard or best practice for using GET or POST. Generally, in REST world, you use GET if you are fetching a resource and POST when you want to create a new resource. So, it basically depends on what you are trying to achieve using the Function. Found a similar questions here > https://stackoverflow.com/a/504993/5344880 and When do you use POST and when do you use GET?

Regarding the pricing, it is based on the number of executions and memory and not by logic. This link will provide more details https://azure.microsoft.com/en-in/pricing/details/functions/ Please do note that there is hard timeout of 10 minutes for Azure Functions

Hope this helps.

Guru Pasupathy
  • 440
  • 1
  • 4
  • 13
  • 1
    I appreciate the response, but I'm familiar with RESTful APIs; I'm more asking how to separate the logic in the Azure Functions world. I ask about pricing implications because I'm not sure if one method would use more computing resources than the other. – Mike May 23 '20 at 01:55