Search code examples
amazon-web-servicespowershellamazon-sqspublish-subscribeamazon-sns

aws cli command to subscribe to a topic with filters


I'm trying to write a cross account aws cli command to subscribe to a topic and create a filter for that subscription at the same time. Below is how my command looks like.

aws sns subscribe --topic-arn arn:aws:sns:region:accountId:my_topic --protocol sqs --notification-endpoint arn:aws:sqs:region:differentAccountId:my_sqs_queue --attributes "{'RawMessageDelivery': 'true', 'FilterPolicy': '{\"filter\": [\"value1\", \"value2\"]}'}"

I'm getting below error when I run this.

Unknown options: --attributes, [\value1\,, \value2\]}'}, {'RawMessageDelivery': 'true', 'FilterPolicy': '{" filter\:

I've access to admin access both the aws accounts. Any suggestions on what I'm doing wrong?

EDIT: I'm running this in VS Code powershell terminal in windows.


Solution

  • There's probably an easier way to do it (eg using --cli-input-json and providing JSON in a file), but I got this working:

    aws sns subscribe \
      --topic-arn arn:aws:sns:region:accountId:my_topic \
      --protocol sqs \
      --notification-endpoint arn:aws:sqs:region:differentAccountId:my_sqs_queue \
      --attributes '{\"RawMessageDelivery\": \"true\", \"FilterPolicy\": \"{\\\"filter\\\": [\\\"value1\\\", \\\"value2\\\"]}\"}'
    

    The problem was the JSON included in a string, which needed \" to be escaped as \\\".