Automation is the buzzword nowadays and when it comes to Cloud, it
becomes more inevitable. Creating various Azure resources and then
logically grouping them inside a Resource group is a very common task we
perform while working in Azure. We can go ahead and hit the Azure
portal and create all the necessary resources by simple clicks and set
up the environment in no time and get things running. Cool!
This may be good and acceptable for your development environment.
What about other environments such as QA,Staging or even Production. Is
it feasible to repeat the same task over and over again in each
environment and create the exact same stuff. What about human error.
Manual processes are largely error prone and a slight hiccup could lead
to a big costly mistake. Moreover you might not have permission to all
the environments and the same you might forget the exact resource
configurations few months down the line. So, in short there's hell lot
of things that can go wrong if things are manual.
Hence Automation. In this article I will walk you
through the steps of creating a simple ARM template containing an Azure
Storage resource in VS-2017 solution and then deploying the same with
the help of PowerShell to Azure Cloud. Hope you all would enjoy the
journey!
Please note that I have purposefully left out some complex
areas or haven’t deep dived into details at some places. This is just to
make this article an easy read and help as a quick guide from where to
start in creating and deploying ARM templates
Prerequisites:
- Microsoft Visual Studio 2017 with Azure SDK installed.
- Azure Subscription (Free subscription of 30 days will also do)
- Basic JSON understanding
- Windows PowerShell (Command Line or ISE)
Let's get Started
- Fire up your Visual Studio 2017
- File > New > Project. Click on "Cloud" in the left panel
and then select "Azure Resource Group" project type. Give the project
name, location as per your choice. Click OK
- Select "Blank Template" from the list of templates as of now. We will add resources later. Click OK
- A Resource Group project will be created as below.
The main entry point is the azuredeploy.json. This is where we will create all the resources along with its respective configurations. Next is azuredeploy.parameters.json
which is basically used to override any parameter that is defined in
the azuredeploy.json file. Anything that needs to be customized on a per
item basis should be placed here. Deploy-AzureResourceGroup.ps1 is the powershell script require to deploy the resources.
We will only make use the azuredeploy.json in this demo and perform
the other two steps a bit differently so to avoid any complexity and
help in easy understanding.
- Double-Click on the azuredeploy.json file to open the code window.
There are four main sections in the json file.
parameters : Here we will define all the
parameters respective to a resource in key-value pair instead of
hard-coding in the resource body.
variables : This is optional. These are
piece of information which the ARM template can work out on the fly.
Specially used to deploy similar resources in different environments.
resources : All resource code blocks will go into this section following the same structure and pattern.
outputs : This is optional. Used when
linking other ARM template with the master template or infact creating
linked templates. Will not use this in this demo.
- Once you opened the code view of the azuredeploy.json file,
VS-2017 will automatically open a JSON Outline window for powerful
navigation purpose.
If you do not see this windows then goto View > Other Windows > JSON Outline
- Now our target is to create a Azure Storage resource here. Right
click on resource section inside the JSON Outline window and select "Add
New Resource"
- Scroll down to find the "Storage Account" resource. Give it a name of your choice. I gave demoStorage but it can be any name. Click "Add"
- Visual Studio will add all the necessary boilerplate code
required for this resource type and will populate all the required
sections of the ARM template with some default values.
- Now lets do some minor modifications. The Storage name should be
unique like any other Azure resources. Visual Studio has tried to make
it unique by its own way but what if I want the storage name to start
with "demo" as a prefix. Let do this.
We will add one parameter name "demoStoragePrefix" as below in the parameters section
"demoStoragePrefix": {
"type": "string",
"minLength": 1,
"defaultValue": "demo"
}
In the variables section lets modify the demoStorageName variable to read the prefix from the parameter we just defined above.
"demoStorageName": "[concat(parameters('demoStoragePrefix'), uniqueString(resourceGroup().id))]"
Now also add an additional parameter name "metadata" in the parameters section.
This is optional and most people do overlook but I strongly recommend
to add this with every template as it makes the code more readable.
"metadata": {
"type": "string",
"defaultValue": "Creation of Storage Account"
}
- Now the new modified json code block looks like this with the above changes in place.
- Save the file and solution. We are done with our ARM template. Now lets dive into the deployment part.
Open PowerShell window (Command line or ISE window will do)
- Connect to your Azure account. Execute the below command.
Connect-AzureRmAccount
This will prompt for your Azure username and password. Provide the
details and login. A successful login with show all your subscriptions. I
have only one subscription and hence it shows the details related to
that.
Account : XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
SubscriptionName : XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
SubscriptionId : XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
TenantId : XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Environment : XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
- Next set some deployment variables which we will use later in the steps. Select these steps and execute.
$location = 'SouthEast Asia'
$resourceGroupName = 'ARMdemo-simple-storage'
$resourceDeploymentName = 'ARMdemo-simple-storage-deployment'
$templatePath = $env:SystemDrive + '\' + 'Personal\Projects\DemoAzureRmStorageDeployment'
$templateFile = 'azuredeploy.json'
$template = $templatePath + '\' + $templateFile
- Create a Resource group in Azure under which our Storage resource
will be placed. Now here you might be thinking that why we are not
creating this resource group through the same ARM template. You are
absolutely right but I have purposefully kept this separate to make our
ARM template look simple. Otherwise we have to make it a bit complex by
adding priority in the template regarding which resource should get
created first and depends on what. Hope you understand.
New-AzureRmResourceGroup `
-Name $resourceGroupName `
-Location $location `
-Verbose -Force
Execute the above script. It will create the resource group name
"ARMdemo-simple-storage" in "SouthEast Asia" region. These are coming
from the deployment variables we set before. Your name and region can be different based on what you have set in the variables.
- Now the most important step. Deploying our ARM template with
Powershell. Execute the following command. The command
"New-AzureRmResourceGroupDeployment" will pick the ARM template from the
location of our local drive and would deploy the resource under the
resource group created above. The parameters passed are very much self
explanatory
New-AzureRmResourceGroupDeployment -Name $resourceDeploymentName -ResourceGroupName $resourceGroupName -TemplateFile $template -Verbose -Force
- Wait for sometime for the deployment to finish. The command will
also validate the ARM template before deployment and if looks fine will
display "-Template is valid" else will throw an error message with
description of what and where it went wrong.
- DONE with our automation.
Login to Azure portal and check if everything is created as expected.
First lets check the resource group. Our "ARMdemo-simple-storage" is there as expected.
Let dive into it. Yes our storage account (provisioned thorough the
ARM template) is showing up under the resource group in location :
Southeast Asia.
One thing to notice here is the unique storage name. The prefix is
"demo" which we had set in the ARM template and the rest is appended
from the resourceGroup id. Recall the below variable we set in the
template. Amazing!
"variables": {
"demoStorageName": "[concat(parameters('demoStoragePrefix'), uniqueString(resourceGroup().id))]"
}
Click on the storage and you can view more details.
Great Stuff and Well Done! No more manual steps anymore. Going forward any redundant work you do, try to automate it. It will be a blessing.
Do share with me about your experience and what you have built upon this foundation. You can take it upto any level and integrate. I would love to hear from you.