Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/amazon-web-services/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Postgresql 通过AWS lambda函数访问EC2实例中运行的数据库_Postgresql_Amazon Web Services_Amazon S3_Aws Lambda_Amazon Vpc - Fatal编程技术网

Postgresql 通过AWS lambda函数访问EC2实例中运行的数据库

Postgresql 通过AWS lambda函数访问EC2实例中运行的数据库,postgresql,amazon-web-services,amazon-s3,aws-lambda,amazon-vpc,Postgresql,Amazon Web Services,Amazon S3,Aws Lambda,Amazon Vpc,我在python3.6中编写了lambda函数来访问在EC2实例中运行的postgresql数据库 psycopg2.connect(user="<USER NAME>", password="<PASSWORD>", host="<EC2 IP Address>", port="<PORT NUM

我在python3.6中编写了lambda函数来访问在EC2实例中运行的postgresql数据库

       psycopg2.connect(user="<USER NAME>",
                        password="<PASSWORD>",
                        host="<EC2 IP Address>",
                        port="<PORT NUMBER>",
                        database="<DATABASE NAME>")
创建了具有所需依赖项的部署包(作为zip文件),并将其上载到AWS lambda中

并且还将虚拟私有云VPC配置为默认,还包括Ec2实例的详细信息,但我无法从数据库获取连接。尝试从lambda连接数据库时导致超时

Lambda函数:


我在谷歌上搜索了很多,但找不到任何解决方法。有什么方法可以满足这一要求吗?

要让lambda连接到专有网络内的任何资源,它需要将ENIs设置到专有网络的相关私有子网。您是否正确设置了EC2的VPC关联和安全组?
您可以参考

您的配置需要:

专有网络中的数据库 Lambda函数配置为使用与数据库相同的VPC Lambda函数Lambda SG上的安全组 数据库DB-SG上的安全组,允许从相关数据库端口上的Lambda SG进行入站连接 也就是说,DB-SG指Lambda SG

from __future__ import print_function
import json
import ast,datetime
import psycopg2


def lambda_handler(event, context):
    received_event = json.dumps(event, indent=2)
    load = ast.literal_eval(received_event)

    try:
        connection = psycopg2.connect(user="<USER NAME>",
                                        password="<PASSWORD>",
                                        host="<EC2 IP Address>",
                                        # host="localhost",
                                        port="<PORT NUMBER>",
                                        database="<DATABASE NAME>")

        cursor = connection.cursor()
        postgreSQL_select_Query = "select * from test_table limit 10"
        cursor.execute(postgreSQL_select_Query)
        print("Selecting rows from mobile table using cursor.fetchall")
        mobile_records = cursor.fetchall() 

        print("Print each row and it's columns values")
        for row in mobile_records:
            print("Id = ", row[0], )

    except (Exception,) as error :
        print ("Error while fetching data from PostgreSQL", error)
    finally:
        #closing database connection.
        if(connection):
            cursor.close()
            connection.close()
            print("PostgreSQL connection is closed")

    return {
        'statusCode': 200,
        'body': json.dumps('Hello from Lambda!'),
        'dt' : str(datetime.datetime.now())
    }