Search code examples
bashamazon-web-servicesaws-cliamazon-sqs

Shorthand syntax for message-attributes in the send-message command in aws-cli for sqs


When trying to send a message using AWS CLI for SQS, I cannot get the shorthand syntax for the --message-attributes parameter to work.

Specifying a json file works fine, and the reference doesn't show an example for the shorthand option.

Here is the reference for this command which specifies the shorthand I'm trying to use but I can't get it to work: http://docs.aws.amazon.com/cli/latest/reference/sqs/send-message.html

Here's the command I've tried:

aws sqs send-message \
--queue-url https://sqs.us-east-1.amazonaws.com/0000000000/aa_queue_name \
--message-body "message body goes here" \
--message-attributes firstAttribute={DataType=String,StringValue="hello world"},secondAttribute={DataType=String,StringValue="goodbye world"}

I keep getting error messages:

Parameter validation failed: Invalid type for parameter MessageAttributes.contentType, value: StringValue=Snapshot, type: , valid types:

Anyone ever managed sending attributes for message using the shorthand?


Solution

  • Your Shorthand Syntax format is correct:

    MY_KEY={DataType=String, StringValue=MY_VALUE}

    You just forgot to enclose the commandline option with single or double quotes:

    aws sqs send-message \
      --queue-url https://sqs.us-east-1.amazonaws.com/0000000000/aa_queue_name \
      --message-body "message body goes here" \
      --message-attributes 'firstAttribute={DataType=String, StringValue="hello world"}, secondAttribute={DataType=String,StringValue="goodbye world"}'
    

    The above shortcut syntax should correctly produce a message with 2 extra headers, a.k.a. Message Attributes:

    firstAttribute=hello world
    secondAttribute=goodbye world
    

    NOTE:

    an attribute is a <class 'dict'>, so every attribute looks like a dictionary: {DataType=String, StringValue=MY_VALUE},

    • where supported DataTypes are String, Number, or Binary.

    • each DataType value can contain an optional custom extention, that is ignored by AWS. For example: String.uuid, Number.int, Binary.pdf.

    https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/sqs-message-attributes.html#message-attribute-components