简体   繁体   中英

unable to connect to AWS RDS instance in default VPC from AWS Lambda

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]

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.

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/

  • 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

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