Tuesday, April 16, 2019

Create an Azure Function and triggered by a Blob Trigger


Serverless - Azure Functions


Azure Functions triggered by Blob Trigger – Hands on activity 


In a previous article, we came across the Serverless Computing approach that allows you to build and run applications and services without thinking about hardware, software or any that type of bits & pieces. In fact, Serverless computing is an event-driven application design and deployment model in near-real time, where computing resources provided as scalable cloud services.

Once we talk about Serverless, it means the code is running essentially on this platform and it is waiting for execution orders. Azure Functions is one of Serverless components that serves a platform to execute code based on events that you specify. By using Azure Functions, you can execute a script or piece of code in response to a variety of events and you do not have to worry about the infrastructure platform.

Microsoft Azure Functions


Next, onwards in this article, we will deep dive to do some hands on activity with Azure Functions and trigger. Here, you can see how to execute Azure Functions triggered by the Blob Trigger, whenever any file being uploaded to a Storage Blob. Since we have already covered in one of the articles, the provisioning of an Azure Storage Blobs, so will focus the following tasks – 
  • Create an Azure Function
  • Configure Blob Trigger
  • Execute and validate Azure Function by using uploading files to Storage Blob.


Meanwhile, if required, then you can visit few previous articles to have a brief awareness about the following artifacts – 


Pre-requisites


Before moving ahead, we need some pre-requisites to accomplish this Azure Functions and Blob Trigger hands on activities on top of Azure Cloud.
  1. Azure subscription, if you don't have an account then sign up for a free Azure account - https://azure.microsoft.com/en-gb/free/
  2. Microsoft Azure Storage Explored, you can download and install the same using - https://storageexplorer.com/


STEP – 1: Create an Azure Function App 


Login to the Azure portal https://portal.azure.com/

In the Microsoft Azure portal, click the + Create a resource from the Hub and click the Compute from the Azure Marketplace. It will load all available services under the Featured section, select the Function App or can search from the search box.

Function App


Promptly the Function App blade will be loaded, the time you click the Function App and the required details needed to submit for the following properties – 
  • App name – Name that identifies your function app.
  • Subscription – Name of your Azure subscription, it would be either free, paid or specific subscription etc. 
  • Resource group – Name of the resource group used to organize related resources, you can either create a new resource or choose an existing one.
  • OS – Go with Windows because Serverless hosting is currently only available when running on Windows.
  • Hosting Plan – Go with the default Consumption Plan, resources are added dynamically as required by your functions.
  • Location – Ideally, select the same location as your storage account.
  • Runtime Stack – Choose a preferred language, will go with the default .NET for C#.
  • Storage – Create a new or use an existing storage account used by your function app.


Azure Function

Next, required to configure the Application Insights resource of the same App name. By expanding this setting, you can change the New resource name or choose a different Location.

Application Insights


Now, nearly all properties have filled up; proceed by clicking the Create button to create the Azure Function App instance. Post validation, it will be deployed within few minutes. In the Azure Portal, you can view the notification and sooner you will be notified once Function App is created successfully. 

Once deployed, go and view the app.

Azure Function Created

STEP – 2: Choose a Development Environment 


Now Azure Function App has created, time to create a desired function. Expand your function app and click the + button next to Functions or can click the + New function from the Overview section.

Function Overview


By clicking the + button, Quickstart tab will be launched, and you can get the Azure Functions for .NET - getting started as a label.

Function - Quickstart


I trust, this is the first function in your function app, so scroll down a little bit more and select the In-portal and proceed by clicking the Continue button.

In-portal

STEP – 3: Choose Storage Blob trigger 


Once you click the Continue button, Azure will launch few by default trigger or a template of the function. However, we need to create a Blob based trigger so go ahead and click the More templates, then Finish and view templates subsequently.

More templates


It will display all available template, required to scroll the templates and choose the Azure Blob Storage trigger or you can type blob in the search field and choose the same.

Azure Blob Storage trigger template


Once you select it, might be it prompted as Extensions not Installed.  Go ahead and install the same by clicking the Install button. 

Extension Installation

Installation can take some time depends on template dependencies, once see the Continue button, then proceed ahead by clicking this.

Extension Installation

STEP – 4: Create a Function 


In previous steps, you launched the Azure Blob Storage trigger blade, now time to create a new function by providing following properties details – 
  • Name – Unique function name.
  • Path – The path within your storage account that the trigger will monitor.
  • Storage account connection - You can use the storage account connection already being used by your function app, or create a new one.


New Function


If required to create a new storage, proceed further by clicking the new link that will launch a new Storage Account blade.

Storage Account


Here you can see a proposed account; select the same that will be synced-up with this function and provide a name for the container.
  
New Function


Go ahead and click the Create button to finalize the settings. Later on the move in your function, click Integrate, expand Documentation, and validate the Account name and Account key that already pre-populated, copy the same for further connecting the storage account.

Documentation

STEP – 5: Connect to Microsoft Azure Storage Explorer 


Now you have a blob trigger and a blob container, you can validate the function by uploading a file to the container. Move to Microsoft Azure Storage Explorer utility, click the connect icon on the left that will launch Connect to Azure Storage page contains a couple of options.

Connect to Azure Storage


Now select the Use a storage account name and key option and click the Next button that will launch Connect with Account Name and Key blade. Here required to submit the Account name and Account key details, you can enter the same details, which you had copied in the last steps during the function creation.

Connect with Name and Key


Proceed ahead to be connect by clicking the Next button, once you provided the account details, sooner you can see summary details.

Connection Summary


Click the Connect, if you see all details are appearing correct. 

STEP – 6: Create Blob Container 


Navigate to the storage account and expand it, and select the storage connector and right click the Blob Containers, move ahead by clicking the Create Blob Container.

Create Blob Container


Next, provide the same that you had given during creation of a function named function-data; it will create a Blob Container with this name.

Create Function


Now blob container exists and you can test the function by uploading a file to the container.

Container Created


STEP – 7: Validate the Function 


Navigate back to the Azure Portal; expand the Logs under that comes at the bottom of the function page. Before proceeding ahead, make sure, the log streaming is in running mode.

Logs


Next, move to Storage Explorer and select the newly created Blob Containers the function-data, click Upload and upload files.

Upload files

Further, click the Upload button once you selected file through the Upload files dialog box, sooner you can see the list of the uploaded files.

Files Uploaded

Go back to your function logs and verify that the Blob trigger function has been processed and the blob has been read subsequently.

Function Triggered

Congratulations, Blob trigger function executed successfully! 😊

Here you have gone through some hands on activity on top of Azure Function and Storage Blob by creating a function that runs when a blob is being added to or updated in Blob storage. Since the Function App runs in the default Consumption plan, so a delay expected between the blob being added or updated and the function being triggered.

In the next article, we will walk through the Visual Studio and Azure Function hands on activities. Keep visiting the blog.


1 comment:

  1. A very interesting article! These things are fun. It is true that I'm just starting to enter it, but I'm already wondering what to do in my company. I'm still reading about cloud solutions. check it. It seems to be a great solution :)

    ReplyDelete