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.