DevOps Basics: Infrastructure as Code – The PowerShell Method


Hello Folks,

Last Thursday I wrote about deploying an Infrastructure using a template through the Azure management Portal. But Infrastructure as Code does not require you to deploy it using the portal; and a GUI.  You can use PowerShell to deploy your infrastructure making that much easier to automate your deployments.

Infrastructure as Code (IaC) allows you to instead of creating and managing individual resources,( like Virtual Machines, Virtual Networks, storage accounts…)  you can design complex services, such as a blog, a photo gallery, a SharePoint portal, or a wiki using a template Then, you can manage and deploy that resource group as a logical unit.

First you must have Azure PowerShell version 0.8.0 or later. To install the latest version and associate it with your Azure subscription, see How to install and configure Azure PowerShell.

Using PowerShell with Azure Resource Manager Templates

When you use Azure PowerShell, the cmdlets in the Azure module are imported by default. To switch to the Azure Resource Manager module, use the Switch-AzureMode cmdlet. It removes the Azure module from your session and imports the Azure Resource Manager and Azure Profile modules.

To switch to the AzureResoureManager module, type:

PS C:\> Switch-AzureMode -Name AzureResourceManager

clip_image002

To switch back to the Azure module, type:

PS C:\> Switch-AzureMode -Name AzureServiceManagement

To add your Azure account to the Windows PowerShell session, use the Add-AzureAccount cmdlet. Please note: The AzureResourceManager module requires Add-AzureAccount to work. A Publish Settings file will not be sufficient.

PS C:\> Add-AzureAccount

clip_image004

There are multiple templates repositories.  Azure has its own Gallery, GitHub has one to. and I’m sure there are other out there that I have not found yet.  To see all of the templates in the Azure resource group template gallery that were published by Microsoft using PowerShell use the following command. 

PS C:\> Get-AzureResourceGroupGalleryTemplate -Publisher Microsoft

clip_image006

The cmdlet returns a list of gallery templates with Microsoft as the publisher.

You can use the Identity property to identify the template in the commands and save it locally by using the Save-AzureResourceGroupGalleryTemplate command.

For exemple, if I want to get more details on the Microsoft.WindowsServer2012R2Datacenter.0.2.83-preview template. I can use the following command:

PS C:\> Get-AzureResourceGroupGalleryTemplate -Identity Microsoft.WindowsServer2012R2Datacenter.0.2.83-preview

image

If I want to save it locally, to modify it or just study its composition:

PS C:\> Save-AzureResourceGroupGalleryTemplate -Identity Microsoft.WindowsServer2012R2Datacenter.0.2.83-preview -Path C:\Azure\Templates\WindowsServer2012R2.json

image

Deploying the Azure Resource Manager Templates Using PowerShell

As I mentioned above you can use templates from the Gallery, or other sources. Since we want to use the same one as last week we will go back to the Azure Quickstart Templates and select the template we previously selected. On the third Page ,click on the first template. (Deploy a simple Windows VM in West US)

Once on that page, scroll down to see the Use the template, PowerShell section. There you will see the location on the template. https://raw.githubusercontent.com/azure/azure-quickstart-templates/master/101-simple-windows-vm/azuredeploy.json and the command to deploy it.

But first we need to create the Resource Group

PS C:\> New-AzureResourceGroup -Location "West US" -Name iactestpr1 -DeploymentName iactestpr1 -Force

Now that the resource group is created, we can deploy our template in that Resource Group.

PS C:\> New-AzureResourceGroupDeployment -Name iactestpr1 -ResourceGroupName iactestpr1 -TemplateUri https://raw.githubusercontent.com/azure/azure-quickstart-templates/master/101-simple-windows-vm/azuredeploy.json

clip_image008

And there you go.

I’ve logged in the portal and the Resource group had been deployed with the infrastructure defined in the template.

SNAGHTML60564a

Next week we’ll look at one more way you can deploy a templates.  Using Visual Studio.  that way you can provide templates to your development teams and they can deploy the invironment they need when deploying solutions for testing, QA, UAT, pre-prod or production.  And the nice thing is they will all be EXACTLY the same.

Until then.

Cheers!

Signature

Pierre Roman
@pierreroman

Comments (0)

Skip to main content