若在使用Aws Lambda和Firestore时遇到请求超时的问题,可以考虑以下几个方面:
检查网络连接是否正常,防火墙和DNS设置是否正确。
确认Firestore的权限是否设置正确。 在Firestore中,需要为Lambda函数提供足够的权限以执行Firestore操作。您可以通过在IAM控制台中为特定角色授予访问Firestore服务的权限来设置正确的权限。
调整Lambda的超时设定以获得更充分的时间执行Firestore请求。 您可以通过更新控制台或CLI中的函数配置设置来调整Lambda函数的执行限制。将函数超时值增加到足以允许Firestore查询或写入在其预期时间内完成即可。
以下示例演示如何使用Node.js和Firestore设置Lambda函数的权限(前提是AWS-S3已启用):
const {Firestore} = require('@google-cloud/firestore');
const firestore = new Firestore({ projectId: 'your-project-id', });
exports.handler = async (event, context) => { // Get document from Firestore const docRef = firestore.collection('myCollection').doc('myDoc'); const doc = await docRef.get(); const data = doc.data();
// Perform other operations on data
return { statusCode: 200, body: JSON.stringify(data), }; };
然后,您需要创建一种Lambda IAM角色,以便该角色具有访问Firestore权限。
{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "firestore:*" ], "Resource": [ "arn:aws:firestore:region:project-id:database/(default)" ] } ] }
最后,将上述IAM角色分配给您的Lambda函数,以便该函数可以通过AWS访问Firestore。