我正在尝试从 Lambda 函数连接到 DocumentDB。
我已经按照此配置了我的 DocumentDBtutorial https://aws.amazon.com/getting-started/hands-on/getting-started-amazon-documentdb-with-aws-cloud9/并可以通过cloud9命令提示符访问它。
documentDB 集群是两个安全组的一部分。第一个安全组称为demoDocDB
第二个叫default
是vpc默认安全组。
入库规则为demoDocDB
将来自 cloud9 实例的请求转发到运行我的 documentDB 数据库的端口 27017。
入库规则为defualt
安全组指定所有流量、所有端口范围及其自身的来源。 VPC ID 是默认的 VPC 设置。
在 lambda 中编辑 VPC 详细信息时,我输入了:
- VPC - 默认 VPC
- 子网 - 选择所有 3 个可用子网
- 安全组 - 的
default
VPC 的安全组
该函数在写入数据库时工作了两次,其余时间都超时了,Lambda 函数的超时时间为 2 分钟,但在达到该超时时间之前,它将抛出超时错误。
[ERROR] ServerSelectionTimeoutError: MY_DATABASE_URL:27017: [Errno -2] Name or service not known
下面的代码片段是尝试执行的代码,该函数永远不会到达print("INSERTED DATA")
它在插入语句期间超时。
def getDBConnection():
client = pymongo.MongoClient(***MY_URL***)
##Specify the database to be used
db = client.test
print("GOT CONNECTION",db)
##Specify the collection to be used
col = db.myTestCollection
print("GOT COL",col)
##Insert a single document
col.insert_one({'hello':'Amazon DocumentDB'})
print("INSERTED DATA")
##Find the document that was previously written
x = col.find_one({'hello':'Amazon DocumentDB'})
##Print the result to the screen
print("RETRIEVED DATA",x)
##Close the connection
client.close()
我尝试过更改 pymongo 的版本,如下所示thread https://stackoverflow.com/questions/39927024/serverselectiontimeouterror-when-connecting-to-aws-with-pymongo建议但没有帮助。