我正在尝试从Lambda函数连接到DocumentDB。
我已经按照这个DocumentDB配置了教程,并且可以通过cloud9命令提示符访问它。
documentDB集群是两个安全组的一部分。第一个安全组称为demoDocDB
,第二个安全组称为default
,它是vpc安全组。
demoDocDB
的入站规则将请求从cloud9实例转发到运行cloud9数据库的端口27017。
defualt
安全组的入站规则指定所有通信量、所有端口范围和自身的一个源。VPC ID是默认的VPC设置。
在lambda中,编辑VPC详细信息时,我输入:
default
安全组函数在写到数据库时已经工作了两次,其余的时间都超时了,Lambda函数的超时时间是2分钟,但是在到达之前,它会抛出一个超时错误。
[ERROR] ServerSelectionTimeoutError: MY_DATABASE_URL:27017: [Errno -2] Name or service not known
下面的代码片段是要执行的,函数永远不会到达在insert语句中超时的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的版本,但是没有帮助。
发布于 2020-12-09 20:00:05
Lambda安全组出站规则:
Type Protocol Port Range Destination
All Traffic All All 0.0.0.0/0
如果愿意,还可以将其限制为端口80/443上的HTTP/HTTPS。
2.检查您的DocumentDB集群的安全组,看看它是否使用入站规则设置,如下所示:
Type Protocol Port Range Source
Custom TCP TCP 27017 Lambda Security Group
完成此操作后,您的VPC部分应该如下所示: 1. VPC --默认的VPC2.SUBNETS--选择了2个子网(都是私有的) 3. Lambda函数的安全组。不是默认的安全组。
这应该对你有好处。如果它不起作用,请告诉我,我将尽力帮助您排除故障。
https://stackoverflow.com/questions/65222660
复制相似问题