Azure – Create and Deploy Azure Resource Manager Template using Visual Studio 2017
If you missed our following articles on Exam : Developing Microsoft Azure Solutions, Please have a look once here :
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…
- Now lunch Visual Studio, and select File — New 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.
- 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.
- 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.
- Open WindowsVirtualMachine.json file. This file contains 3 sections as Parameters, Variables and Resources. We can see the same in JSON Outline window of visual studio, as shown in following figure.
- 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.
- 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 .
- Right click on the project and click Deploy — New to start with deployment, as shown in the following figure.
- 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.
- 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.
- 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.
- 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.
- After installing both, we redeployed the template and it ask for VM adminPassword through PowerShell .After providing the password, it successfully deployed the template.
- 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.
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