Automation on Azure using Resource Manager

As you probably already heard cloud computing is well designed for dev & test scenarios. Each developer could create and deploy his or her own environment and isolate development of new feature. The power of cloud has a great keen sense of automation tests. For each test you can provision all resources and services at the beginning run the test and finally delete whole environment. And the best you can do it in parallel. If you are only developer and you do not run integration tests the automated provisioning is still the best way how to create environment. With the Azure Resource Manager this is a simplest task. You can choose between imperative approach using Powershell or CLI, or a declarative way by defining the resources in JSON files.

basho-arm101-0.png

In this article I will cover 101 of Azure Resource Manager, specifically how to create a template using Visual Studio project. I will create template for an environment hosting simple Web App with database as data tier. I will add all resources to the template in the default setting and at the end I will modify auto-generated JSON file manually.

Application composition

I will show the Azure Resource Manager template at the most simplest web application. There will be only 2 main components, web server and database.

From usability standpoint user will run web application in browser and our web application is generally accessible via its URL. User needs to be logged in to display or modify his data.

From technical point of view there is not other public endpoint. Application database is only accessible from backend of application running on web server. For the simplicity of solution I will not define more components neither for functionality nor for performance or security.

Create Visual Studio project

Before we start. If you do not have a Visual Studio 2015, you can install its community edition from here which is free to use. When you have Visual Studio installed you will need Microsoft Azure SDK for .NET currently latest version 2.8.2. And finally you will need Azure account, e.g. you can obtain free trial subscription here.

Run the Visual Studio and select Create new project command. In the dialog select Visual C# project templates and Cloud subsection. Select Azure resource Group project. Slect the project location and choose project name and press OK.

basho-arm101-1.png

The Azure Template selection dialog will appeared. There are a few predefined templates if you want to save some time, but for now please select the Blank at the bottom of the list.

basho-arm101-2.png

Now we have a solution with one project opened in Visual Studio. Let’s take a look into the files we have associated with project in the Solution Explorer. If it is not shown by default, go to the menu View -> Solution Explorer.

basho-arm101-3.png

There are 3 directories. The first ‘Scripts’ includes Powershell script ‘Deploy-AzureresourceGroup.ps1’ which is deploying and starting the Azure Templates. The last ‘Tools’ includes ‘AzCopy.exe’ utility which copies files from local to Azure Storage account container. And finally ‘Templates’ includes 2 template files.

  • ‘azuredeploy.json’ is template file declaring all Azure resources and services our deployment will provision.
  • ‘azuredeploy.parameters.json’ is file which could pre-define the values of template parameters. You can create as many files as you have environments you will just need to update Powershell script to use the correct one.

Structure of azuredeploy.json file

The default content of azuredeploy.json file is


{
  "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
  },
  "variables": {
  },
  "resources": [
  ],
  "outputs": {
  }
}

The main sections:

  • $schema – the location of json schema file which describes how the template is structured.
  • contentVersion – defines version of the template and during the deployment the version could be used to be ensured the correct template will be deployed.
  • parameters – can customize resource deployment and the values are provided at the beginning.
  • variables – can simplify expression used in resources and readability of json.
  • resources – resource types deploying or updating in Resource Group.
  • outputs – the value that is return after deployment.

For more details check out the Azure documentation Authoring Azure Resource Manager templates.

Add resources

As we defined at the beginning of article we will need 2 main resources Web App and Database and several other components to have our environment workable. I will use power of Visual Studio and Resource Group project. Open azuredeploy.json file in editor by double click on file in Solution Explorer.

At the right ‘JSON Outline’ window right click on ‘resources’ and popup menu will appear.

basho-arm101-5.png

Click ‘Add New Resource’.

In the dialog you can add all available resources by selecting them in the list at the right, filling a few edit fields and press ‘Add’ button which will immediately update the json file.

Web App

