다음을 통해 공유


Microsoft Azure Functions: Getting started

Introduction

In this article, we will discuss an Azure Service called Azure Functions. This service has gone GA (General Availability) in November 2016 and provides a capability of running small pieces of code in Azure. With functions, you do not have to deal that much with infrastructure, it’s the responsibility of Azure or an application (see Azure Functions Overview). The functionality you develop in a browser i.e. in the Azure Portal (Function App or https://functions.azure.com/), test it and be done. The only aspects other than the code are scalability and pricing.

Basic Walkthrough of creating an Azure Function

In the Azure Portal, you can navigate/search for Function App. And click on Function App. 

http://ultraimg.com/images/2016/12/04/VzYU.png

Next, you’ll see a Create button and once you press that a pane will appear.

http://ultraimg.com/images/2016/12/04/VzY8.png

In this pane, specify the name for the app, select a subscription, create or add to the existing resource group, choose a hosting plan, location and choose a storage account. Hit Create and a Quick Start tab will appear once the app has been provisioned, where you can choose a scenario, a preferred language or create your own function. You can choose the Webhook + API scenario with C# and click “Create this function”, which will create an HTTP-triggered function. The first thing that will happen is that you will be taken through a tour guiding you through several tabs and options. The focus will be on the development tab once you complete the tour.

http://ultraimg.com/images/2016/12/04/VzYa.png

By choosing the scenario “Webhook + API” you instantly get access to sample code you can leverage. The code represents an HTTP request that takes a request and returns with a response either Hello plus or that you’ll have to provide a query string. You can run this straight away or call via Postman i.e. copy the Function URL and execute a GET.

By choosing the Integrate tab you can change the trigger, input and output bindings for your function. By clicking the document plus sign you’ll see information on the setting for HTTP and webhook bindings, including more sample code (see also Azure Functions HTTP and webhook bindings). In the next tab, Manage, you can enable/disable the function, and manage function keys, which are intended for security purposes (see also Working with Keys). In the Monitor tab, you observe your function runs.

http://ultraimg.com/images/2016/12/04/VznQ.png

The Function app settings tab below shows function app settings, that hook into the capabilities of the app service, where basically the function apps run.

http://ultraimg.com/images/2016/12/04/Vznd.png

In the Development tab, you can test your function by selecting test and configuring how you like to test your function.

http://ultraimg.com/images/2016/12/04/Vznj.png

The development environment or feature of functions provides you with the necessary tools to build, test and deploy your function. You do not specifically need any other tools like Postman. However, the latter can be useful to see if your function is accessible from the outside. For testing a function, see Testing Azure Functions.

Considerations

The Azure Function can be another solution building block in your toolbox. From a solution architecture perspective, you have a few similar building blocks available such as WebJob, Flow, and Logic Apps. All capable of solving integration problems and/or automate business processes. Fortunately there is guidance on what to choose: Choose between Flow, Logic Apps, Functions, and WebJobs.

Pricing is another aspect of any Azure Service you use. With Functions, you can either opt for a consumption plan or regular pricing through an App Service Plan, see Function Pricing. You can use the calculator to have a better indication of price.

Depending on your requirements i.e. predominantly at what scale you want to run your Functions, see Scaling Azure Functions.

As it is a server less coding experience you can use a browser to build Azure Functions. However, as we have seen in this article, you also have the possibility of building Functions using Visual Studio 2015. Note that this is still in preview.

Another aspect with developing functions is complexity. The Function implementation is limited by code, i.e. you can implement a substantial amount of code to do various complex tasks. Therefore, you should maintain some best practices when it comes to creating Functions. These practices are:

  • Functions should do just one thing
  • Functions should finish as quickly as possible
  • Functions should be stateless
  • Functions should be idempotent

Moreover, you might want to create a strategy around the amount of Functions you think are useful can be put in a Function App for reuse towards Logic Apps. We saw a simple example of a Function that adheres to previous mentioned best practices which changes a date in a different format.

Other than re-usability, you need to consider some ALM type of aspects like versioning, operations and deployment. Functions can be created in a browser and/or in Visual Studio. And deployment can be regulated through Visual Studio and is fit for continues deployment.

Functions are secured by means of a code parameter that has to be supplied when calling the Function endpoint. Since we created a WebHook trigger in this article to directly call the Function endpoint we need to supply this code.

https://logicappfunctionsorders.azurewebsites.net/api/HttpTriggerCsharp1?code=pu16vmDDdWalCaP8bm/N7w5/KZPzbjBqdtwnhwNmBTNIaNgCNnl2Cg==

These codes i.e. keys are managed through the Function App Manage tab. Here you can also enable/disable the Function, and manage the Function keys (see also Working with Keys). To further secure the solution itself the Logic App Http Endpoint needs to be secured for which you have several options:

  • API Management
  • Access Control (Restrict calls to triggers in this logic app to the provided IP ranges)
  • SAS Keys of the Logic App (access key can be regenerated to invalidate the Logic App’s SAS URLs).

In case you run into issues with Functions, you have several options:

Conclusion

In this article, we scratched the surface of Azure Functions by providing a basic walkthrough of creating a function. The service has gone GA and extends the capabilities Microsoft Azure brings to its consumers. It’s a great addition to building Azure solutions.