0

I have a RDS mysql instance running

  1. its assigned in default VPC to all default subnets

  2. has a security group, inbound rule set to listen all Traffic, all protocol, all port ranges and source 0.0.0.0/0

  3. 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 -

  1. 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]

  • Possible duplicate of [AWS Lambda "Process exited before completing request"](https://stackoverflow.com/questions/31627950/aws-lambda-process-exited-before-completing-request) – ruakh Jun 24 '18 at 17:53
  • have a look at the code – user3036212 Jun 24 '18 at 18:18
  • If you increase the timeout duration for your Lamba function, do you get a different result? (Maximum = 5 minutes, set it to at least 1 minute to allow for connection timeouts) – John Rotenstein Jun 24 '18 at 23:50
  • Your code is incorrectly written. Lambda will call `lambda_handler()`, which should in turn call `get_database_connection()`. – John Rotenstein Jun 24 '18 at 23:52
  • @John Rotenstein check out this [link](https://docs.aws.amazon.com/lambda/latest/dg/vpc-rds-deployment-pkg.html) connecting to DB is written outside of handler – user3036212 Jun 25 '18 at 02:17
  • I'd suggest to place more logging messages across your code to narrow down where exactly the process is exiting. Are you able to do this and post here at which line the code fails? – Renato Byrro Jun 25 '18 at 04:34
  • @ruakh It's not the same issue, though the error message is the same. Lambda could give this same "process exited" for a vast selection of reasons and it really depends on each implementation. The question you pointed uses JavaScript and is trying to connect to DynamoDB. The current question is in Python and connecting to RDS. – Renato Byrro Jun 25 '18 at 04:39
  • @RenatoByrro: The answers to that question explain that "process exited' is a generic message, and explain how to investigate it. I don't consider it relevant that the root-cause for this OP is likely to be different than for that one; the point is that this question doesn't contain enough information, because the OP needs to start by finding out what actually went wrong. (You're trying to use comments to guide the OP to fix that, which is fine, but I think the question should be put on hold in the meantime.) – ruakh Jun 25 '18 at 04:47
  • @RenatoByrro 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.")** – user3036212 Jun 25 '18 at 05:27
  • 1
    Could you modify this line `except:` to `except Exception as error:` and add another logger call right below it `logger.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 as `except KeyError as error:` or at least always log the error stack trace with `logger.exception()`. – Renato Byrro Jun 25 '18 at 11:54
  • @RenatoByrro thanks a ton man!! nice tip your logging statement worked **except Exception as error**: problem was I had imported pymysql inside of package folder. so I just had to prefix package in fromt of pymysql (package.mysql) – user3036212 Jun 25 '18 at 16:33
  • 1
    @user3036212 awesome, glad it helped! Good luck with your project. When you have some time, take a moment and try to help others here as well in stuff your already have some knowledge, doesn't need to be an expert ;) – Renato Byrro Jun 25 '18 at 20:38

3 Answers3

0

What you need to do is this:

Create 2 private subnets for the default VPC

xxx.xxx.64.0/20
xxx.xxx.128.0/20

Go to your Lambda function in the console.

Scroll down and on the left hand side select the default VPC.

Select the 2 Private Subnets as your subnets on your lambda function.
Chad Elias
  • 637
  • 6
  • 7
0

yes, your lambda is not in a vpc so the instance cant contact the rds public instance, follow this documentation for provide to your lambda function the internet "functionality"

https://aws.amazon.com/it/premiumsupport/knowledge-center/internet-access-lambda-function/

Gianmarco Carrieri
  • 771
  • 2
  • 9
  • 20
0
  • There are lots of documentation that says to have 2 private subnets for lambda in your VPC and have internet connection using NAT gateway etc..
  • Actually I was able to connect to RDS in default VPC directly from lambda(without placing it in private subnets). Issue was I had imported pymysql file inside of pacakage folder, so I was getting
    that connection Timeout error.
  • I just had to prefix package in from of pymysql (package.mysql)
    except Exception as error: did trick for me