Creating Azure Resources Programmatically

Creating Azure resources programmatically is extremely beneficial in streamlining the process. The Azure CLI and Azure Powershell are tools that allow you to deploy resources to Azure via commands. I will show how to create an Azure resource in each tool below.

Azure CLI

Click here for instructions on installing the current Azure command line interface if you do not already have it.

1. Once installed open your operating systems command line program and enter the command below to login. A new window will open requesting you for your Azure portal credentials.

az login

2. After that you will need to create a new azure resource group or use an existing group. Every resource in Azure has to belong to a group.

az group create --name "{NAME}" --location "{AZURELOCATION}"

3. Finally you can create a resource. Every resource will have required parameters that need to be passed in as well in order to create the resource.

az {RESOURCEIDENTIFIER} create --resource-group "{RESOURCEGROUP}" --name "{NAME}"

Here is an example script for creating a Virtual Machine resource:

az create --resource-group "VMResourceGroup" --name "VM1" --image "win2019datacenter" --admin-username "admin" --admin-password "Test123!"

Azure Powershell

Click here for instructions on installing Az Modules which is the powershell module used for azure commands.

1. Once Az Modules have been installed and inside a Powershell terminal use the command below to connect to Azure. A new window will open requesting you for your Azure portal credentials.

Connect-AzAccount -SubscriptionName '{NAME}'

2. After that you will need to create a new azure resource group or use an existing group. Every resource in Azure has to belong to a group.

New-AzResourceGroup -Name '{NAME}' -Location '{AZURELOCATION}'

3. One difference between Azure CLI and Azure Powershell is that you will have to create a credential for some resources.

$username = '{Username}'
$password = ConvertTo-SecureString '{Password}' -AsPlainText -Force
$windowsCred = New-Object System.Management.Automation.PSCredential ($username, $password)

4. Finally you can create a resource. Every resource will have required parameters that need to be passed in as well in order to create the resource. Here is an example script for creating a Virtual Machine resource:

New-AzVM -ResourceGroupName 'VMResourceGroup' -Name 'VM1' -Image 'Win2019Datacenter' -Credentials $windowsCred

As you can imagine creating resources with either one of these tools will help speed up and streamline the process of creating multiple resources that you end up creating over and over as opposed to going through the Azure Portal user interface every time you need to create a resource.

Drew Demechko

Drew Demechko

Dallas, TX