在 Nodejs 中从 Dynamodb Steam 调用 AWS Lambda

Invoking AWS Lambda from Dynamodb Steam in Nodejs

当我尝试从 AWS 控制台 运行 lambda 函数时,代码工作正常。因为我想 运行 一个特定的代码只有在添加了一个新的 dynamo DB 记录时,我想 运行 来自 dymanoDB 流插入事件的 lambda 函数。我尝试了下面的代码,似乎 lambda 没有调用。

serverless.yml

中的权限
  - Effect: "Allow"
    Action:
      - "lambda:InvokeFunction"
    Resource: 
      - "*"

dynamoDB 流的代码

 

exports.main = async (event) => {
    const records = event.Records.filter((record) => {
        if (record.eventName.toUpperCase() === "INSERT") {
            const recordType = record.dynamodb.NewImage.type.S;

            if (recordType.toUpperCase() === "TRA") {
                 // here I capture the value for the arguments. They work fine
                 inkvokeTraLamda(keyword, key, tag);
            }  
        }
    });
};

调用lambda的函数

const AWS = require("aws-sdk");
const lambda = new AWS.Lambda({ region: "us-west-1" });
exports.inkvokeTraLamda = async function invoke(
    keyword,
    key,
    tag
) {
    const playload = {
        keyword,
        key,
        tag,
    };

    const LambdaPromise = (params) => lambda.invoke(params).promise();

    const resp = await LambdaPromise(params);
    console.log(resp);
    return resp;  
};


非常感谢您的指导。

而不是告诉一个 lambda 函数调用另一个。配置您相关的 Lambda 函数以侦听 DynamoDB Streams。因此,您的侦听器 Lambda 会将其添加到其事件配置中。

  events:
  - stream:
      type: dynamodb
      batchSize: 1
      # INFO: Batch Window size in seconds allows you to wait as long as 300s to build a batch before invoking a function.
      # Now, a function is invoked when one of the following conditions is met: the payload size reaches 6MB, the Batch Window reaches its maximum value, or the Batch Size reaches its maximum value.
      # With Batch Window, you can increase the average number of records passed to the function with each invocation.
      batchWindow: 5
      # This gives the option to recursively split the failed batch and retry on a smaller subset of records, eventually isolating the metadata causing the error.
      bisectBatchOnFunctionError: true
      maximumRetryAttempts: 3
      maximumRecordAgeInSeconds: 120

      arn:
        Fn::GetAtt:
          - <TableName>
          - <TableStreamArn>

一旦插入内容,您的第二个代码片段就会响应。第三个片段似乎与数据库流无关。