You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

AWS Lambda处理DynamoDB Stream事件报错,SNS通知未发送问题排查

问题排查与修复方案

问题根因

  • 变量名拼写错误:Lambda函数中,你将更新后的订单总额赋值给了latestAccountBalance变量,但后续打印、阈值判断、消息构造环节用的是未定义的latestAccountOrder,代码运行到该处会直接抛出变量未定义的报错,中断执行,无法走到SNS发送逻辑。
  • OrderTotal表未创建:你Lambda代码中操作的OrderTotal表,在提供的CloudFormation模板中没有对应的资源定义,调用DynamoDB的update_item操作会直接报表不存在的错误。
  • DynamoDB Stream未开启:你的TotalNotifier Lambda需要监听Orders表的新增数据变更来触发执行,你当前Orders表的Stream处于关闭状态,Lambda根本不会被触发运行。
  • SNS主题未创建:你代码中匹配的HighOrderPurchaseAlertSNSTopic主题没有在CloudFormation模板中定义,Lambda初始化阶段获取SNS主题ARN时会因为找不到对应资源直接报索引越界错误,函数完全无法运行。

修复步骤

  1. 修正Lambda变量名错误,将代码中所有的latestAccountOrder替换为latestAccountBalance
  2. 在CloudFormation模板的Resources块中添加OrderTotal表的定义,示例如下:
"OrderTotalDynamoDBTable": {
  "Type": "AWS::DynamoDB::Table",
  "Properties": {
    "TableName": "OrderTotal",
    "AttributeDefinitions": [
      {
        "AttributeName": "CustomerId",
        "AttributeType": "S"
      }
    ],
    "KeySchema": [
      {
        "AttributeName": "CustomerId",
        "KeyType": "HASH"
      }
    ],
    "ProvisionedThroughput": {
      "ReadCapacityUnits": "5",
      "WriteCapacityUnits": "5"
    }
  }
}
  1. 在OrdersDynamoDBTable的Properties块中添加Stream配置,开启流并指定返回新镜像:
"StreamSpecification": {
  "StreamViewType": "NEW_IMAGE"
}
  1. 在CloudFormation模板中添加SNS主题和订阅配置,绑定你的邮箱:
"HighOrderPurchaseAlertSNSTopic": {
  "Type": "AWS::SNS::Topic",
  "Properties": {
    "TopicName": "HighOrderPurchaseAlertSNSTopic",
    "Subscription": [
      {
        "Endpoint": "你的邮箱地址",
        "Protocol": "email"
      }
    ]
  }
}
  1. 新增Lambda事件源映射,将Orders表的Stream绑定到TotalNotifier Lambda:
"TotalNotifierEventSourceMapping": {
  "Type": "AWS::Lambda::EventSourceMapping",
  "Properties": {
    "EventSourceArn": {"Fn::GetAtt": ["OrdersDynamoDBTable", "StreamArn"],
    "FunctionName": "你的TotalNotifier Lambda函数名",
    "StartingPosition": "LATEST"
  }
}
  1. 重新部署CloudFormation堆栈,部署完成后确认邮箱收到的SNS订阅确认邮件,点击确认后再触发测试即可。

内容的提问来源于stack exchange,提问作者AaronW2

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.25 22:06:02