Amazon SQS、Amazon SNS 与 Tools for Windows PowerShell - AWS Tools for PowerShell

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

Amazon SQS、Amazon SNS 与 Tools for Windows PowerShell

本节提供演示如何执行以下操作的示例:

  • 创建 Amazon SQS 队列并获取队列 ARN(Amazon 资源名称)。

  • 创建 Amazon SNS 主题。

  • 向 SNS 主题授予权限,以便该主题向队列发送消息。

  • 为队列订阅 SNS 主题

  • 向 IAM 用户或AWS账户授予权限,以便上述用户或账户能够发布 SNS 主题并读取来自 SQS 队列的消息。

  • 通过向主题发布消息并读取来自队列的消息来验证结果。

创建 Amazon SQS 队列并获取队列 ARN

以下命令在您的默认区域中创建 SQS 队列。输出会显示新队列的 URL。

PS > New-SQSQueue -QueueName myQueue https://sqs.us-west-2.amazonaws.com/123456789012/myQueue

以下命令检索队列的 ARN。

PS > Get-SQSQueueAttribute -QueueUrl https://sqs.us-west-2.amazonaws.com/123456789012/myQueue -AttributeName QueueArn ... QueueARN : arn:aws:sqs:us-west-2:123456789012:myQueue ...

创建 Amazon SNS 主题

以下命令在您的默认区域中创建 SNS 主题,并返回新主题的 ARN。

PS > New-SNSTopic -Name myTopic arn:aws:sns:us-west-2:123456789012:myTopic

向 SNS 主题授予权限

下面的示例脚本创建一个 SQS 队列和一个 SNS 主题,并授予对 SNS 主题的权限,以便它可以将消息发送到 SQS 队列:

# create the queue and topic to be associated $qurl = New-SQSQueue -QueueName "myQueue" $topicarn = New-SNSTopic -Name "myTopic" # get the queue ARN to inject into the policy; it will be returned # in the output's QueueARN member but we need to put it into a variable # so text expansion in the policy string takes effect $qarn = (Get-SQSQueueAttribute -QueueUrl $qurl -AttributeNames "QueueArn").QueueARN # construct the policy and inject arns $policy = @" { "Version": "2012-10-17", "Statement": { "Effect": "Allow", "Principal": "*", "Action": "SQS:SendMessage", "Resource": "$qarn", "Condition": { "ArnEquals": { "aws:SourceArn": "$topicarn" } } } } "@ # set the policy Set-SQSQueueAttribute -QueueUrl $qurl -Attribute @{ Policy=$policy }

为队列订阅 SNS 主题

以下命令将队列 myQueue 订阅到 SNS 主题 myTopic,并返回订阅 ID:

PS > Connect-SNSNotification ` -TopicARN arn:aws:sns:us-west-2:123456789012:myTopic ` -Protocol SQS ` -Endpoint arn:aws:sqs:us-west-2:123456789012:myQueue arn:aws:sns:us-west-2:123456789012:myTopic:f8ff77c6-e719-4d70-8e5c-a54d41feb754

授予权限

以下命令授予对主题 sns:Publish 执行 myTopic 操作的权限。

PS > Add-SNSPermission ` -TopicArn arn:aws:sns:us-west-2:123456789012:myTopic ` -Label ps-cmdlet-topic ` -AWSAccountIds 123456789012 ` -ActionNames publish

以下命令授予对队列 sqs:ReceiveMessage 执行 sqs:DeleteMessagemyQueue 操作的权限。

PS > Add-SQSPermission ` -QueueUrl https://sqs.us-west-2.amazonaws.com/123456789012/myQueue ` -AWSAccountId "123456789012" ` -Label queue-permission ` -ActionName SendMessage, ReceiveMessage

验证结果

以下命令通过向 SNS 主题 myTopic 发布消息来测试新队列和主题,并返回 MessageId

PS > Publish-SNSMessage ` -TopicArn arn:aws:sns:us-west-2:123456789012:myTopic ` -Message "Have A Nice Day!" 728180b6-f62b-49d5-b4d3-3824bb2e77f4

以下命令从 SQS 队列 myQueue 检索消息并显示此消息。

PS > Receive-SQSMessage -QueueUrl https://sqs.us-west-2.amazonaws.com/123456789012/myQueue Attributes : {} Body : { "Type" : "Notification", "MessageId" : "491c687d-b78d-5c48-b7a0-3d8d769ee91b", "TopicArn" : "arn:aws:sns:us-west-2:123456789012:myTopic", "Message" : "Have A Nice Day!", "Timestamp" : "2019-09-09T21:06:27.201Z", "SignatureVersion" : "1", "Signature" : "llE17A2+XOuJZnw3TlgcXz4C4KPLXZxbxoEMIirelhl3u/oxkWmz5+9tJKFMns1ZOqQvKxk+ExfEZcD5yWt6biVuBb8pyRmZ1bO3hUENl3ayv2WQiQT1vpLpM7VEQN5m+hLIiPFcs vyuGkJReV7lOJWPHnCN+qTE2lId2RPkFOeGtLGawTsSPTWEvJdDbLlf7E0zZ0q1niXTUtpsZ8Swx01X3QO6u9i9qBFt0ekJFZNJp6Avu05hIklb4yoRs1IkbLVNBK/y0a8Yl9lWp7a7EoWaBn0zhCESe7o kZC6ncBJWphX7KCGVYD0qhVf/5VDgBuv9w8T+higJyvr3WbaSvg==", "SigningCertURL" : "https://sns.us-west-2.amazonaws.com/SimpleNotificationService-6aad65c2f9911b05cd53efda11f913f9.pem", "UnsubscribeURL" : "https://sns.us-west-2.amazonaws.com/?Action=Unsubscribe&SubscriptionArn=arn:aws:sns:us-west-2:123456789012:myTopic:22b77de7-a216-4000-9a23-bf465744ca84" } MD5OfBody : 5b5ee4f073e9c618eda3718b594fa257 MD5OfMessageAttributes : MessageAttributes : {} MessageId : 728180b6-f62b-49d5-b4d3-3824bb2e77f4 ReceiptHandle : AQEB2vvk1e5cOKFjeIWJticabkc664yuDEjhucnIOqdVUmie7bX7GiJbl7F0enABUgaI2XjEcNPxixhVc/wfsAJZLNHnl8SlbQa0R/kD+Saqa4OIvfj8x3M4Oh1yM1cVKpYmhAzsYrAwAD5g5FvxNBD6zs +HmXdkax2Wd+9AxrHlQZV5ur1MoByKWWbDbsqoYJTJquCclOgWIak/sBx/daBRMTiVQ4GHsrQWMVHtNC14q7Jy/0L2dkmb4dzJfJq0VbFSX1G+u/lrSLpgae+Dfux646y8yFiPFzY4ua4mCF/SVUn63Spy sHN12776axknhg3j9K/Xwj54DixdsegnrKoLx+ctI+0jzAetBR66Q1VhIoJAq7s0a2MseyOeM/Jjucg6Sr9VUnTWVhV8ErXmotoiEg==