簡體   English   中英

如何在用作CloudWatch Events目標時指定SQS消息屬性?

[英]How to specify SQS message attributes when used as CloudWatch Events target?

我想使用AWS CloudWatch Events以預定義的時間表向SQS發送消息。 消息正文無關緊要,但確實需要多個消息屬性。

在CloudFormation中創建此事件規則時,我找不到有關如何指定消息屬性的任何文檔。 目前資源看起來像這樣 -

ScheduledEvent:
  Type: AWS::Events::Rule
  Properties:
    RoleArn: !Ref ScheduledEventRole
    ScheduleExpression: !Ref ScheduledEventRule
    Targets:
    - Arn: !Ref Queue
      Id: !GetAtt Queue.Name
      Input: "message body"

消息體應該是什么,以便將屬性發送到SQS

幾天前我正在努力解決同樣的問題,我想出了一個解決這個問題的方法。 Amazon文檔或任何在線資源不提供有關如何使用CFT通過CloudWatch Events發送SQS消息屬性的信息。

在SQS中使用消息屬性的目的是傳遞可在實際處理消息體之前使用的元數據。 以下內容來自AWS文檔

您的使用者可以使用消息屬性以特定方式處理消息,而無需先處理消息正文。

但在我們的場景中,我們找不到發送消息屬性的方法。 因此,您可以在消息正文中包含消息屬性。 例如:

ScheduledEvent:
  Type: AWS::Events::Rule
  Properties:
    RoleArn: !Ref ScheduledEventRole
    ScheduleExpression: !Ref ScheduledEventRule
    Targets:
    - Arn: !Ref Queue
      Id: !GetAtt Queue.Name
      Input: "{\"attribute1\":\"value1\", \"attribute2\":\"value2\"}"

有了這個,您可以從郵件正文中訪問屬性。 但請記住,這違反了屬性的實際使用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM