0
votes

I am trying to connect to my SQL Server RDS instance from AWS Lambda in my VPC (using .net core).

When I attempt to run the Lambda function, it returns:

"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;",

I have configured the following:

  • Both the RDS instance and Lambda are in the same VPC
  • Both the RDS instance and Lambda are in the same AZ and subnet
  • The Lambda is in its own Security Group
  • The RDS instance Security Group has given permission for port 1433 for the Lambda SG
  • The Lambda IAM role has permission for AWSLambdaFullAccess, and AWSLambdaVPCAccessExecutionRole
  • The MSSQL connection string (endpoint, user id, pwd, etc.) works from an EC2 instance in the same VPC/AZ/subnet.

The connection string looks like this (using Dapper as my ORM)

string dbconnstr = @"server=vpc100-db.xyz.us-west-1.rds.amazonaws.com;database=dbMyDb;user id=sa;password=mypwd;";

Any idea what I'm missing?

1
It looks like you've covered everything, so first thing I would double check that the actual settings are according to the above. What SG did you set to the EC2 which is connect successfully? I had similar issue and it resolved once I set the SG more loose, allowing any IP address. Let me know how it goes. - johni
Did you mean making the RDS SG more loose? I tried adding an inbound rule to the RDS security group that allowed ALL TCP and ALL IPs. It didn't change the result. Still getting the 'timeout period elapsed during the post-login phase' error. It seems that the Lambda function is finding the SQL Server because it does NOT say 'server not found', but the connection is still failing. - Pete Lunenfeld

1 Answers

1
votes

Solved. DB engine version was the issue.

It turns out it wasn't a permission problem with Lambda or the VPC and RDS. The inability to connect from Lambda (via c# .net core) to the RDS was the database version! I was connecting to a RDS MS SQL 2008 R2 which worked from local code but not from Lambda. When I changed the RDS target db to MS SQL 2016 it worked!