问题
Where I work, we are trying to use an Azure Function to take a JSON string message from an Event Hub and insert it into Postgres RDS in AWS. Unfortunately, we have to use Postgres RDS for the time being to persist data but this will likely change to an Azure technology in future.
I am able to get the Function bound to an Event Hub and can successfully receive messages.
run.csx
#r "System.Data"
using System;
using System.Data;
using Npgsql;
public static void Run(string myEventHubMessage, TraceWriter log)
{
log.Info($"C# Event Hub trigger function processed a message:
{myEventHubMessage}");
using (NpgsqlConnection connection = new NpgsqlConnection(
"Host=host;Port=5432;Database=database;Username=username;Password=password;Timeout=300"))
{
try
{
log.Info("Opening connection to Postgres...");
connection.Open();
log.Info("Connection open.");
}
catch (Exception ex)
{
log.Info($"Failed to open connection to Postgres. EXCEPTION:
{ex.Message}");
throw;
}
}
}
project.json
{
"frameworks": {
"net46":{
"dependencies": {
"Npgsql": "3.2.2",
}
}
}
}
I am using Npgsql to try to connect to Postgres but it can't seem to connect giving the following error in the logs:
2017-04-27T09:58:30.710 Failed to open connection to Postgres. EXCEPTION: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond
I know this database is available to connect to and I have tried upping the Timeout etc in the connection string but no luck.
Is this actually possible to do?
Many thanks.
回答1:
Most probably it's not a timeout, but a firewall sitting between Azure Web App and AWS Database, which blocks the connection.
Azure does not restrict outbound connections, at least not to port 5432.
So, I guess that's AWS who has a restriction on IP range configured. Try adding your Azure IP range to the white list there.
Azure portal doesn't seem to show the Outbound IP ranges for a Function App, but I'm able to see them in Azure Resource Explorer. The path to your resource will look like
http://resources.azure.com/subscriptions/{subscriptionid}/resourceGroups/{webappplan}
/providers/Microsoft.Web/sites/{functionapp}
search for a property like
"outboundIpAddresses": "104.46.38.91,104.46.38.110,104.46.35.12,23.97.218.73"
UPDATE: The outbound IP address is not shown in the portal for a reason. They are not guaranteed to be stable, since Function App instances may be in different scale sets. See this answer.
来源:https://stackoverflow.com/questions/43654708/azure-function-event-hub-to-aws-rds-postgres