Skip to content

Instantly share code, notes, and snippets.

@StefanIvemo
Created November 17, 2020 20:22
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save StefanIvemo/31cda6faa214824b2049a1e98f0e279b to your computer and use it in GitHub Desktop.
Save StefanIvemo/31cda6faa214824b2049a1e98f0e279b to your computer and use it in GitHub Desktop.
NSGTemplate.json
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"location": {
"type": "string"
},
"VNetName": {
"type": "string",
"metadata": {
"description": "description"
}
},
"NSGs": {
"type": "array"
}
},
"resources": [
{
"type": "Microsoft.Network/networkSecurityGroups",
"apiVersion": "2020-05-01",
"name": "[concat(parameters('NSGs')[copyIndex()].properties.NSGName)]",
"location": "[parameters('Location')]",
"copy": {
"name": "NSGs",
"count": "[length(parameters('NSGs'))]"
},
"properties": {
"securityRules": "[parameters('NSGs')[copyIndex()].properties.SecurityRules]"
}
},
{
"type": "Microsoft.Resources/deployments",
"apiVersion": "2020-06-01",
"name": "[concat('apply',parameters('NSGs')[copyIndex()].properties.NSGName)]",
"dependsOn": [
"NSGs"
],
"copy": {
"name": "association",
"count": "[length(parameters('NSGs'))]",
"mode": "Serial"
},
"properties": {
"mode": "Incremental",
"template": {
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"resources": [
{
"apiVersion": "2020-05-01",
"type": "Microsoft.Network/virtualNetworks/subnets",
"name": "[concat(parameters('VNetName'), '/', parameters('NSGs')[copyIndex()].properties.SubnetName)]",
"location": "[resourceGroup().location]",
"properties": {
"addressPrefix": "[reference(resourceId(resourceGroup().name, 'Microsoft.Network/virtualNetworks/subnets', parameters('VNetName'), parameters('NSGs')[copyIndex()].properties.SubnetName), '2018-03-01').addressPrefix]",
"networkSecurityGroup": {
"id": "[resourceId('Microsoft.Network/networkSecurityGroups', parameters('NSGs')[copyIndex()].properties.NSGName)]"
}
}
}
]
}
}
}
]
}
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentParameters.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"location": {
"value": "westeurope"
},
"VNetName": {
"value": "super-vnet"
},
"NSGs": {
"value": [
{
"properties": {
"NSGName": "NSG1",
"SubnetName": "Subnet1",
"securityRules": [
{
"name": "Inbound_Deny_All",
"properties": {
"description": "Deny all inbound traffic",
"protocol": "*",
"sourcePortRange": "*",
"sourceAddressPrefix": "*",
"destinationPortRange": "*",
"destinationAddressPrefix": "*",
"access": "Deny",
"priority": 4096,
"direction": "Inbound"
}
}
]
}
},
{
"properties": {
"NSGName": "NSG2",
"SubnetName": "Subnet2",
"securityRules": [
{
"name": "Outbound_Deny_All",
"properties": {
"description": "Deny all inbound traffic",
"protocol": "*",
"sourcePortRange": "*",
"sourceAddressPrefix": "*",
"destinationPortRange": "*",
"destinationAddressPrefix": "*",
"access": "Deny",
"priority": 4096,
"direction": "Outbound"
}
}
]
}
},
{
"properties": {
"NSGName": "NSG3",
"SubnetName": "Subnet3",
"securityRules": [
{
"name": "Inbound_Allow_Http",
"properties": {
"description": "Allow inbound http traffic",
"protocol": "TCP",
"sourcePortRange": "*",
"sourceAddressPrefix": "*",
"destinationPortRange": "80",
"destinationAddressPrefix": "*",
"access": "Allow",
"priority": 4096,
"direction": "Inbound"
}
}
]
}
},
{
"properties": {
"NSGName": "NSG4",
"SubnetName": "Subnet4",
"SecurityRules": []
}
}
]
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment