AWS SQS与其关联的Lambda之间的映射问题

huangapple go评论90阅读模式
英文:

Mapping issue between AWS SQS and its linked Lambda

问题

背景:

我有一个具有以下签名的函数:

public string Function(MyCommand cmd, ILambdaContext context)

其中MyCommand是:

public class MyCommand 
{
    public string Url { get; set; } = default!;
    public string Email { get; set; } = default!;
}

如果我在AWS UI上使用以下请求体对我的Lambda进行测试:

{
    "Url": "https://www.google.com/",
    "Email": "myemail@gmail.com"
}

我得到了我想要的结果。我已经在云监控日志中设置了日志记录,对于我的Lambda,我如预期地看到了以下日志:

Received json: {'Url': 'https://www.google.com/', 'Email': 'myemail@gmail.com'}

问题:

我还设置了一个SQS队列,并为我的Lambda设置了触发器。如果我将相同的JSON消息添加到该队列中,我会得到以下日志输出:

Received json: {'Url': null, 'Email': null}

所以看起来我的Lambda确实被正确触发了,因为它触发了我的日志代码,但是值为空。

可能是什么原因导致这种情况?

英文:

Background

I have a function with this signature:

public string Function(MyCommand cmd, ILambdaContext context)

Where MyCommand is:

public class MyCommand 
{
    public string Url { get; set; } = default!;
    public string Email { get; set; } = default!;
}

If I test my lambda on the AWS UI with this body:

{
    'Url': 'https://www.google.com/',
    'Email': 'myemail@gmail.com'
}

I get the result I want. I have logging in place, on the cloudwatch log, for my lambda I see the following as expected:

Received json: {'Url': 'https://www.google.com/', 'Email': 'myemail@gmail.com'}

The issue

I have also an SQS queue set up with a trigger on it for my Lambda. If I add the same JSON message to that queue I get the following log output:

Received json: {'Url': null, 'Email': null}

So it looks like my lambda is correctly triggered, because it hits my log code, but the values arrive empty.

What could be the cause of this?

答案1

得分: 1

要在Lambda函数中处理SQS消息,您需要接受SQSEvent数据类型。尽管您只从SQS发送了MyCommand有效载荷,但当它到达Lambda函数时,消息会被转换为SQSEvent

public string Function(SQSEvent sqsEvent, ILambdaContext context)
{
    return sqsEvent.Records.First().Body;
}

附注:要在Lambda函数中访问SQSEvent,您需要安装以下NuGet包:Amazon.Lambda.SQSEvents

英文:

To Process SQS messages within a Lambda function, you need to accept the SQSEvent data type. Though you sent only the MyCommand payload from SQS, when it reaches the lambda function, the message is transformed into SQSEvent

public string Function(SQSEvent sqsEvent, ILambdaContext context)
{
    return sqsEvent.Records.First().Body;
}

p.s. To access SQSEvent in the lambda function, you need to install the following NuGet package: Amazon.Lambda.SQSEvents

huangapple
  • 本文由 发表于 2023年8月9日 17:48:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/76866522.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定