Send message from Lambda to SQS in Go

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

Send message from Lambda to SQS in Go

问题

我正在寻找一种在使用Go编写的Lambda函数中向SQS队列发送消息的方法,我已经在AWS控制台中配置了将SQS队列连接为目标。我看到的代码示例(这里)建议我在调用SendMsg发送消息之前加载config,创建一个client并获取一个queueURL。这种方法在本地运行代码时效果很好,但在Lambda中似乎是不必要的,因为我已经在控制台中连接了SQS队列。当我已经将其连接为目标时,我是否需要告诉我的Lambda在哪里找到queueURL

另外,我尝试了这种方法,但在Lambda内部运行代码时收到了以下错误。在本地在Lambda之外调用代码时,可以正常找到queueURL

{
"errorMessage": "operation error SQS: GetQueueUrl, https response error StatusCode: 400, RequestID: 2f725471-ec4e-5380-99ed-0f708c1fcfa4, AWS.SimpleQueueService.NonExistentQueue: ",
"errorType": "OperationError"
}

这让我开始思考另一种情况,我有一个Lambda函数从队列中消费消息,并将SQS队列配置为触发器。我只需要遍历sqsEvent.Records而无需指定configclientqueueURL,因为我认为这在控制台中是在后台处理的,即:

package main

import (
"context"
"fmt"
"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-lambda-go/lambda"
)

func handler(ctx context.Context, sqsEvent events.SQSEvent) error {

for _, message := range sqsEvent.Records {
	fmt.Printf("The message %s for event source %s = %s \n", message.MessageId, message.EventSource, message.Body)
}

return nil

}

func main() {
lambda.Start(handler)
}

有没有一种方法可以在Lambda内部向队列发送消息,而无需首先找到queueURL

英文:

I'm looking to send a message to an SQS queue from a Lambda function written in Go, which in the AWS console I have configured so that the SQS queue is connected as a Destination.

Send message from Lambda to SQS in Go

The code sample I've seen (here) suggests I need to load a config, create a client and get a queueURL before issuing the message via a call to SendMsg. This approach works fine when running the code locally, but seems unnecessary within the Lambda given I'm connecting the SQS queue in the console. Do I need to tell my Lambda to find the queueURL when I've connected to it as a destination already?

As an aside, I tried this approach but was receiving the following error when running the code from within the Lambda. Invoking the code outside of a Lambda locally would find the queueURL without issue.

{
  "errorMessage": "operation error SQS: GetQueueUrl, https response error StatusCode: 400, RequestID: 2f725471-ec4e-5380-99ed-0f708c1fcfa4, AWS.SimpleQueueService.NonExistentQueue: ",
  "errorType": "OperationError"
}

This led me to look at the flip side, where I have a Lambda which is consuming from the queue and thereby has the SQS queue configured as a trigger. I simply need to iterate over the sqsEvent.Records without having to specify config, client or queueURL as I presume this is handled in the console behind the scenes, i.e.

package main

import (
	"context"
	"fmt"
	"github.com/aws/aws-lambda-go/events"
	"github.com/aws/aws-lambda-go/lambda"
)

func handler(ctx context.Context, sqsEvent events.SQSEvent) error {

	for _, message := range sqsEvent.Records {
		fmt.Printf("The message %s for event source %s = %s \n", message.MessageId, message.EventSource, message.Body)
	}

	return nil
}

func main() {
	lambda.Start(handler)
}

Is there a way of sending a message to a queue from within the Lambda without having to find the queueURL first?

答案1

得分: 2

我已经配置了SQS队列作为目标连接。

发送消息不需要你定义一个目标,目标是用于传递异步Lambda函数调用结果的。基本上,如果你不明确需要它们(并且你知道何时需要),它们就不是你所需要的。

给你的Lambda函数授予发送消息到队列的权限,并使用AWS SDK的SendMessage方法发送消息。不需要目标。

错误消息提示找不到队列,可能需要再次检查你是否在正确的区域中查找。

我只需要遍历sqsEvent.Records,而不需要指定配置、客户端或队列URL,因为我认为这在控制台中是在后台处理的。

是的,这是抽象的 - 最终,你的Lambda函数是由事件调用的,将Amazon SQS设置为触发器可以将事件传递给你的函数。

有没有一种方法可以在Lambda函数内部发送消息到队列,而不需要先找到队列URL呢?

让我们换个说法 - 有没有一种方法可以在不知道地址的情况下给某人发送一封信?当然不行,因为我们需要知道信件应该去哪里。

同样的概念适用于SQS队列。

英文:

> I have configured so that the SQS queue is connected as a Destination.

Sending a message doesn't need you to define a destination per se - destinations are for passing asynchronous Lambda function invocation results. Basically - if you don't explicitly need them (and you know when you do), they won't be what you need.

Give your Lambda permission to send messages to the queue, & use SendMessage using the AWS SDK to send messages. No destinations needed.

> AWS.SimpleQueueService.NonExistentQueue

The error message suggests it's not finding the queue, maybe double-check if you're looking in the right region.

> I simply need to iterate over the sqsEvent.Records without having to specify config, client or queueURL as I presume this is handled in the console behind the scenes, i.e.

Yes, this is abstracted away - ultimately, your Lambda function is invoked by events and setting Amazon SQS as a trigger allows it to pass events to your function.

> Is there a way of sending a message to a queue from within the Lambda without having to find the queueURL first?

Let's rephrase it as - is there a way of sending a letter to someone without having to find their address? Of course not, as we need to know where the letter needs to go.

The same concept applies to SQS queues.

huangapple
  • 本文由 发表于 2022年5月20日 17:43:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/72316792.html
匿名

发表评论

匿名网友

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

确定