Instantly share code, notes, and snippets.

What would you like to do?
Outbound IP Registration to Azure SQL Using Azure Functions
public static async Task<HttpResponseMessage> Run(
[HttpTrigger(AuthorizationLevel.Function, "post", Route = "firewall/rules")]HttpRequestMessage req,
TraceWriter log)
log.Info($"Firewall rules on database servers in {resourceGroupName} are updating...");
var tasks = new List<Task>();
var servers = await azure.SqlServers
foreach (var server in servers)
var registeredIps = server.FirewallRules
.ToDictionary(p => p.Name, p => p.StartIPAddress);
var ipsToExclude = registeredIps.Where(p => !outboundIps.Contains(p.Value))
.Select(p => p.Key)
var IpsToInclude = outboundIps.Where(p => !registeredIps.ContainsValue(p))
var tasksToExclude = ipsToExclude.Select(ip => server.FirewallRules
var tasksToInclude = IpsToInclude.Select(ip => server.FirewallRules
.Define($"webapp-{ip.Replace(".", "-")}")
.WithIPAddressRange(ip, ip)
await Task.WhenAll(tasks).ConfigureAwait(false);
log.Info($"Firewall rules on database servers in {resourceGroupName} have been updated.");
return req.CreateResponse(HttpStatusCode.OK);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment