通过 AWS Lambda 函数连接 AWS 文档数据库时出现 ServerSelectionTimeoutError

问题描述 投票:0回答:1

我一直在尝试通过用 AWS Lambda 函数编写的 python 脚本连接到 AWS Document DB。

代码是:

def lambda_handler(event, context):
    create_mongo_connection()

def create_mongo_connection():
    try :   
        conn = pymongo.MongoClient('mongodb://<username>:<password>@test-document-db-exportify.cluster-c9puxz9rzs0a.ap-south-1.docdb.amazonaws.com:27017/?ssl=true&ssl_ca_certs=rds-combined-ca-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false')
        db = conn['exportifydb']
        docs = db['constants'].find({})
        for doc in docs:
            print(doc)
    except Exception as e:
        print(e)

但是,它提出:

    raise ServerSelectionTimeoutError(                                                                                                                                  
    pymongo.errors.ServerSelectionTimeoutError: test-document-db-exportify.cluster- 
    c9puxz9rzs0a.ap-south-1.docdb.amazonaws.com:27017: timed out, Timeout: 30s, Topology 
    Description: <TopologyDescription id: 611f576267e03bfd2fcb0b3c, topology_type: 
    ReplicaSetNoPrimary, servers: [<ServerDescription ('test-document-db-exportify.cluster- 
    c9puxz9rzs0a.ap-south-1.docdb.amazonaws.com', 27017) server_type: Unknown, rtt: None, 
    error=NetworkTimeout('test-document-db-exportify.cluster-c9puxz9rzs0a.ap-south- 
    1.docdb.amazonaws.com:27017: timed out')>]> 

但是,我可以使用 shell 和 Robo3T 连接到 SameDB 并访问查询结果

python amazon-web-services aws-lambda pymongo aws-documentdb
1个回答
0
投票

检查 DocumentDB 设置中集群的参数组是否启用了 TLS。我看到您正在传递 ssl true 并确保您的证书正在使用集群上配置的证书进行身份验证。希望这能解决问题。

© www.soinside.com 2019 - 2024. All rights reserved.