Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Azure Serverless Computing Cookbook

You're reading from   Azure Serverless Computing Cookbook Build and monitor Azure applications hosted on serverless architecture using Azure Functions

Arrow left icon
Product type Paperback
Published in Nov 2018
Publisher Packt
ISBN-13 9781789615265
Length 424 pages
Edition 2nd Edition
Tools
Concepts
Arrow right icon
Authors (2):
Arrow left icon
Jason Marston Jason Marston
Author Profile Icon Jason Marston
Jason Marston
Praveen Kumar Sreeram Praveen Kumar Sreeram
Author Profile Icon Praveen Kumar Sreeram
Praveen Kumar Sreeram
Arrow right icon
View More author details
Toc

Table of Contents (13) Chapters Close

Preface 1. Developing Cloud Applications Using Function Triggers and Bindings FREE CHAPTER 2. Working with Notifications Using the SendGrid and Twilio Services 3. Seamless Integration of Azure Functions with Azure Services 4. Understanding the Integrated Developer Experience of Visual Studio Tools 5. Exploring Testing Tools for the Validation of Azure Functions 6. Monitoring and Troubleshooting Azure Serverless Services 7. Developing Reliable Serverless Applications Using Durable Functions 8. Bulk Import of Data Using Azure Durable Functions and Cosmos DB 9. Implementing Best Practices for Azure Functions 10. Configuring of Serverless Applications in the Production Environment 11. Implementing and Deploying Continuous Integration Using Azure DevOps 12. Other Books You May Enjoy

Building a backend Web API using HTTP triggers

We will use the Azure serverless architecture to build a Web API using HTTP triggers. These HTTP triggers can be consumed by any frontend application that is capable of making HTTP calls.

Getting ready

Let's start our journey of understanding Azure serverless computing using Azure Functions by creating a basic backend Web API that responds to HTTP requests:

We will be using C# as the programming language throughout this book. Most of these functions are developed using the Azure Functions V2 runtime. However, there are a few recipes that are not yet supported in V2 runtime, which is mentioned in the respective recipes. Hopefully, by the time you read this book, Microsoft will have made those features available for V2 runtime as well.

How to do it...

Perform the following steps:

  1. Navigate to the Function App listing page by clicking on the Function Apps menu, which is available on the left-hand side.
  2. Create a new function by clicking on the + icon:
  1. You will see the Azure Functions for .NET - getting started page, where you will be prompted to choose the type of tools you would like to use. You can choose the one you are the most interested in. For the initial few chapters, we will use the In-portal option, where you can quickly create Azure Functions right from the portal without any tools. Later, in the following chapters, we will use Visual Studio and Azure Functions Core Tools to create our functions:
  1. Next select More templates and click on Finish and view templates, as shown in the following screenshot:
  1. In the Choose a template below or go to the quickstart section, choose HTTP trigger to create a new HTTP trigger function:
  1. Provide a meaningful name. For this example, I have used RegisterUser as the name of the Azure Function.
  2. In the Authorization level drop-down, choose the Anonymous option. We will learn more about the all authorization levels in Chapter 9, Implementing Best Practices for Azure Functions:
  1. Click on the Create button to create the HTTP trigger function.
  1. As soon as you create the function, all the required code and configuration files will be created automatically and the run.csx file will be opened for you so that you can edit the code. Remove the default code and replace it with the following code. I have added two parameters (firstname and lastname), which will be displayed in the output when the HTTP trigger is triggered:
#r "Newtonsoft.Json"

using System.Net;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Primitives;
using Newtonsoft.Json;

public static async Task<IActionResult> Run(
HttpRequest req,
ILogger log)
{
log.LogInformation("C# HTTP trigger function processed a request.");
string firstname=null,lastname = null;
string requestBody = await new StreamReader(req.Body).ReadToEndAsync();

dynamic inputJson = JsonConvert.DeserializeObject(requestBody);
firstname = firstname ?? inputJson?.firstname;
lastname = inputJson?.lastname;

return (lastname + firstname) != null
? (ActionResult)new OkObjectResult($"Hello, {firstname + " " + lastname}")
: new BadRequestObjectResult("Please pass a name on the query" + "string or in the request body");
}
  1. Save these changes by clicking on the Save button, which is available just above the code editor.
  2. Let's try and test the RegisterUser function using the Test console. Click on the Test tab to open the Test console:
  1. Enter the values for firstname and lastname in the Request body section:

Make sure that you select POST in the HTTP method drop-down.

  1. Once you have reviewed the input parameters, click on the Run button, which is available at the bottom of the Test console:
  1. If the input request workload is passed correctly with all the required parameters, you will see a Status 200 OK, and the output in the Output window will be like what's shown in the preceding screenshot.

How it works...

We have created the first basic Azure Function using HTTP triggers and made a few modifications to the default code. The code just accepts the firstname and lastname parameters and prints the name of the end user with a Hello {firstname} {lastname} message as a response. We also learned how to test the HTTP trigger function right from the Azure Management portal.

For the sake of simplicity, I didn't perform validations of the input parameter. Make sure that you validate all the input parameters in the applications that are running on your production environment.

See also

The Enabling authorization for function apps recipe in Chapter 9, Implementing Best Practices for Azure Functions.

You have been reading a chapter from
Azure Serverless Computing Cookbook - Second Edition
Published in: Nov 2018
Publisher: Packt
ISBN-13: 9781789615265
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime