简体   繁体   中英

AWS Lambda returning [ODBC Driver 17 for SQL Server]Login timeout expired

I'm trying to develop a AWS Lambda to act as a middle point between my application and my RDS MSSQL database, also hosted on AWS. When I run the following code from PyCharm I get the correct data from my database.

import pyodbc
server = 'RDSDB endpoint + port'
database = 'vanim8'
username =  'RDSDB username'
password = 'RDSDB password'

conn = pyodbc.connect('DRIVER={ODBC Driver 17 for SQL Server};SERVER=' + server + ';DATABASE=' + database + ';UID=' + username + ';PWD=' + password)
cursor = conn.cursor()
cursor.execute("SELECT * FROM login")
row = cursor.fetchone()
while row:
    print(row[2])
    row = cursor.fetchone()

But when I run invoke the lambda from the AWS CLI with the same code it returns [ODBC Driver 17 for SQL Server]Login timeout expired

I can't figure out what it would be, I was thinking it would have to be something to do with security perhaps denying inbound traffic from the lambda but not from my IP, but I'm not sure I'm new to cloud computing

I figured out that the security group that my RDS DB was using was allowing traffic from my IP address but not the IP of the VPC which was causing the error

Bear in mind that lambda is, by default, a public service, meaning that any name resolution and routing will be via public pathways. If your database is in a private VPC you will need to have a VPC endpoint that ties the lambda function into the private VPC, and you will also need to consider the DNS resolution for that VPC. Once the VPC endpoint is in place, you can bind the lambda function to a particular VPC, and it will route internally.

If your database is publicly accessible, then you would just need to confirm that your inbound security groups and ACL's are allowing traffic from the lambda IP ranges.

In either case you'll want to consider name resolution and network reachability as the primary culprits, with network reachability (security groups, ACL's, and routing) being the most likely based on the error you're getting.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM