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

Viewed 1415

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?

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.

Related