Thursday, April 25, 2019

Create and Publish an Azure Function with Blob Trigger by using Visual Studio


Azure Functions and Visual Studio


Azure Functions triggered by Blob Trigger using Visual Studio 2017 – Hands on activity 


We have gone through that Azure Functions enables us execute small pieces of code or function on top of a Serverless environment as a cloud function. In simple words, required to focus on code and functionalities rather than worrying about cloud infrastructures. 

In last hands on activity, we walked through the creation of an Azure Function using the Blob trigger on top of In-portal platform that was a quick implementation and execution. Apart from the Azure Portal, you can develop Azure Functions using the latest release of Visual Studio 2017 version 15.4 or later. In fact, Azure Function Tools for Visual Studio 2017 is an extension for the VS IDE that lets you develop and deploy C# functions to Azure. Development under Visual Studio gives you lots of additional flexibility as well, where you can leverage all the IDE features including source control integration, testing, and other aspects. 

Have a look the following key benefits of Azure Functions Tools – 
  • Develop, build and run functions on the local development workstation.
  • You can publish the Azure Function directly to Azure.
  • You can use WebJobs attributes to declare function bindings in C# rather than maintaining a separate function.json file.
  • Develop and deploy pre-compiled C# functions.
  • During coding your functions in C#, you can leverage all benefits of VS IDE.


Here, you can see how to use the Azure Functions Tools for Visual Studio 2017 to develop C# functions and publish them to Azure. We will walk through a Blob trigger and validate the function using uploading files to the Blob container. However, we have already covered in many articles, the provisioning of an Azure Storage Blobs, so will focus the following tasks –
  • Develop C# functions and publish them to Azure
  • Configure Blob Trigger
  • Validate the function by using uploading files to the Blob container

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 by using Visual Studio 2017.
  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. An Azure Storage account.
  3. Microsoft Azure Storage Explorer (optional), you can download and install the same using - https://storageexplorer.com/
  4. Latest version of Visual Studio 2017 - https://visualstudio.microsoft.com/vs/


Validate the Azure Development Workloads 


Azure Functions Tools comes under Azure development Workloads of Visual Studio 2017 version 15.5 or later. It is essentials to ensure that you included this feature during Visual Studio 2017 installation. If not, then might be you will have to modify the installation.

Visual Studio Professional 2017


Validate the Tools Version 


Open the Visual Studio 2017, move to the Extensions and Updates under Tools menu. Here expand Installed - > Tools and choose the Azure Functions and Web Job Tools.

Azure Functions and Web Jobs Tools


Note the installed version, in my case it looks like an updated version. However, if it exists as an old version in your case update the tools by using – Extensions and Updates – > Updates - > Visual Studio Marketplace

STEP – 1: Copy the Connection String of Storage Account 


Here, in the activity we will work with Blob trigger so one Storage Blob account is required. Since we have covered multiple times how to provisioning a Storage account, from now on just moving to an existing Storage account.

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

On the Hub menu, click All services and select the storage account, required to browse and load its blade in the Azure portal. Here you can see all details and options under different sections like Essentials and Services.

Storage Account

Proceed ahead by clicking the Access Keys under Settings; you can see Key and Connection string details. Copy the Connection string for the Key 1 section, which will be used further to create a connection using Visual Studio 2017.

Access Keys


STEP – 2: Create an Azure Function Project in Visual Studio


Start a new instance of Visual Studio and click the Project from New under the File menu that will launch New Project dialog box. Select Azure Functions that available under Installed - > Visual C# - >Cloud template from the New Project dialog box.

Azure Functions


Once you have submitted the desired function name and location proceed ahead by clicking the OK button that will launch function (trigger) selection page.

Azure Functions v1 (.NET Framework)


Here you can see different type of available functions, select the Blob trigger and fill the required property details as – 
  • Storage Account – You can select an existing storage account or go with Storage Emulator.
  • Connection String setting – Connection String of the Storage Account.
  • Path – It would be Blob Container

Select the Blob trigger and click the Browse from the drop down list of Storage Account.

Blob trigger

