Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 785
user007 Avatar asked Apr 16 '20 19:04

user007


People also ask

Which CLI command is used to tag AWS resources?

By default, the AWS CLI uses SSL when communicating with AWS services. For each SSL connection, the AWS CLI will verify SSL certificates. This option overrides the default behavior of verifying SSL certificates.

Which of the following can subscribe as an endpoint to Amazon SNS topic?

You specify the endpoint using its URL. To subscribe to a topic, you can use the Amazon SNS console, the sns-subscribe command, or the Subscribe API action.

What is SNS topic in AWS?

An Amazon SNS topic is a logical access point that acts as a communication channel. A topic lets you group multiple endpoints (such as AWS Lambda, Amazon SQS, HTTP/S, or an email address).

What are the different output formats available when working with the AWS CLI?

The AWS CLI supports the following output formats: json – The output is formatted as a JSON string. yaml – The output is formatted as a YAML string. yaml-stream – The output is streamed and formatted as a YAML string.


2 Answers

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

like image 189
John Rotenstein Avatar answered Nov 09 '22 23:11

John Rotenstein


This Github repo has an example: https://github.com/Haple/sns-sqs-subscribe

#!/bin/sh

# SETUP

queue_arn=$(awslocal sqs create-queue --queue-name test_queue --output text)

echo "Queue ARN: $queue_arn"

topic_arn=$(awslocal sns create-topic --name test_topic --output text)

echo "Topic ARN: $topic_arn"

subscription_arn=$(awslocal sns subscribe \
    --topic-arn "$topic_arn" \
    --protocol sqs \
    --notification-endpoint "$queue_arn" \
    --output text)

echo "Subscription ARN: $subscription_arn" 

awslocal sns set-subscription-attributes \
    --subscription-arn "$subscription_arn" \
    --attribute-name FilterPolicy \
    --attribute-value "{ \"EVENT_TYPE\": [\"SUCCESS\"] }"

# TEST

awslocal sns publish \
    --topic-arn "$topic_arn" \
    --message "SUCCESS PAYLOAD (SHOULD GO TO THE QUEUE)" \
    --message-attributes '{"EVENT_TYPE" : { "DataType":"String", "StringValue":"SUCCESS"}}'

awslocal sns publish \
    --topic-arn "$topic_arn" \
    --message "ERROR PAYLOAD (SHOULD NOT GO TO THE QUEUE)" \
    --message-attributes '{"EVENT_TYPE" : { "DataType":"String", "StringValue":"ERROR"}}'


awslocal sqs get-queue-attributes \
    --queue-url http://localhost:4576/queue/test_queue \
    --attribute-names All
like image 45
Haple Avatar answered Nov 10 '22 00:11

Haple