简体   繁体   English

跨AWS账户在2个SQS队列中复制消息的最佳方法

[英]Best way to copy messages within 2 SQS queues across AWS accounts

What is the best way to copy messages from one SQS queue to the other across AWS accounts? 将消息从一个SQS队列复制到整个AWS账户的另一个队列的最佳方法是什么? After searching, I attached the policy granting full read and write access for the IAM user(in the destination account) to the source queue. 搜索之后,我将允许IAM用户(在目标帐户中)的完全读写访问权限的策略附加到源队列。 Then I attached a Permission to the source queue for Everyone to have send and receive message access on the queue. 然后,我在源队列上附加了一个权限,以便每个人都可以在队列上发送和接收消息访问权限。

I am getting below error - 我遇到错误-

Exception in thread "main" com.amazonaws.AmazonServiceException: The security token included in the request is invalid. 线程“ main” com.amazonaws.AmazonServiceException中的异常:请求中包含的安全令牌无效。 (Service: AmazonSQS; Status Code: 403 (服务:AmazonSQS;状态码:403

Probably there is something wrong with the IAM user credentials, however, I have refreshed the credentials, but I'm still getting the error IAM用户凭据可能有问题,但是,我刷新了凭据,但仍然收到错误

Permissions are a secondary matter. 许可是次要的。 The primary question is how to "copy messages from one SQS queue to the other across AWS accounts". 主要问题是如何“将消息从一个SQS队列复制到整个AWS账户中的另一个”。

Amazon SQS very recently introduced the ability to trigger AWS Lambda functions when a message arrives. Amazon SQS最近引入了在消息到达时触发AWS Lambda函数的功能 You could create a Lambda function that then creates the message in another queue (which could be in a different account, region, etc). 您可以创建一个Lambda函数,然后在另一个队列(可以在不同的帐户,区域等)中创建消息。 However, the original message will be deleted once it is processed , so it isn't really "copying" the message. 但是, 原始邮件在处理后将被删除 ,因此它并不是真正的“复制”邮件。

The better method would be: 更好的方法是:

  • Instead of sending a message to an Amazon SQS message, send it to an Amazon SNS topic 而不是将消息发送到Amazon SQS消息, 而是将其发送到Amazon SNS主题
  • You can then subscribe the Amazon SQS queues to the topic -- yes, you can subscribe multiple queues to the topic 然后,您可以为该主题订阅Amazon SQS队列 -是的,您可以为该主题订阅多个队列
  • This way, whenever a message is sent to the SNS topic, both queues will receive it 这样,每当一条消息发送到SNS主题时,两个队列都将收到该消息。

See: Sending Amazon SNS messages to an Amazon SQS queue in a different account - Amazon Simple Notification Service 请参阅:将Amazon SNS消息发送到其他帐户中的Amazon SQS队列-Amazon Simple Notification Service

There is a great video from AWS re:Invent that shows how to use SQS and SNS together. AWS re:Invent上有一段很棒的视频 ,展示了如何一起使用SQS和SNS。

Update: How to copy existing messages in a queue 更新:如何复制队列中的现有消息

"there are existing messages in a queue that needs to be copied to a new queue in a new account." “队列中已有消息,需要将其复制到新帐户中的新队列中。”

If that's the case, then you are in trouble! 如果真是这样,那么您就有麻烦了!

The idea of a queue is that a message is retrieved processed and deleted. 队列的思想是检索一条消息并进行处理和删除。 Clearly this is a bad idea for "copying" a message because you don't want to delete them. 显然,这对于“复制”邮件是个坏主意,因为您不想删除它们。

You could try increasing the invisibility timeout of the queue, then retrieve all the messages in the queue. 您可以尝试增加队列的隐身超时时间 ,然后检索队列中的所有消息。 They will be placed in-flight , which means they are temporarily invisible but will reappear on the queue if not deleted at the end of the invisibility timeout period. 它们将在飞行中放置,这意味着它们暂时不可见,但是如果在看不见超时时间结束时未删除,它们将重新出现在队列中。 So, your app could read each message (but not delete them) and create new messages in the second queue. 因此,您的应用可以读取每条消息(但不能删除它们)并在第二个队列中创建新消息。 Then, each original message would reappear in the original queue. 然后,每个原始消息将重新出现在原始队列中。

Or, you could write an app that reads each message and sends it to two queues, deleting the source messages as it goes. 或者,您可以编写一个应用程序来读取每条消息并将其发送到两个队列,并随即删除源消息。 Then, treat one of the new queues as a replacement for the original queue. 然后,将其中一个新队列替换为原始队列。

Bottom line: There is no pre-provided method. 底线:没有预先提供的方法。 You'll have to do it yourself. 您必须自己做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM