AWS Lambda - Unable to connect to SQL Server RDS i

2019-06-14 12:53发布

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条回答
虎瘦雄心在
2楼-- · 2019-06-14 13:36

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!

查看更多
登录 后发表回答