Welcome to the Microsoft Q&A Platform. Thank you for reaching out & I hope you are doing well.
I understand that you would like to know how to add an VM to an ASG using ARM Templates.
Technically, you can do it in a couple of ways.
- If the VM is not yet created, and you are planning to create it with a new NIC : https://learn.microsoft.com/en-us/azure/templates/microsoft.compute/virtualmachines?pivots=deployment-language-arm-template
- In this case, under "ipConfigurations"
-
- In this case, under "ipConfigurations"
- Or you can add the NIC referenced by the VM to the ASG
-
- In both these cases, the "id" references to the ASG Component created using : https://learn.microsoft.com/en-us/azure/templates/microsoft.network/applicationsecuritygroups?pivots=deployment-language-arm-template
With Powershell,
- First, create an ASG : https://learn.microsoft.com/en-us/powershell/module/az.network/new-azapplicationsecuritygroup?view=azps-10.1.0
- Then Update the IP Configuration of your NIC to add it to this ASG : https://learn.microsoft.com/en-us/powershell/module/az.network/set-aznetworkinterfaceipconfig?view=azps-10.1.0#2-associating-an-ip-configuration-with-an-application-security-group
$vnet = Get-AzVirtualNetwork -Name myvnet -ResourceGroupName myrg
$subnet = Get-AzVirtualNetworkSubnetConfig -Name mysubnet -VirtualNetwork $vnet
$asg = Get-AzApplicationSecurityGroup -Name myasg -ResourceGroupName myrg
$nic = Get-AzNetworkInterface -Name nic1 -ResourceGroupName myrg
$nic | Set-AzNetworkInterfaceIpConfig -Name ipconfig1 -PrivateIpAddress 10.0.0.11 -Subnet $subnet -ApplicationSecurityGroup $asg -Primary
$nic | Set-AzNetworkInterface
Kindly let us know if this helps or you need further assistance on this issue.
Thanks,
Kapil
Please don’t forget to close the thread by clicking "Accept the answer" wherever the information provided helps you, as this can be beneficial to other community members.