Loading...

Azure Quick Links

Azure Cloud Projects

Bicep template deployments

Create a Windows Virtual Machine, NIC, Subnet, Storage Account, Public IP, NSG, DNS Zone and VNet using a Bicep file.

Task Details

1. Create a Windows Virtual Machine, NIC, Subnet, Storage Account, Public IP, NSG, and VNet using a Bicep file.

2. Creating an Azure DNS Zone and Record with Bicep

*

Steps

Create a Windows Virtual Machine, NIC, Subnet, Storage Account, Public IP, NSG, and VNet using a Bicep file.

*

Inspect the Bicep file

Copy code

--SNIP--

*

Understanding the Bicep Script:

Azure Resources Deployed by the Template:

  • Subnet – Microsoft.Network/virtualNetworks/subnets
  • Storage Account – Microsoft.Storage/storageAccounts
  • Public IP – Microsoft.Network/publicIPAddresses
  • Network Security Group (NSG) – Microsoft.Network/networkSecurityGroups
  • Virtual Network (VNet) – Microsoft.Network/virtualNetworks
  • Network Interface (NIC) – Microsoft.Network/networkInterfaces
  • Virtual Machine (VM) – Microsoft.Compute/virtualMachines

Let’s break down the main parts of the Bicep file:

1. Parameters

  • adminUsername – Stores the VM’s username.
  • adminPassword – Stores the VM’s password (@secure() ensures it is encrypted and @minLength(12) enforces a minimum length).
  • dnsLabelPrefix – Stores the unique DNS name for the VM’s Public IP. Default value is generated using toLower('${vmName}-${uniqueString(resourceGroup().id, vmName)}').
  • publicIpName – Name of the Public IP.
  • publicIpAllocation – Allowed values: static or dynamic (default: dynamic).
  • publicIpSKU – Allowed SKU values for the Public IP (default: Basic).
  • osVersion – Allowed OS versions (default: 2022-datacenter-azure-edition).
  • Other parameters define VM size, location, VM name, and security type.

2. Variables

  • storageAccountName – Unique storage account name using 'bootdiags' + uniqueString(resourceGroup().id).
  • Other variables define network resources: NIC name, VNet, subnet names, prefixes, NSG group, etc.
  • securityProfileJson – JSON object containing security settings:
  • uefiSettings – Secure Boot and vTPM enabled.
  • securityType – Links to the securityType parameter.
  • Additional variables store VM extension info like extensionName, extensionPublisher, extensionVersion.

3. Resources

  • Storage Account – Declares the storage account with SKU and location.
  • Public IP – Creates a Public IP resource with SKU, location, and properties.
  • Network Security Group (NSG) – Creates NSG to control network traffic.
  • Virtual Network (VNet) – Defines a VNet for the VM.
  • Network Interface (NIC) – Defines NIC and depends on VNet deployment.

Virtual Machine (VM) – Declares VM with:

  • Hardware profile
  • OS profile
  • OS disk and data disk
  • Network profile
  • Diagnostic profile
  • VM Extension – Defines an extension for VM configuration.

4. Outputs

  • hostname – Returns the fully qualified domain name (FQDN) of the Public IP assigned to the VM.

*

Deploy Bicep template

1. In the Azure Portal, open Cloud Shell by clicking the Cloud Shell icon.

  • When prompted, choose Bash.
  • In the setup box, enter the following:
  • No storage account required
  • Subscription: Select your resource group
  • Click Apply.

*

2. In the Cloud Shell toolbar, click the Manage files icon, choose Upload from the dropdown, and upload the main.bicep file to the Cloud Shell home directory.

*

3. Deploy the resources using the following command. The deployment may take a few minutes to complete successfully.

Copy command: az deployment group create --resource-group <resource-group-name> --template-file main.bicep --parameters adminUsername=<admin-username>

Note: If you have "The content for this response was already consumed" error Include all required parameters in a parameters JSON file (@main.parameters.json), including secure strings like adminPassword then deploy it.

Copy Main.parameters.json

*

