good1492
good1492

Reputation: 251

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

I have two DynamoDB tables: Order and Client.

When an Order is created, a Lambda function is triggered and first seeks to read the Client associated with the Order. However, the Lambda function fails to do so and does not report any error.

The Lambda function is:

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); }

      }
   })
}

When I run this, it gets to console.log(params) but then doesn't show any result nor any error.

My function was configured with the amplify cli and it appears to have read permissions to the Client 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"
        },
        {

Am I missing something?

Thanks!

Upvotes: 0

Views: 424

Answers (1)

cyberwombat
cyberwombat

Reputation: 40074

The issue is that you are using await in a forEach loop which doesn't work. Your handler is existing before the Db call is done which is why there is no response. You will want to do something more like this:

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)
  }
}

Upvotes: 2

Related Questions