Azure – Create and Deploy Azure Resource Manager Template using Visual Studio 2017

Hello Friends,

If you missed our following articles on Exam : Developing Microsoft Azure Solutions, Please have a look once here :

Azure – Azure Automation State Configuration

Azure – Configure Azure PowerShell Desired State Configuration (DSC) Through Azure Portal

Azure – Configure Azure PowerShell Desired State Configuration (DSC) Using PoserShell

Azure – Azure Resource Manager (ARM)

Azure – Create and Deploy Azure Resource Manager Template using Azure Portal

In our last blog, we have discussed How to Create and Deploy Azure Resource Manager Template using Azure Portal. Today we will see, how to create and deploy an ARM template using Visual Studio 2017. Azure development tools provides a project template for Visual Studio to create the ARM templates. Let’s proceed with the article step by step and see how it works.

I was going through one article on DevOps, there was an interesting word “Infrastructure as Code” and the Azure ARM Templates are good examples of “Infrastructure as Code”. As we know, ARM template (it would be a .json file) will be used to deploy our resources to different environments such as testing, staging, and production.

For this demo, I will use the Visual Studio 2017 Community edition. So Let’s install it if not yet and go through the following steps…

  1. Now lunch Visual Studio, and select FileNew Project and under the C# branch, select “Cloud”, and “Azure Resource Group” project template.
    This template creates an Azure Resource Group deployment project. It contain component needed to provision Azure resources using Azure Resource Manager that will create an environment for our application.

    Create Resource Group Project in VS

    Figure 1: Azure Certification 70-532 -Create new VS project

  2. From Select Azure Template window, select the resource template that we need for our demo/project/solution. we can start with blank or select from the available templates from this project templates. Also we can get numerous templates available from Github . Here we will select “Window Virtual Machine” template and click Ok, as shown in the following figure.

    Create Resource Group Project in VS
    Figure 2: Azure Certification 70-532 -Select ARM template

  3. After click Ok button, it creates following 3 files.
    • Deploy-AzureResourceGroup.ps1 is a PowerShell script that invokes PowerShell commands to deploy to Azure Resource Manager. Note that Visual Studio uses this PowerShell script to deploy your template to Azure..
    • WindowsVirtualMachine.json is the Resource Manager template that defines the infrastructure we want deployed to Azure, as well as the parameters we provide during deployment. It also defines the dependencies between the resources so that Resource Manager will deploy the resources in the correct order
    • WindowsVirtualMachine.parametes.json is a parameters file that contains values needed by the template. we pass in parameter values to customize each deployment.

    Three file createdFigure 3: Azure Certification 70-532 -Three file created

  4. Open WindowsVirtualMachine.json file. This file contains 3 sections as ParametersVariables and Resources. We can see the same in JSON Outline window of visual studio, as shown in following figure.

    WindowsVirtualMachine.json file in JSON OutlineFigure 4: Azure Certification 70-532 – WindowsVirtualMachine.json file in JSON Outline

  5. We can customize a deployment project by modifying the JSON templates that describe the resources we want to deploy. Modify WindowsVirtualMachine.json file to set values of variables,parameters or modify other properties if required and save it. Following figure is a sample of template json file.

    Modify Arm Template.json fileFigure 5: Azure Certification 70-532 -Modify WindowsVirtualMachine.json file file

  6. Open WindowsVirtualMachine.parametes.json file and set all parameter values if required and save it as shown in following figure. We can also leave it , if we don’t want to pass any parameters from here .

    Set parameters in parameter.json fileFigure 6: Azure Certification 70-532 -Set parameters in parameter.json file

  7. Right click on the project and click DeployNew to start with deployment, as shown in the following figure.

    Deploy to Azure from solutionFigure 7: Azure Certification 70-532 – Deploy to Azure from solution

  8. Select Add an account and log in using the Microsoft account associated with our Azure subscription. We are going to deploy our template to our target Resource Group.In “Deploy to Resource Group” window, we have options to select existing resource group or create new resource group for our deployment. Here we are creating a new resource group as shown in the following figure.

    Create new Resource GroupFigure 8: Azure Certification 70-532 – Create new Resource Group

  9. Once we created our new Resource Group, lets check it through Azure Portal. In the following figure we can see, the newly created resource group has 0 item and it is completely empty.

    New Resource Group before deploymentFigure 9: Azure Certification 70-532 – New Resource Group before deployment

  10. If we didn’t update the parameters.json file earlier and now we want to make changes to parameters, we can do it using “Deploy to Resource Group” window by clicking the “Edit parameters” button as show in following figure.

    Set parameters in Edit parameters windowFigure 10: Azure Certification 70-532 -Set parameters in Edit parameters window

  11. Once everything is ready,click Deploy to start with deployment. Our deployment fail and when we check the details from Output window, we found we need to install AzureRM.BootStrapper and AzureRM.Profile .In following figures showing the error we got and it’s fixes.

    Error for AzureRM.Bootstrapper Figure 11: Azure Certification 70-532 -Error for AzureRM.Bootstrapper
    Error for AzureRM.ProfileFigure 12: Azure Certification 70-532 -Installing AzureRM.Profile

  12. After installing both, we redeployed the template and it ask for VM adminPassword through PowerShell .After providing the password, it successfully deployed the template.

    Deploying successfullyFigure 13: Azure Certification 70-532 – ARM template Deploying successfully

  13. Once we have successfully done with our deployment, lets check the status of the target resource group in Azure Portal. As shown in the following figure, the deployment is done successfully and it has deployed our required resources in the resource group.

    Successfully created resources in resource groupFigure 14: Azure Certification 70-532 – Successfully created resources in resource group

In our next blog we will share, How to add new resource to the existing ARM Template using visual studio.

Thanks for reading 🙂

Keep reading, share your thoughts, experiences. Feel free to contact us to discuss more. If you have any suggestion / feedback / doubt, you are most welcome.

Stay tuned on Knowledge-Junction, will come up with more such articles



2 comments on “Azure – Create and Deploy Azure Resource Manager Template using Visual Studio 2017”

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.