Creating an Azure Function in the Portal

Lab Steps

lock
Logging in to the Microsoft Azure Portal
lock
Creating an Azure Function App in the Portal
lock
Creating an Azure Function in the Portal
lock
Testing the Azure Function
lock
Logging and Monitoring Azure Functions
Need help? Contact our support team

Here you can find the instructions for this specific Lab Step.

If you are ready for a real environment experience please start the Lab. Keep in mind that you'll need to start from the first step.

Introduction

There are several ways to create and deploy Azure functions. In production, you would probably configure a deploy mechanism to allow Azure Functions to get the latest version of code from your version control system. Azure Functions integrates with GitHub and other version control systems for that. For this Lab, you will use the simplest method: editing the source code directly in Azure Functions in the Portal. The function you create in this Lab will integrate Azure Blob Storage with Azure Table Storage. Any time a blob is uploaded to a Blob Storage container, a corresponding row in Table Storage will be created. The function will highlight the concept of binding and demonstrate how easy it is to integrate services with Azure Functions.

 

Instructions

1. Click on Functions in the left menu and then on + Add.

 

2. Observe the various templates available before clicking Azure Blob Storage trigger:

alt

 

3. Under Template details, fill out the following values before clicking Add:

  • NameUploadTrigger
  • Pathuploads/{name} (uploads is the name of the blob storage container where you want to monitor for blobs. The uploads container has been pre-created when you started the Lab. {name} is a pattern that will match the name of any blob in the container.)

alt

Once the Function is created, click on Code + Test and you will be displayed the code that the Function runs:

alt

The code simply logs a line about the blob that triggered the function.

 

4. Look at the upper part of the console and click on the dropdown, then click function.json:

alt

The function.json file stores the bindings for the C# script function. The binding encapsulates what triggers your Function and the data associated with it. In this case, any new blob added to the uploads/ path will trigger the function (blobTrigger) and the input (in) binding will populate the Function input parameters myBlob and name (as seen in the first line of the run.csx C# script).

There are also output bindings. You want to store the blob information into a table and use an output binding to achieve that.

 

5. Add the following output binding to the bindings list in function.json:

Copy code
    ,
{
"name": "$return",
"type": "table",
"direction": "out",
"tableName": "uploads",
"connection": "AzureWebJobsStorage"
}

The complete file should look as follows:

alt

The $return name indicates that the table binds to the return value of the function. More details about Table Storage binding is available here.

 

6. Click Save to save the function.json file.

 

7. Move to the run.csx file and replace the code with the following that returns an object of a new class called Upload:

Copy code
public static Upload Run(Stream myBlob, string name, ILogger log)
{
log.LogInformation(
quot;C# Blob trigger function Processed blob\n Name:{name} \n Size: {myBlob.Length} Bytes");

return new Upload() {
PartitionKey = "Uploads",
RowKey = Guid.NewGuid().ToString(),
Name = name,
Length = myBlob.Length
};
}

public class Upload
{
public string PartitionKey { get; set; }
public string RowKey { get; set; }
public string Name { get; set; }
public long Length { get; set; }
}

The returned Upload object is what is used for the output bind to Table Storage. The binding uses the PartitionKey and RowKey properties to create the partition and row key of the entity stored in the table. A timestamp will automatically be added by the table. Notice how the trigger and output binding are entirely described in the function.json file. There is no code in run.csx that indicates there is any binding taking place. This separation helps keep the code clean and more maintainable.

 

8. Click Save.

 

Summary

In this Lab Step, you created an Azure Function that is triggered whenever a blob is created in an uploads storage container and writes a corresponding row to a table in Azure Table Storage. You learned about triggers and bindings and how they make event processing with Azure Functions easy. You will test the Function in the next Lab Step.

Validation checks
1Checks
Created Azure Function App

Check if the Azure Function App has been created

Azure Functions