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
?
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With