If you are not connected with the Azure account, then it will launch the blade through which you can sign in the same using Add an account.

Add an account


Once you have connected, and then can select the existing desired Storage Account for this function activity. Select the Storage Account and move ahead by clicking the OK button.

Storage Account


Sooner the selected Storage account will be mapped under the Storage Account, further submit the Connection String that you copied and note down in the previous steps. Along with this, you can provide a new Path that will be a new Blob Container.

Blob trigger - Storage Account

Next, the project creation proceeds, the time you click the OK button and within a few moments the default class and other basic structure will be appeared.

Solution - Visual Studio

STEP – 3: Validate Connection String


Now C# project has been created, contains the following files – 
  • Function1.cs – default class file for the Azure Function, the Blob trigger.
  • host.json –You can use this file for the configuration of Function host, apply both the local as well Azure.
  • local.settings.json – It maintains the settings for running the function locally.


Next, validate the Storage Connection String that you had provided in the previous steps. Go to local.settings.json file, here the connection string should be populated.

local.settings.json


STEP – 4: Validate Function Class


By default the Function1.cs class has been created, you can change the class name as per feasibility. Once you try to rename the class name, the system will ask a confirmation. 

Renaming a file


Just go ahead by clicking the Yes button, promptly the class name will be reflected in the new class name. Along with this, you can rename the name of the function also from Function1 to anything as per your choice.

Renaming Function1

Make ensure about the Connection, which should be the exact you had submitted in previous steps for Storage Account.

In the code section, you can see a simple log code that display the acknowledgement of the item, which is being uploaded to the Blob container, like name and size. It is just a sample; here you can do a different activity as per necessities. 

Log code inside function


STEP – 5: Publish the Function to Azure


Time to deploy the function, right click the project and click the Publish.

Publish


Promptly system will ask to pick a publish target once you click the Publish option. It gives following options – 
  1. Azure Function App
  2. Folder


Pick a publish target


Go ahead and select Create New under Azure Function App, and click the Publish button. Sooner you can get a Create App Service page, and the required details needed to provide 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 or create a new Consumption Plan.
  • Storage Account– Go with the default dedicated Storage account used by your function app or create a new one. 


Create App Service


Once you select the preferred Hosting Plan then click the OK button, almost the App Service is ready to be provisioned.

Create App Service

Validate the submitted details and go ahead by clicking the Create button to create the Azure Function App instance. Sooner the App will get started to deploy, will take a few moments to configured Azure. Shortly, you will get Publish page synched-up the function and Azure.

Publish Page


Even you can validate the Azure Function App on the Azure portal, move to the All resources and select the same Function App that was created using Visual Studio.

Function App blade


Here you can see all details, like the Site URL and function details inside the trigger. You can validate the Function App, copy the URL and run in the browser.

Function App is running


Congratulations, your Function App is up & running! 😊

STEP – 6: Connect to Microsoft Azure Storage Explorer 


Now you have a Blob trigger and a Storage account, 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

Here select the Use a connection string option and click the Next button that will launch Attach with Connect String blade. Here required to submit the same Connection string, which you had copied and noted down in previous steps

Attach with Connection String


Proceed ahead to be connect by clicking the Next button, once you provided the Connection string, shortly you can see summary details.

Connection Summary


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

STEP – 7: 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 name that you had given during creation of a function named function-data under Visual Studio; it will create a Blob Container with this name.

Blob Containers

STEP – 8: Validate the Function 


Now desired Blob container exists and you can test the function by uploading a file to the container. Navigate back to the Azure Portal; select the Function App that was published from the Visual Studio end. 

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, quicker you can see the list of the uploaded files.

File Uploaded

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

Logs


Congratulations, Blob trigger function executed successfully! 😊

Here it was a quick walk-through on how we can create Azure Functions by using the Visual Studio 2017, though there are different types of triggers that you can explore more. Further, I will come with more articles synched-up with different Azure services.  

keep visiting the blog.


1 comment:

  1. The information which you have provided in this blog is really useful to everyone. Thanks for sharing.
    Azure Development Training in Hyderabad

    ReplyDelete