In the ‘Add Resource’ dialog select Web App resource. Fill its name e.g. ‘armWebApp’ and select existing Application Service Plan in json or create fresh new. Note: all Web Apps on Azure run on web server farm representing by Application Service Plan.

basho-arm101-7.png

In the editor you can see 3 parameters, 1 variable and 2 resources were created by default.

  • parameters
    • armAppServicePlanName – is type of string and represents name of creating AppServicePlan.
    • armAppServicePlanSKU – is type of string with predefined list of values and represents the type of creating web server farm. The default value is “Free”.
    • armAppServicePlanWorkerSize – is type of string and represents the size of web server. The default value is “0” what is the smallest server.
  • variable
    • armWebAppName – is type of string and represents name of creating Web Application. This name must be unique and thus it is concatenate from static string “armWebApp” + name of Resource Group under which deployment will be executed.
  • resources
    • “Microsoft.Web/serverfarms” – is resource definition for AppServicePlan.
    • “Microsoft.Web/sites” – is resource definition for WebApp.

Database

On the resources right click and select “Add New Resource”. In the dialog find SQL Server and name it “armDatabase”. You will need add database server as well.

basho-arm101-9.png

In the editor you can see new 6 parameters, 1 variable and 1 resource with 2 sub-resources were created by default.

  • parameters
    • sqlserverAdminLogin – is type of string and represents admin credentials for SQL server.
    • sqlserverAdminLoginPassword – is type of securestring and represents admin credentials for SQL server.
    • armDatabaseName – is type of string and represents name of creating database.
    • armDatabaseCollation – is type of string and represents a collation encodes the rules governing the proper use of characters for either a language, such as Greek or Polish, or an alphabet, such as Latin1_General (the Latin alphabet used by western European languages). The default value is SQL_Latin1_General_CP1_CI_AS.
    • armDatabaseEdition – is type of string with predefined list of values and represents the edition of Azure SQL server. The default value is “Basic”.
    • armDatabaseRequestedServiceObjectiveName – is type of string with predefined list of values and represents the size of database and pricing tier. The default value is “Basic”.
  • variables
    • sqlserverName – is type of string and represents name of creating SQL server. This name must be unique and thus it is concatenate from static string “sqlserver” + name of Resource Group under which deployment will be executed.
  • resources
    • “Microsoft.Sql/servers” – is resource definition for SqlServer.
      • “databases” – is resource definition for Azure SqlDatabase.
      • “AllowAllWindowsAzureIps” – is resource definition for firewall on SQL server allows all Azure resources to access the server.

Additional resources

You can add some more definition to better define your environment, typically connection string for the web application. Create it as sub-resource of web application and in the list of resources select the Application Setting for Web Apps.

Define parameters for deployment

For the deployment you can update azuredeploy.parameters.json file and of course you can create many such files that will be specific for each your environment.

Execute the deployment

It is very simple to execute the deployment by right click on Resource Group project file in Solution Explorer and select “New Deployment…” command.

basho-arm101-10.png

 

 

In the dialog select the subscription and Resource Group or create a new. Press “Edit Parameters…” and fill missing values, the changes will be saved into azuredeploy.parameters.json.

basho-arm101-11.png

After you press Deploy button the Powershell script will ask for SQL server’s administrator password parameter.

If the deployment successfully completes you will see created Resource Group and its resources in the Azure Portal. If deployment fails (as it happened to me several times), you can go to Azure Portal as well, open the Resource Group, click on Events and there click on latest deployment with error. In the list of deployment logs locate the errors and in the detail view you can find the error message.

Conclusion

In this article I have created Resource Group project in Visual Studio. There I defined resources for web application and database using visual editor. The deployment JSON file was created by Visual Studio, however it is possible to update the file manually to fit your needs. I used Visual Studio for simple deployment as well and created the environment on Azure. The whole creation process could be easily automated by running the Powershell script and parameterized deployment by azuredeploy.parameters.<someId>.json file.

 

Stay tuned.

Leave a Reply

Your email address will not be published. Required fields are marked *