I have a RDS mysql instance running
its assigned in default VPC to all default subnets
has a security group, inbound rule set to listen all Traffic, all protocol, all port ranges and source 0.0.0.0/0
Publicly accessible is set to True
I am able to connect to RDS from SQl Workbench and also from local python script
-In my python lambda function -
have assigned role with AWSLambdaVPCAccessExecutionRole ,lambda_basic_execution
2.Lambda is not assigned to any VPC
I get following error message from lambda "errorMessage": "RequestId: xx Process exited before completing request"
Code fails at a point where it tries to connect to DB get_database_connection() and in except block logging message logger.error("ERROR: Unexpected error: Could not connect to MySql instance.")
Is it even possible for lambda to connect to RDS instance in default VPC ? lambda is not assigned to any VPC
Lambda Code
import sys
import logging
import package.pymysql
import logging
import package.pymysql.cursors
DATABASE_HOST = 'XXX'
DATABASE_USER = 'XXX'
DATABASE_PASSWORD = 'XXX'
DATABASE_DB_NAME = 'XXX'
port = 3306
def get_database_connection():
"Build a database connection"
conn = pymysql.connect(DATABASE_HOST, user=DATABASE_USER,
passwd=DATABASE_PASSWORD, db=DATABASE_DB_NAME, connect_timeout=5)
return conn
try:
conn = get_database_connection()
except:
logger.error("ERROR: Unexpected error: Could not connect to MySql instance.")
sys.exit()
logger.info("SUCCESS: Connection to RDS mysql instance succeeded")
def lambda_handler(event, context):
print("Lambda executed")
followed this link [https://docs.aws.amazon.com/lambda/latest/dg/vpc-rds-deployment-pkg.html][1]
except:
toexcept Exception as error:
and add another logger call right below itlogger.exception(error)
? Then run the function again and you should be able to see the entire error stack trace in cloudwatch. Please post this trace here as well. As a 'side note' suggestion, never use this way of handling exceptions. Prefer expliciting which errors you're expecting, such asexcept KeyError as error:
or at least always log the error stack trace withlogger.exception()
. – Renato Byrro