DEV Community

Stiven Castro
Stiven Castro

Posted on

Add Network Restrictions to Azure App Services using Powershell

Executing tasks directly in the Azure Portal sometimes needs more time than expected due to in some resources configuration parameters only can be set one by one.

This is the case when it is necessary to configure network restrictions to an App Service after it is deployed, you only can set 1 rule at a time.

Azure Powershell provides cmdlets that help to manage our resources faster and effectively.

In this case, we will see how to configure in an App Service several network restriction rules when they belong to a different network segment using an Azure Powershell function.

Requierements

To start coding first we will need some information that will be used in our PowerShell runbook:

  • The subscription name and tenant Id where our App Service is hosted
  • The resources group that contains our App Service
  • The App Service name

Login

Connect to our Azure Account:

$SubscriptionName = 'Your_Subscription_Name'
$TenantId = 'Your_Tenant_Id'

Connect-AzAccount -SubscriptionName $SubscriptionName -TenantId $TenantId
Enter fullscreen mode Exit fullscreen mode

Define Variables

Define the variables to pass to the function like the Resource Group, the App Service name, and the number of the priority we want to give to the first rule of the set we are creating.

$ResourceGroup = "Resrouce_Group_Name"
$WebappName = "App_Service_Name"

$RuleName = 'Your_Rule_Name'
$RulePriority = 100

Enter fullscreen mode Exit fullscreen mode

Create the Function

To avoid executing many times the PowerShell runbook, we will create a buckle that will create the Network Restriction Rules in our App Service

Function NetworkRestrictions ($IpAddress) 

{

[String]$IPAddress, 
[String]$ResourceGroup, 
[String]$WebappName, 
[String]$RuleName, 
[Int]$RulePriority

Add-AzWebAppAccessRestrictionRule -ResourceGroupName $ResourceGroup -WebAppName $WebappName   `
-Name $RuleName -Priority $RulePriority -Action Allow -IpAddress $IPAddress


} ('IP_1','IP_2','IP_3','IP_n') | foreach  {
NetworkRestrictions $_ 
$RulePriority++ 
} 


Enter fullscreen mode Exit fullscreen mode

Conclusion

  • We are now able to create several Network Restrictions to an App Services instead of configuring one by one

  • We will save time because set one rule at a time using the Azure Portal will be slower

  • We see how useful and powerful are the azure PowerShell modules.

Top comments (0)