Book Image

Azure Serverless Computing Cookbook

By : Praveen Kumar Sreeram
Book Image

Azure Serverless Computing Cookbook

By: Praveen Kumar Sreeram

Overview of this book

Microsoft provides a solution to easily run small segment of code in the Cloud with Azure Functions. Azure Functions provides solutions for processing data, integrating systems, and building simple APIs and microservices. The book starts with intermediate-level recipes on serverless computing along with some use cases on benefits and key features of Azure Functions. Then, we’ll deep dive into the core aspects of Azure Functions such as the services it provides, how you can develop and write Azure functions, and how to monitor and troubleshoot them. Moving on, you’ll get practical recipes on integrating DevOps with Azure functions, and providing continuous integration and continous deployment with Visual Studio Team Services. It also provides hands-on steps and tutorials based on real-world serverless use cases, to guide you through configuring and setting up your serverless environments with ease. Finally, you’ll see how to manage Azure functions, providing enterprise-level security and compliance to your serverless code architecture. By the end of this book, you will have all the skills required to work with serverless code architecture, providing continuous delivery to your users.
Table of Contents (11 chapters)

Persisting employee details using Azure Storage table output bindings

In the previous recipe, you have learnt how to create an HTTP trigger and accept the input parameters. Let's now work on something interesting, that is, where you store the input data into a persistent medium. Azure Functions supports us to store data in many ways. For this example, we will store the data in Azure Table storage.

Getting ready

In this recipe, you will learn how easy it is to integrate an HTTP trigger and the Azure Table storage service using output bindings. The Azure HTTP trigger function receives the data from multiple sources and stores the user profile data in a storage table named tblUserProfile.

How to do it...

  1. Navigate to the Integrate tab of the RegisterUser HTTP trigger function.
  2. Click on the New Output button and select Azure Table Storage then click on the Select button:
  1. Once you click on the Select button in the previous step, you will be prompted to choose the following settings of the Azure Table storage output bindings:
    • Table parameter name: This is the name of the parameter that you will be using in the Run method of the Azure Function. For this example, please provide objUserProfileTable as the value.
    • Table name: A new table in the Azure Table storage will be created to persist the data. If the table doesn't exist already, Azure will automatically create one for you! For this example, please provide tblUserProfile as the table name.
    • Storage account connection: If you don't see the Storage account connection string, click on the new (shown in the following screenshot) to create a new one or to choose an existing storage account.
    • The Azure Table storage output bindings should be as shown in the following screenshot:
  1. Click on Save to save the changes.
  1. Navigate to the code editor by clicking on the function name and paste the following code:
        #r "Microsoft.WindowsAzure.Storage"
using System.Net;
using Microsoft.WindowsAzure.Storage.Table;

public static async Task<HttpResponseMessage>
Run(HttpRequestMessage req,TraceWriter
log,CloudTable objUserProfileTable)
{
dynamic data = await
req.Content.ReadAsAsync<object>();
string firstname= data.firstname;
string lastname=data.lastname;

UserProfile objUserProfile = new UserProfile(firstname,
lastname);
TableOperation objTblOperationInsert =
TableOperation.Insert(objUserProfile);
objUserProfileTable.Execute(objTblOperationInsert);

return req.CreateResponse(HttpStatusCode.OK,
"Thank you for Registering..");
}

public class UserProfile : TableEntity
{
public UserProfile(string lastName, string firstName)
{
this.PartitionKey = "p1";
this.RowKey = Guid.NewGuid().ToString();;
this.FirstName = firstName;
this.LastName = lastName;
}
public UserProfile() { }
public string FirstName { get; set; }
public string LastName { get; set; }
}
  1. Let's execute the function by clicking on the Run button of the Test tab by passing firstname and lastname parameters in the Request body as shown in the following screenshot:
  1. If everything went well, you should get a Status 200 OK message in the Output box as shown in the preceding screenshot. Let's navigate to Azure Storage Explorer and view the table storage to see if the table named tblUserProfile was created successfully:

How it works...

Azure Functions allows us to easily integrate with other Azure services just by adding an output binding to the trigger. For this example, we have integrated the HTTP trigger with the Azure Storage table binding and also configured the Azure Storage account by providing the storage connection string and the Azure Storage table name in which we would like to create a record for each of the HTTP requests received by the HTTP trigger.

We have also added an additional parameter for handling the table storage named objUserProfileTable, of type CloudTable, to the Run method. We can perform all the operations on the Azure Table storage using objUserProfileTable.

For the sake of explanation the input parameters are not validated in the code sample. However, in your production environment, it's important that you should validate them before storing in in any kind of persist medium.

We have also created an object of UserProfile, and filled it with the values received in the request object, and then passed it to a table operation. You can learn more about handling operations on Azure Table storage service from the URL https://docs.microsoft.com/en-us/azure/storage/storage-dotnet-how-to-use-tables.

Understanding more about Storage Connection

When you create a new storage connection (please refer to the third step of the How to do it... section of this recipe) a new App settings will be created as shown in the following screenshot:

You can navigate to the App settings by clicking on Application settings of the Platform features tab as shown in the following screenshot:

What is Azure Table storage service?

Partition key and row key

The primary key of Azure Table storage tables has two parts as follows:

  • Partition key: Azure Table storage records are classified and organized into partitions. Each record located in a partition will have the same partition key (p1 in our example).
  • Row key: A unique value should be assigned for each of the rows.
A clustered index will be created with the values of the partition key and row key to improve the query performance.

There's more...

Following is the very first line of the code in this recipe:

#r "Microsoft.WindowsAzure.Storage"

The preceding line of code instructs the function runtime to include a reference to the specified library to the current context.