{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "location": {
            "type": "string"
        },
        "storagePrefix": {
            "type": "string"
        },
        "nsgName": {
            "type": "string"
        },
        "rgName": {
            "type": "string"
        }
    },
    "variables": {
        "storageDeployName": "[concat('policyStorage_', uniqueString(parameters('location'), parameters('nsgName')))]"
    },
    "resources": [
        {
            "type": "Microsoft.Network/networkSecurityGroups/providers/diagnosticSettings",
            "name": "[concat(parameters('nsgName'),'/Microsoft.Insights/service2')]",
            "apiVersion": "2017-05-01-preview",
            "location": "[parameters('location')]",
            "dependsOn": [
                "[variables('storageDeployName')]"
            ],
            "properties": {
                "storageAccountId": "[reference(variables('storageDeployName')).outputs.storageAccountId.value]",
                "logs": [
                    {
                        "category": "NetworkSecurityGroupEvent",
                        "enabled": true,
                        "retentionPolicy": {
                            "enabled": false,
                            "days": 0
                        }
                    },
                    {
                        "category": "NetworkSecurityGroupRuleCounter",
                        "enabled": true,
                        "retentionPolicy": {
                            "enabled": false,
                            "days": 0
                        }
                    }
                ]
            }
        },
        {
            "apiVersion": "2017-05-10",
            "name": "[variables('storageDeployName')]",
            "type": "Microsoft.Resources/deployments",
            "resourceGroup": "[parameters('rgName')]",
            "properties": {
                "mode": "incremental",
                "parameters": {
                    "location": {
                        "value": "[parameters('location')]"
                    },
                    "storagePrefix": {
                        "value": "[parameters('storagePrefix')]"
                    }
                },
                "template": {
                    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json",
                    "contentVersion": "1.0.0.0",
                    "parameters": {
                        "location": {
                            "type": "string"
                        },
                        "storagePrefix": {
                            "type": "string"
                        }
                    },
                    "resources": [
                        {
                            "apiVersion": "2017-06-01",
                            "type": "Microsoft.Storage/storageAccounts",
                            "name": "[concat(parameters('storageprefix'), parameters('location'))]",
                            "sku": {
                                "name": "Standard_LRS",
                                "tier": "Standard"
                            },
                            "kind": "Storage",
                            "location": "[parameters('location')]",
                            "tags": {
                                "created-by": "policy"
                            },
                            "scale": null,
                            "properties": {
                                "networkAcls": {
                                    "bypass": "AzureServices",
                                    "defaultAction": "Allow",
                                    "ipRules": [],
                                    "virtualNetworkRules": []
                                },
                                "supportsHttpsTrafficOnly": false
                            }
                        }
                    ],
                    "outputs": {
                        "storageAccountId": {
                            "type": "string",
                            "value": "[resourceId(parameters('rgName'), 'Microsoft.Storage/storageAccounts', concat(parameters('storagePrefix'), parameters('location')))]"
                        }
                    }
                }
            }
        }
    ]
}