当从另一个 DynamoDB table 作为触发器调用时,Lambda 无法读取 DynamoDB Table

Lambda unable to read DynamoDB Table when called from another DynamoDB table as a trigger

我有两个 DynamoDB tables:Order 和 Client。

创建订单时,会触发 Lambda 函数并首先尝试读取与订单关联的客户端。但是,Lambda 函数没有这样做,也没有报告任何错误。

Lambda 函数是:

var AWS = require('aws-sdk');
AWS.config.update({region: process.env.REGION})
docClient = new AWS.DynamoDB.DocumentClient();

exports.handler = async (event) => {
   await event.Records.forEach( async (record) => {
      const { eventName, dynamodb } = record
      if(eventName==="INSERT") {
         console.log('A new order has been created')
         const client_id = dynamodb.NewImage.client_id.S

         let params = {
          TableName: process.env.API_REALAPI_CLIENTTABLE_NAME,
          Key: {
            "id": client_id
          }
         }
         console.log(params)
         
         try {
            const result = await docClient.get(params).promise()
            console.log(result)
         } catch(err) { console.log(err); throw new Error(err); }

      }
   })
}

当我 运行 这个时,它到达 console.log(params) 但没有显示任何结果也没有任何错误。

我的函数配置了 amplify cli,它似乎对客户端具有读取权限 table:

    "Statement": [
        {
            "Action": [
                "dynamodb:Put*",
                "dynamodb:Create*",
                "dynamodb:BatchWriteItem",
                "dynamodb:Get*",
                "dynamodb:BatchGetItem",
                "dynamodb:List*",
                "dynamodb:Describe*",
                "dynamodb:Scan",
                "dynamodb:Query",
                "dynamodb:Update*",
                "dynamodb:RestoreTable*",
                "dynamodb:Delete*"
            ],
            "Resource": [
                "arn:aws:dynamodb:us-east-1:123456123456:table/Client-abcdefg-dev",
                "arn:aws:dynamodb:us-east-1:123456123456:table/Client-abcdefg-dev/index/*"
            ],
            "Effect": "Allow"
        },
        {

我是不是漏掉了什么?

谢谢!

问题是您在 forEach 循环中使用 await,但该循环不起作用。您的处理程序在 Db 调用完成之前就已经存在,这就是没有响应的原因。你会想做更多像这样的事情:

exports.handler = async (event) => {
  const jobs = event.Records.map((record) => {
    const { eventName, dynamodb } = record
    if (eventName === 'INSERT') {
      console.log('A new order has been created')
      const client_id = dynamodb.NewImage.client_id.S

      let params = {
        TableName: process.env.API_REALAPI_CLIENTTABLE_NAME,
        Key: {
          id: client_id
        }
      }
      console.log(params)

      return docClient.get(params).promise()
    }
  })

  try {
    const results = await Promise.all(jobs)
  } catch (err) {
    console.log(err)
    throw new Error(err)
  }
}