ARM templates

If you are using Azure to host your application then how you create resources in Azure? Azure Portal is a very good option to create resources in Azure. You can see all the options in the very user friendly interface. If you are new in Azure then Azure Portal is a very good option to start. Even if for experienced people Azure Portal is a good option for creating and managing resources on Azure visually.

But when you want to create resources repeatedly because of CI CD then manually creating resources in Azure Portal will not be a good option. In this case you need some form of automation to do this. Azure have good support for automation. The most popular methods for automation in Azure is Azure CLI and Azure PowerShell. You need to download the Azure Resource Manager (ARM) template file one time for your resources from Azure Portal and then you can use that json format ARM template file to create resources with Azure CLI or Azure PowerShell multiple times.

Today I am using Azure PowerShell to create an App Service in Azure with ARM template file. I am using Windows.

Install PowerShell Core and Az module

First I need to install PowerShell Core. I have dotnet core installed in my machine so I can use that to install PowerShell Core as a global tool for dotnet core. From command line I am executing the following command.

dotnet tool install --global PowerShell

After that I can check if it is installed or not using the following command.

dotnet tool list --global

The next thing is to install Azure Powershell Az module. The following command will check the PowerShell version.

$PSVersionTable.PSVersion

Azure PowerShell works with PowerShell 6.2.4 and later on all platforms. As you can see that I have PowerShell 7.0.3 installed, the next step is to install the Az module.

if ($PSVersionTable.PSEdition -eq 'Desktop' -and (Get-Module -Name AzureRM -ListAvailable))
{     
    Write-Warning -Message ('Az module not installed. Having both the AzureRM and Az modules installed at the same time is not supported.')
}
else
{
    Install-Module -Name Az -AllowClobber -Scope CurrentUser
} 

When installed properly, I can check the Az modules are installed properly with below command.

Get-InstalledModule

Deployment with ARM template

Now we have installed PowerShell Core and Az module, the next step is to create a resource group on Azure. I am opening PowerShell Core and connect with my Azure account.

Connect-AzAccount

This will give you a link and a code to login with browser. After successful login, I can see my subscription details.

Next I am creating a new resource group.

New-AzResourceGroup -Name "dev-test-rg" -Location "North Europe"

Now from the portal, I am going to create a new app service. But I will not complete the creation process and it is only to download the ARM template file.

Now I am going to execute the following command to create the app service.

New-AzResourceGroupDeployment -ResourceGroupName "dev-test-rg" 
  -TemplateFile "C:\template\template.json" 
  -TemplateParameterFile "C:\template\parameters.json"

After the successful deployment, I can see my newly created app service in the Azure Portal. In this way I can automate the creation process of my app service with ARM template and Azure PowerShell.

Clean up

To clean up, I can remove the resource group from Azure Portal.

Or I can execute the below command to delete the resource group from PowerShell Core.

Remove-AzResourceGroup -Name "dev-test-rg"

Finally I am logging out from the session.

Disconnect-AzAccount

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s