使用过滤器订阅主题的 aws cli 命令
aws cli command to subscribe to a topic with filters
我正在尝试编写跨账户 aws cli 命令来订阅主题并同时为该订阅创建过滤器。下面是我的命令的样子。
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\"]}'}"
当我 运行 这个时,我遇到了以下错误。
Unknown options: --attributes, [\value1\,, \value2\]}'}, {'RawMessageDelivery': 'true', 'FilterPolicy': '{" filter\:
我可以访问这两个 aws 帐户的管理员权限。对我做错了什么有什么建议吗?
编辑:
我在 windows.
的 VS Code powershell 终端中 运行ning 这个
可能有更简单的方法(例如使用 --cli-input-json
并在文件中提供 JSON),但我成功了:
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\\"]}\"}'
问题是 JSON 包含在字符串中,需要将 \"
转义为 \\"
。
此 Github 存储库有一个示例: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
前面回答的整体流程还可以,但是有个大问题:
我不知道为什么没有人提到这个,但是如果你没有正确设置 SQS 队列策略 文档,整个事情就不会起作用.
github 示例没有这样做,出于某种原因,AWS 文档甚至没有提及它。
参考这个问题:Set SQS policy document with AWS CLI
我浪费了几天时间解决这个问题。希望对你有帮助。
我正在尝试编写跨账户 aws cli 命令来订阅主题并同时为该订阅创建过滤器。下面是我的命令的样子。
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\"]}'}"
当我 运行 这个时,我遇到了以下错误。
Unknown options: --attributes, [\value1\,, \value2\]}'}, {'RawMessageDelivery': 'true', 'FilterPolicy': '{" filter\:
我可以访问这两个 aws 帐户的管理员权限。对我做错了什么有什么建议吗?
编辑: 我在 windows.
的 VS Code powershell 终端中 运行ning 这个可能有更简单的方法(例如使用 --cli-input-json
并在文件中提供 JSON),但我成功了:
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\\"]}\"}'
问题是 JSON 包含在字符串中,需要将 \"
转义为 \\"
。
此 Github 存储库有一个示例: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
前面回答的整体流程还可以,但是有个大问题:
我不知道为什么没有人提到这个,但是如果你没有正确设置 SQS 队列策略 文档,整个事情就不会起作用.
github 示例没有这样做,出于某种原因,AWS 文档甚至没有提及它。
参考这个问题:Set SQS policy document with AWS CLI
我浪费了几天时间解决这个问题。希望对你有帮助。