当新消息到达 SQS 时触发 Lambda [英] Trigger Lambda when new message arrives to SQS

查看:43
本文介绍了当新消息到达 SQS 时触发 Lambda的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 AWS 的新手,这是我要解决的任务.SQS 队列已建立,并且不时有新消息传入.我想设置 Lambda 并检索这些消息并对这些消息的内容执行一些业务逻辑.

I'm new to AWS and here is the task I'm trying to solve. SQS queue is set up and from time to time new messages are coming to it. I want to set up Lambda and retrieve those messages and perform some business logic on the content of that messages.

在 AWS 站点和 Internet 上进行搜索,我了解到 SQS 本身不能成为 Lambda 的触发器,因此我需要设置 Cloud Watch 来按计划(例如每分钟)触发 Lambda.这是 代码示例来自 aws github 如何使用消息.

Searching across AWS site and Internet in general I understood that SQS itself can't be a trigger for Lambda, hence I need to set up Cloud Watch that will trigger Lambda by schedule (every minute for example). Here is code example from aws github how to consume a message.

到目前为止一切顺利.现在,在创建 Lambda 本身时,我需要指定输入类型来实现 RequestHandler 接口:

So far so good. Now, when creating Lambda itself, I need to specify the input type to implement RequestHandler interface:

public interface RequestHandler<I, O> {
O handleRequest(I var1, Context var2);

}

但如果我的 Lambda 不期待任何输入,它会自行前往 SQS 并提取消息,输入是否有意义?我可以让它为空甚至使用其他一些方法签名吗(当然在这种情况下不实现该接口)?

But if my Lambda is not expecting any input, it will go to SQS on its own and pull the messages does it make any sense to have input? Can I leave it void or even use some other method signature at all (of course not implementing that interface in this case)?

推荐答案

在这里,您的 Lambda 将获得对 cloudwatch 触发器的引用.

Here your Lambda will get a reference to the cloudwatch trigger.

您可能对此不感兴趣,但在某些情况下,即使触发器是 cloudwatch 警报,Lambda 也可能想知道触发器的详细信息

You might not be interested in that but there can be instances where the Lambda wants to know the trigger details even if the trigger is a cloudwatch alarm

以下是一个示例事件:

{ "version": "0", "id": "53dc4d37-cffa-4f76-80c9-8b7d4a4d2eaa","detail-type": "Scheduled Event", "source": "aws.events", "account":"123456789012", "time": "2015-10-08T16:53:06Z", "region": "us-east-1",资源": ["arn:aws:events:us-east-1:123456789012:rule/my-scheduled-rule"],详细信息":{} }

{ "version": "0", "id": "53dc4d37-cffa-4f76-80c9-8b7d4a4d2eaa", "detail-type": "Scheduled Event", "source": "aws.events", "account": "123456789012", "time": "2015-10-08T16:53:06Z", "region": "us-east-1", "resources": [ "arn:aws:events:us-east-1:123456789012:rule/my-scheduled-rule" ], "detail": {} }

这篇关于当新消息到达 SQS 时触发 Lambda的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