Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Building API in the cloud using Azure Functions

Building API in the cloud using Azure Functions

Building API in the cloud using Azure Functions.

Aleksandar Bozinovski

November 17, 2018
Tweet

Other Decks in Programming

Transcript

  1. Agenda • Journey to Azure Functions • Developing Azure Functions

    in Azure Portal • Developing Azure Functions with Visual Studio • Hosting and pricing • Typical usage scenarios
  2. App Service • Hosting applications inside of IIS (Internet Information

    Services) on Windows • Needs an App Service Plan • The App Service Plan defines the hardware resources • Made to support mainly web applications • Difficult to run • Batch processing • Scheduled tasks • Long-running processes
  3. Web Jobs • Built-in triggers • Azure Storage Queue •

    Service Bus • Schedule tasks • Needs an App Service Plan • The App Service Plan defines the hardware resources • Difficult to scale
  4. Azure Functions • More triggers • Cosmos DB • Event

    Hub • HTTP • Web Hook • Pay-per-use • Doesn’t need an App Service Plan • Although can run on a App Service Plan too • Easy to scale
  5. C# Script - CSX • Automatically referenced • mscorlib •

    System • System.Core • System.Xml • System.Net.Http • Microsoft.Azure.WebJobs • Microsoft.Azure.WebJobs.Host • Microsoft.Azure.WebJobs.Extensions • System.Web.Http • System.Net.Http.Formatting • Platform packages, use #r directly • Newtonsoft.Json • Microsoft.WindowsAzure.Storage • Microsoft.ServiceBus • Microsoft.AspNet.WebHooks.Receivers • Microsoft.AspNet.WebHooks.Common • Microsoft.Azure.NotificationHubs
  6. Azure Functions Tools for Visual Studio • Edit, build, and

    run functions locally. • Publish directly to Azure. • Use attributes to declare function bindings directly in the C# code. • Develop and deploy pre- compiled C# functions. • Included in the Visual Studio 2017 version 15.5
  7. Azure Function App • Based on .NET class libraries •

    Get the full power of IntelliSense, unit testing, and local debugging
  8. Used APIs and libraries • Faces API • Project Oxford

    SDK. • CoreCompat GDI (our Azure Functions are in .NET Core).
  9. Azure Functions hosting • Consumption plan • Instances are dynamically

    added and removed. • Pay only when your functions are running. • Scale out automatically, even during periods of high load. • App Service plan • Function apps run on dedicated App Service VMs. • Pay for the utilized resources under the App Service plan. • Functions can run longer than the maximum execution time allowed on the Consumption plan (of 10 minutes).
  10. Azure Functions scaling • Up to 200 instances. • A

    single instance may process more than one message or request at a time. • New instances will only be allocated at most once every 10 seconds.
  11. Azure Functions billing • Based on per-second resource consumption and

    executions METRIC PRICE FREE GRANT (PER MONTH) Execution Time* $0.000016/GB-s 400,000 GB-s Total Executions* $0.20 per million executions 1 million executions
  12. Usage 1 – as Web API • HTTP Trigger Function

    == Web API Action * // Endpoint URL: /api/products/{id} public static async Task<HttpResponseMessage> Run( [HttpTrigger(AuthorizationLevel.User, "get", Route = "products/{id}")] HttpRequestMessage req, int id, TraceWriter log) { var msg = new { Id = id, Message = "Hello World" }; return req.CreateResponse(HttpStatusCode.OK, msg); } // Endpoint URL: /products/{id} [Route("products/{id}")] [HttpGet] public async Task<IActionResult> GetProduct(int id) { var msg = new { Id = id, Message = "Hello World" }; return Ok(msg); } * Except that is not!
  13. Usage 1 – as Web API • Functions are always

    static methods. • Functions always receive HttpRequestMessage instance as a parameter. • DI not natively supported! • Benefits in scaling and maybe in pricing due to micro billing.
  14. Usage 2 – for scheduled tasks • Only one instance

    runs at a time. • Executions do not overlap. • Timer history kept in storage.
  15. Usage 3 – for backend event-based processing • Email when

    VM changes. • By using Azure Event Grid. • Process uploaded images. • By using Blob Trigger. • Respond to Service Bus event. • By using Azure Service Bus.