Wednesday 8 August 2018

Developing Azure Functions in Visual Studio and Creating a Deployment Pipeline Using VSTS

Serverless computing has become a hot topic these days. It is providing you with paying only for the actual time your code is running and resources you are consuming without any worries about infrastructure. Azure functions let you build functions that can scale dynamically based on the needs, with a languages of your choice. When you install Visual Studio Azure Development workload you get templates for developing Azure Functions. Let’s look at how to create a simple Azure Function “Hello World” in Visual Studio and most importantly how to get your CI/CD pipeline ready within couple of minutes.

You can create an Azure Function app project using Visual Studio 2017. Then commit it into VSTS Git repo.image

Install continuous delivery extension for Visual Studio 2017 from Visual Studio Marketplace. 01


Click on the new icon appears on the Visual Studio tray and click on Configure Continuous Delivery to Azure.04.0

In the dialog provide your Microsoft account details and you would be able to see the team project Git  repository. Select the branch that you have committed your Azure Function code. Select you Azure Subscription and click Edit button to provide additional information for Azure Function app.04

You can provide new Azure Resource Group or select existing and provide other details such as storage account, hosting plan, and hosting plan size etc.05

Once you click OK Visual Studio will connect with VSTS and Azure, and will generate a build definition and a release definition. Execute a build and release and get the Azure Function deployed.06

The Advantage of generating the build and deployment pipeline will help you get started quickly. Let’s inspect tasks setup in these definitions to understand how the steps are performed so that it enables you to do necessary enhancements to support you real production scenarios such as adding additional environments, say QA, Staging or Prod, and adding further configurations etc.

NuGet Restore task will restore any NuGet packages used and Visual Studio build step will build and publish the app with the arguments passed to MSBuild. The build will package the Function app as a MSDeploy package. You may want to introduce package as NuGet package and push it to a package management feed as an enhancement. We can explore these options in a later post.

/p:DeployOnBuild=true /p:WebPublishMethod=Package /p:PackageAsSingleFile=true /p:SkipInvalidConfigurations=true /p:PackageLocation="$(build.artifactstagingdirectory)\\"


Visual Studio Test task will run any unit tests and publish symbols will enable debugging by making .pdbs available. More information on publishing symbols for debugging can be found here. Executed build will create a MSDeploy package as shown below.image

The continuous delivery pipeline also get automatically created and will have the Dev environment setup for deployment using the build artifacts.image

Azure App Service deployment task is configured supporting deployment of the Function App as shown below. The App Type is set to Function App and package from the build is used for deployment. The task allows you to use variable substitution etc. as further enhancements.image

The Service Endpoint created to connect with the Azure subscription is scoped to the resource group picked in the Visual Studio. You can inspect the service endpoint by clicking the Manage link next to the Azure Subscription and the opening the new service endpoint for update.image

You can verify the current connection and change scope or even define new service endpoints to target different Azure subscriptions depending on you target environment needs. For example you production environment can be totally different Azure subscription. Then you can clone the release pipeline auto generated environment to create the next step in the pipeline.image

Beauty of the Continuous Delivery extension to Visual Studio is it creates you entire CI/CD pipeline for Azure Function apps and even executes the first release to get it deployed to the first target environment. This enables you to get started really quick and do further enhancement as and when needed.image


Deployed function can be tested as shown below (This is just the template function created by Visual Studio as default when creating a function app project).image

No comments:

Popular Posts