由 CloudWatch Log Event 触发的 AWS Lambda 未正确接收事件对象

问题描述 投票:0回答:1

我是 AWS 新手。我正在用 C# 编写一个 lambda 函数来监视 SNS 消息的失败传递。我遇到了一个问题,传递给 FunctionHandler 的第一个参数为空(不是 null,而是其所有字段,如 DetailType、Region、Source、Id 等均为 null)。

以下是我的代码:

namespace SnsLogProcessor 
{

    public class EventConfig
    {
        public string TopicArn { get; set; }
        public string status { get; set; }
    }

    public void FunctionHandler(CloudWatchEvent<EventConfig> evnt, ILambdaContext context)
    {
        if (evnt != null)
        {
            if (context != null)
            {
                context.Logger.LogInformation($"Lambda triggered!");
                context.Logger.LogInformation(evnt.DetailType);
                context.Logger.LogInformation(evnt.Region);
                context.Logger.LogInformation(evnt.Source);
                context.Logger.LogInformation(evnt.Id);
                context.Logger.LogInformation(evnt.Version);
                context.Logger.LogInformation(evnt.Account);

                if (evnt.Detail != null)
                {
                    string status = evnt.Detail.status;

                    if (!string.IsNullOrEmpty(status))
                        context.Logger.LogInformation(status);
                    else context.Logger.LogInformation($"Not found.");
                }
                else context.Logger.LogInformation(evnt.ToString());
            }
        }
    }
}

以下是触发后函数的输出:

2024-01-24T13:18:01.725-06:00 START RequestId: f24f25e6-10b3-4b63-be75-ae3174bdab70 Version: $LATEST
2024-01-24T13:18:02.106-06:00   2024-01-24T19:18:02.083Z f24f25e6-10b3-4b63-be75-ae3174bdab70 info Lambda triggered!
2024-01-24T13:18:02.107-06:00   2024-01-24T19:18:02.107Z f24f25e6-10b3-4b63-be75-ae3174bdab70 info
2024-01-24T13:18:02.107-06:00   2024-01-24T19:18:02.107Z f24f25e6-10b3-4b63-be75-ae3174bdab70 info
2024-01-24T13:18:02.107-06:00   2024-01-24T19:18:02.107Z f24f25e6-10b3-4b63-be75-ae3174bdab70 info
2024-01-24T13:18:02.107-06:00   2024-01-24T19:18:02.107Z f24f25e6-10b3-4b63-be75-ae3174bdab70 info
2024-01-24T13:18:02.107-06:00   2024-01-24T19:18:02.107Z f24f25e6-10b3-4b63-be75-ae3174bdab70 info
2024-01-24T13:18:02.107-06:00   2024-01-24T19:18:02.107Z f24f25e6-10b3-4b63-be75-ae3174bdab70 info
2024-01-24T13:18:02.107-06:00   2024-01-24T19:18:02.107Z f24f25e6-10b3-4b63-be75-ae3174bdab70 info Amazon.Lambda.CloudWatchEvents.CloudWatchEvent`1[SnsLogProcessor.EventConfig]
2024-01-24T13:18:02.164-06:00   END RequestId: f24f25e6-10b3-4b63-be75-ae3174bdab70 

我在实时 AWS 环境中尝试了代码并得到了上述结果。我验证了我的帐户,并且我创建的函数具有日志的所有权限。可以看到,事件对象的所有字段,包括DetailType、Region等都是null。 非常感谢您阅读本文!任何帮助将不胜感激!

c# amazon-web-services aws-lambda amazon-sns cloudevents
1个回答
0
投票

您的 lambda 没有监听 CloudWatch Events,而是监听 CloudWatch 日志流。它们是完全不同的东西。前者如今被称为 EventBridge Events。

CloudWatch 日志流事件具有以下形式:

{
    "awslogs": {
        "data": "base64-encoded gzipped JSON with log message"
    }
}

您应该使用类型

CloudWatchLogsEvent
来反序列化它:

public async Task FunctionHandler(CloudWatchLogsEvent evnt, ILambdaContext context)
{
    await Console.Out.WriteLineAsync(evnt.Awslogs.DecodeData());
}
© www.soinside.com 2019 - 2024. All rights reserved.