Hi I was having the same problem as this post but the posted answer (no sample code) wasn't provided so I will start a new thread and ask you guys.
I have created a AWS Serverless Application (.Net Core) project for our lambda function and below code is what I used to connect in AWS RDS.
string ConnectionString = "Data Source=rds4abc1190.asdfqwerqb9l.us-east-1.rds.amazonaws.com,2855;Initial Catalog=MyDatabase;Persist Security Info=True;User ID=myID;Password=mypassword;Encrypt=False";
using (var Conn = new SqlConnection(ConnectionString))
{
using (var Cmd = new SqlCommand("SELECT * from Customer", Conn))
{
Conn.Open(); //<-- this is where the error fires.
SqlDataReader rdr = Cmd.ExecuteReader();
while (rdr.Read())
{
myDbItems.Add(rdr[1].ToString());
}
Conn.Close();
}
}
Using the above code works fine when running on local machine but if deployed in AWS Api Gateway, that code would error.
"errorType": "SqlException", "errorMessage": "Connection Timeout Expired. The timeout period elapsed during the post-login phase. The connection could have timed out while waiting for server to complete the login process and respond; Or it could have timed out while attempting to create multiple active connections. The duration spent while attempting to connect to this server was - [Pre-Login] initialization=781; handshake=1957; [Login] initialization=40; authentication=122; [Post-Login] complete=12219;",