Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I want to use AWS CloudWatch Events to send a message to SQS on a predefined schedule. The message body is irrelevant, but it does require several message attributes.

While creating this Events rule in CloudFormation I could not find any documentation on how to specify the message attributes. At the moment the resource looks like this -

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

What should be the message body so that attributes are sent to SQS?

like image 864
Evgeny Avatar asked May 28 '17 09:05

Evgeny


People also ask

How do I change the message attribute in SQS?

To encode a single Amazon SQS message attributeEncode the transport type ( String or Binary ) of the value (1 byte). The logical data types String and Number use the String transport type. The logical data type Binary uses the Binary transport type. For the String transport type, encode 1.

Which parameter indicates a change in the Amazon Web Services AWS environment when creating an event in Amazon CloudWatch events?

AWS CloudWatch Events make use of 3 main components: events, rules and targets. An event indicates a change in your AWS environment, a target processes events and a rule matches any incoming events and routes them to targets for processing.

Which scenarios indicate that you should use an Amazon SQS standard queue?

You can use standard message queues in many scenarios, as long as your application can process messages that arrive more than once and out of order, for example: Decouple live user requests from intensive background work: Let users upload media while resizing or encoding it.

How can you ensure that your SQS messages arrive in the correct order?

To ensure that Amazon SQS preserves the order in which messages are sent and received, ensure that multiple senders send each message with a unique message group ID. For more information, see FIFO Queue Logic in the Amazon SQS Developer Guide.


1 Answers

I was struggling with the same issue a few days ago, and I came up with a work around for this. Amazon documentation or any online resource does not provide information on how to send SQS Message Attributes via CloudWatch Events using CFTs.

The purpose of using Message Attributes in SQS is to pass meta data which can be used before actually processing the message body. The below is from AWS docs.

Your consumer can use message attributes to handle a message in a particular way without having to process the message body first.

But in our scenario, we cannot find a way to send message attributes. Hence,you can include the message attributes in the Message Body. For example:

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\"}"

With this, you can access the attributes from the message body. But keep in mind that this violates the actual use of attributes.

like image 180
Keet Sugathadasa Avatar answered Oct 17 '22 15:10

Keet Sugathadasa