简体   繁体   English

AWS Lambda 函数连接到 RDS 错误

[英]AWS Lambda function to connect to RDS error

I am unable to connect to RDS using an Lambda Function via the test example they provide我无法通过他们提供的测试示例使用 Lambda 函数连接到 RDS

This is the code:这是代码:

import sys
import logging
import rds_config
import pymysql
#rds settings
rds_host  = "connection_link"
name = rds_config.db_username
password = rds_config.db_password
db_name = rds_config.db_name


logger = logging.getLogger()
logger.setLevel(logging.INFO)

try:
    conn = pymysql.connect(rds_host, user=name, passwd=password, db=db_name, connect_timeout=5)
except:
    logger.error("ERROR: Unexpected error: Could not connect to MySql instance.")
    sys.exit()

logger.info("SUCCESS: Connection to RDS mysql instance succeeded")

def handler(event, context):
    """
    This function fetches content from mysql RDS instance
    """

    item_count = 0

    with conn.cursor() as cur:
        cur.execute("create table Employee3 ( EmpID  int NOT NULL, Name varchar(255) NOT NULL, PRIMARY KEY (EmpID))")  
        cur.execute('insert into Employee3 (EmpID, Name) values(1, "Joe")')
        cur.execute('insert into Employee3 (EmpID, Name) values(2, "Bob")')
        cur.execute('insert into Employee3 (EmpID, Name) values(3, "Mary")')
        conn.commit()
        cur.execute("select * from Employee3")

        for row in cur:
            item_count += 1
            logger.info(row)
            #print(row)

    return "Added %d items from RDS MySQL table" %(item_count)

This is the structure of my deployment package这是我的部署包的结构

app/pymysql/...
app/app.py
app/rds_config.py
app/PyMySQL-0.7.11.dist-info/...

I have packaged all the files inside the app folder in a zip file.我已将 app 文件夹中的所有文件打包在一个 zip 文件中。

This is the error is get这是错误是得到

"errorMessage": "RequestId: 96fb4cd2-79c1-11e7-a2dc-f97407196dbb Process exited before completing request" "errorMessage": "RequestId: 96fb4cd2-79c1-11e7-a2dc-f97407196dbb 进程在完成请求前退出"

I have already checkedmy RDS connection on MYSQL Workbench its working fine我已经在 MYSQL Workbench 上检查了我的 RDS 连接,它工作正常

Update:更新:

Let's assume that your actual Python code is actually indented correctly unlike the code you posted above.让我们假设您的实际 Python 代码实际上是正确缩进的,这与您上面发布的代码不同。

For some reason, your function cannot connect to your database.由于某种原因,您的函数无法连接到您的数据库。 And instead of returning an error to the user, you basically told it to sys.exit(1) so that's the reason why Lambda says "Process exited before completing the request".而不是向用户返回错误,您基本上是告诉它sys.exit(1)所以这就是 Lambda 说“进程在完成请求之前退出”的原因。


-- Original Answer -- ——原答案——

That does not look like an AWS lambda handler.这看起来不像 AWS lambda 处理程序。

You're supposed to write a function handler that accepts the lambda event and context as arguments.您应该编写一个接受 lambda 事件和上下文作为参数的函数处理程序。

Please read more about it from the AWS Lambda documentation .请从AWS Lambda 文档阅读更多相关信息。

As @MarkB mentioned in the comments, For connectivity you need to set VPC , Subnets and Security Group in your Lambda Function same as your RDS instance:正如@MarkB 在评论中提到的,对于连接,您需要在与 RDS 实例相同的 Lambda 函数中设置VPCSubnetsSecurity Group Lambda 函数中的 VPC、子网和安全组

and also you need to check Protocol , Port and Source for Inbound and Outbound in security group to make sure it is open for your IP and port range.并且您还需要在安全组中检查入站和出站的ProtocolPortSource ,以确保它对您的 IP 和端口范围开放。 为您的安全组开放端口和 IP 范围

I just ran into the same problem, and it turns out it is because I did not specify a name for the database on creation.我刚刚遇到了同样的问题,事实证明这是因为我没有在创建时为数据库指定名称。 Easy Create doesn't give you this option, so you will have to go with Standard Create , where one can specify the name under Additional Configuration . Easy Create没有给你这个选项,所以你必须使用Standard Create ,你可以在Additional Configuration下指定名称。 This name is what you should specify for db_name in rds_config.py .此名称是您应该在rds_config.pydb_name指定的rds_config.py

在此处输入图片说明

Alternatively, you can connect with your tool of choice without a database name, perform a CREATE DATABASE xxx;或者,您可以在没有数据库名称的情况下使用您选择的工具进行连接,执行CREATE DATABASE xxx; where xxx is the name of the database, and then use can use that database going forward.其中xxx是数据库的名称,然后使用可以继续使用该数据库。

Source: https://serverfault.com/a/996423来源: https : //serverfault.com/a/996423

This one is also relevant: Why don't I have access to the database from aws lambda but have from a local computer with the same login data?这也是相关的: 为什么我不能从 aws lambda 访问数据库,但可以从具有相同登录数据的本地计算机访问?

The problem with your zip file.您的 zip 文件的问题。

Im also using the same lambda function.我也使用相同的 lambda 函数。

Please follow the below steps.请按照以下步骤操作。

1. your app.py and rds_config.py files are good.

2. Then download the pymysql https://pypi.python.org/pypi/PyMySQL

3. Extract it.

4. Copy the pymysql to somewhere. (Note: Dont add all contents inside the PyMySQL-0.7.11 folder, we just need pymysql only.)

5. Then create a zip file with app.py, rds_config.py and pymysql folder.

在此处输入图片说明

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

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