4. This command deploys resources in the specified resource group using the main.bicep template, applying all parameters from main.parameters.json, and shows detailed output with --verbose.

Copy Json code

az deployment group create \

  --resource-group <your-resource-group> \

  --template-file main.bicep \

  --parameters @main.parameters.json \

  --verbose

--SNIP--

*

Verify your deployments

Note: You can choose your own resource names in Bicep templates

  • Bicep allows you to customize names for all resources using parameters or variables.
  • Examples from this template:
Parameter / VariableDescriptionDefault / Example
adminUsernameVM administrator username"admin01"
adminPasswordVM administrator passwordSecure string, min 12 characters
dnsLabelPrefixUnique DNS name for Public IPGenerated from VM name and resource group
publicIpNameName for Public IP"myPublicIP"
publicIPAllocationMethodStatic or Dynamic IP allocation"Dynamic"
publicIpSkuSKU for Public IP"Basic"
OSVersionWindows OS version"2022-datacenter-azure-edition"
vmSizeVM size"Standard_B2s"
vmNameVM name"simple-vm"
virtualNetworkNameVirtual network name"MyVNET"
subnetNameSubnet name"Subnet"
networkSecurityGroupNameNSG name"default-NSG"
storageAccountNameStorage account name"bootdiags${uniqueString(resourceGroup().id)}"
nicNameNetwork interface name"myVMNic"

Tip: You can replace the default values with your own names for easier identification in Azure.

Using parameters allows flexibility when deploying multiple VMs or repeating deployments in different resource groups.

For example you can replace storageAccountName: bootdiags${uniqueString(resourceGroup().id)} with "storage4567"

Note: bootdiags${uniqueString(resourceGroup().id)} generates a globally unique, deterministic name by appending a hash of the resource group ID to the prefix bootdiags.

Creating an Azure DNS Zone and Record with Bicep

Inspect the Bicep file

Copy code

*

Understanding the Bicep Script:

  • This Bicep file automates the creation of a DNS zone and A record in Azure.

Parameters:

  • ZoneName: Name of the DNS zone (e.g., hostname.org).
  • RecordName: Name of the DNS record within the zone (default: www).

Resources:

  • DNS Zone (Microsoft.Network/DnsZones): Creates the DNS zone with a global location.
  • DNS A Record (Microsoft.Network/DnsZones/A): Adds an A record to the zone with two IPv4 addresses (1.2.3.4 and 1.2.3.5) and TTL of 3600 seconds.

Output:

  • NameServers: Returns the name servers of the created DNS zone for reference or further configuration.

In short: The script sets up a DNS zone and an A record in Azure, capturing the associated name servers as output.

*

Deploy Bicep template.

1. In the Azure Portal, open Cloud Shell by clicking the Cloud Shell icon.

  • When prompted, choose Bash.

In the setup box, enter the following:

  • No storage account required
  • Subscription: Select your resource group
  • Click Apply.

*

2. In the Cloud Shell toolbar, click the Manage files icon, choose Upload from the dropdown, and upload the main.bicep file to the Cloud Shell home directory.

*

3. Deploy the resources using the following command. The deployment may take a few minutes to complete successfully.

Copy Command: az deployment group create --name <deployment-name> --resource-group <resource-group-name> --template-file <path-to-template-file> --parameters zoneName=<zoneName-value> recordName=<recordName-value>

--SNIP--

*

Verify your deployments

*

Open the command prompt, run the command below, and you should see a similar output:

Command: nslookup www.<dns-zone-name> <name-server-name>

*

Conclusion:
The nslookup command successfully queried the Azure DNS server https://www.google.com/search?q=ns1-04.azure-dns.com for the domain www.TestDNS.com. The server responded with two IP addresses, 1.2.3.4 and 1.2.3.5, indicating that the domain has two A records configured for it.

*

Written by Kirill.A - Azure & Cybersecurity Consultant at AntusNet

➤ Want more? Browse all our Azure implementation guides.

Need help implementing secure Azure solutions?

Contact us for a free consultation.

    error: Content is protected !!