Page 46 - MSDN Magazine, December 15, 2017
P. 46

FUNCTIONS
Create Serverless APIs
Using Azure Functions
Alex Karcher
In the world of API development, serverless application plat- forms have redefined the problems developers must solve. With instant scale and consumption billing, developers are increasingly turning to serverless tools like Azure Functions to reduce devel- opment time, be more adaptive to changing traffic patterns, and to stop paying for overprovisioned infrastructure.
Serverless simply means that the developer doesn’t have to con- sider the underlying application server when writing code. You write a single class, with a run method, and then define triggers that execute that code. As a trigger fires multiple times in close succes- sion, your code is loaded onto more and more workers to handle the load. The scale out happens on the sub-second level, and you’re only charged for the CPU running time plus the allocated memory of your function. There’s no charge for idle Functions.
Think of serverless like wireless. There are still wires behind your wireless router, but the consumers of your wireless service don’t
have to manage or configure them. With serverless, you don’t have to manage or configure servers to run your code.
In Azure Functions, the abstraction of the application server is accomplished through a powerful trigger and binding frame- work. Triggers and bindings are a declarative way to define how a function is invoked and with which data it works. A trigger defines how a function is invoked. A function must have exactly one trigger. Triggers have associated data, which is usually the pay- load that triggered the function.
Input and output bindings provide a declarative way to connect to data from within your code. Similar to triggers, you specify connection strings and other properties in your function config- uration. Bindings are optional, and a function can have multiple input and output bindings.
Using triggers and bindings, you can write code that’s more generic and doesn’t hardcode the details of the services with which it inter- acts. Data coming from services simply become input values for your function code. To output data to another service (such as creating a new row in Azure Table Storage), use the return value of the method. See Figure 1 for a listing of all supported triggers and bindings.
Azure Functions is particularly well suited to applications with bursty workloads, like orchestration and automation tasks. In the Infrastructure-as-a-Service (IaaS) world, these infrequently requested tasks are often bundled in with other services to maxi- mize virtual machine (VM) usage, or run on expensive dedicated VMs that are idle most of the time. For example, a synchronization service might get 10,000 requests once a day, exactly at midnight; Azure Functions is perfectly suited to handle that uneven load. The
This article discusses:
• Why you should choose serverless
• General availability of Azure Functions Proxies, and the rest of
the Azure Functions toolbox
• Best practices, design tips and step-by-step guide to building a serverless API
• Advanced tools and techniques
Technologies discussed:
Azure Functions, Azure Functions Proxies, REST APIs
42 msdn magazine














































































   44   45   46   47   48