Automate the Boring Stuff with Azure Resource Manager Template

If you are using Microsoft Azure Portal for your database, your web app, or any other resources, you can find useful to use Azure Resource Manager Templates (ARM templates) in order to deploy resources faster.

Those ARM templates can be run as script and can be included in a build/deployment pipeline easily. Therefore, you can quickly deploy resources for your new release, for your new development environment, or for whatever reason ! 

Here is a little tutorial to starts using ARM ! 

0. Prerequisite

Before creating a script, we need an Azure environment. Here, I will take the example of a simple Java Web App interacting with a Storage Account.  The idea is, that every time I have to deploy a new release, I need to update my application. Depending on the complexity of your system, you can use a lot of different components that are all interdependent (or not). An ARM (Azure Resource Manager) script will therefore allow you to create/update/delete those components as a whole and as many times as you want.

If you do not have an Azure account already, you can create a free one here.

For the third part of this tutorial, you also have to install Azure Cli. You can find the documentation for windows installation here.

The complete ARM Documentation is available here

1. Create the project

You can easily start by creating an Azure Resource Manager Project with Visual Studio. Select File > New Project and select an Azure Resource Group model (don't worry about the type C# or Visual Basic because it has no impact on the project)


Once you click on Ok, a popup appears to select an Azure Model. To fully understand what we are doing, select the Blank Template. But you can select a more relevant template based on your needs. 


The azuredeploy.json file is as follows : 

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

$schema and contentVersion are the current versions of the ARM script used ; parameters is where we are going to put all our parameters that we need to gives to the script ; variables is for defining multiples values that will be used in the template ; resources will content our different components ; and outputs is a specific place to specify returning values when the script as been run.

The azuredeploy.parameters.json file is describe as follows :
{
"$schema":"https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {

     }
}

In this file, we are going to put all of our parameters values. This is very handy since it is an easy way to update our parameters without changing the main script.

2. Describe our Web App

Now that we have our blank template, we can start working ! First, we describe our web app. You can use Visual Studio by right click on resources(0) in the Structure JSON explorer and select Add Resource


Select a Web App and choose a name. If you have no App Service, you can also create it. In my example, I create a new one called "my-appservice". Then click on the Add button. 


When this is done, you should see that your script (azuredeploy.json) have been updated. We now have two new parameters, one variable, and our two resources : 


We then add in the parameters file the value for the my-appServiceName parameter : 

"parameters": {
    "my-appServiceName": {
        "value": "appServiceCreatedWithArm"
    }
}

(You can also override the my-appServiceSkuName if you want, but he is set by default to F1.)

3. Deploy your script

Now that we have created our first script, we need to run it.

Open a terminal window and locate yourself in the repository of azuredeploy.json and azuredeploy.parameters.json. You can then run the following command to deploy your script inside your <resource group name> :

az group deployment create --name <deployment name> --resource-group <resource group name> --template-file azuredeploy.json --parameters azuredeploy.parameters.json

If you don't provide the parameters file, you will have to specify the parameters values inside the command prompt.

In your Azure Portal, you can now see your deployed resources inside the resource-group you specified :


Hope this really quick tutorial give you a lot of idea in order to secure your Azure environment !


No comments:

Post a Comment

Improve the Configuration of Docker Compose with Environment Variables

I recently started working on a new python project (yeah!). This project is really interesting, but the first lines of code are at least a d...