简体   繁体   English

无法从AWS Lambda连接到默认VPC中的AWS RDS实例

[英]unable to connect to AWS RDS instance in default VPC from AWS Lambda

I have a RDS mysql instance running 我有一个RDS mysql实例正在运行

  1. its assigned in default VPC to all default subnets 将其在默认VPC中分配给所有默认子网

  2. has a security group, inbound rule set to listen all Traffic, all protocol, all port ranges and source 0.0.0.0/0 有一个安全组,入站规则设置为侦听所有流量,所有协议,所有端口范围和源0.0.0.0/0

  3. Publicly accessible is set to True 公开访问设置为True

I am able to connect to RDS from SQl Workbench and also from local python script 我能够从SQl Workbench以及本地python脚本连接到RDS

-In my python lambda function - -在我的python lambda函数中 -

  1. have assigned role with AWSLambdaVPCAccessExecutionRole ,lambda_basic_execution 已为AWSLambdaVPCAccessExecutionRole分配角色,lambda_basic_execution

    2.Lambda is not assigned to any VPC 2.Lambda未分配给任何VPC

I get following error message from lambda "errorMessage": "RequestId: xx Process exited before completing request" 我从lambda “ errorMessage”收到以下错误消息:“ RequestId:xx进程在完成请求之前已退出”

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.") 代码在尝试连接到DB get_database_connection()时以及在块日志消息logger.error之外的点均失败(“错误:意外错误:无法连接到MySql实例。”)

Is it even possible for lambda to connect to RDS instance in default VPC ? lambda是否有可能在默认VPC中连接到RDS实例? lambda is not assigned to any VPC lambda未分配给任何VPC

Lambda Code Lambda代码

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] 按照此链接[ 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 为默认VPC创建2个专用子网

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

Go to your Lambda function in the console. 在控制台中转到Lambda函数。

Scroll down and on the left hand side select the default VPC. 向下滚动并在左侧选择默认的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" 是的,您的lambda不在vpc中,因此该实例无法联系rds公共实例,请按照此文档为您的lambda函数提供互联网“功能”

https://aws.amazon.com/it/premiumsupport/knowledge-center/internet-access-lambda-function/ 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.. 有很多文档说您的VPC中有2个lambda专用子网,并使用NAT网关等建立了Internet连接。
  • Actually I was able to connect to RDS in default VPC directly from lambda(without placing it in private subnets). 实际上,我能够直接从lambda连接到默认VPC中的RDS(无需将其放置在专用子网中)。 Issue was I had imported pymysql file inside of pacakage folder, so I was getting 问题是我已经在pacakage文件夹中导入了pymysql文件,所以我正在
    that connection Timeout error. 该连接超时错误。
  • I just had to prefix package in from of pymysql (package.mysql) 我只需要在pymysql(package.mysql)的package中添加前缀
    except Exception as error: did trick for me 除了异常作为错误:欺骗了我

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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