Amazon SQS, Amazon SNS e strumenti per Windows PowerShell - AWS Strumenti per PowerShell (versione 4)

È stata rilasciata la versione 5 (V5) di! AWS Strumenti per PowerShell

Per iniziare a utilizzare la nuova versione degli strumenti, consulta la Guida per l'AWS Strumenti per PowerShell utente (V5), in particolare l'argomento per la migrazione alla V5.

Le traduzioni sono generate tramite traduzione automatica. In caso di conflitto tra il contenuto di una traduzione e la versione originale in Inglese, quest'ultima prevarrà.

Amazon SQS, Amazon SNS e strumenti per Windows PowerShell

In questa sezione vengono forniti esempi in cui viene illustrato come:

  • Creare una coda Amazon SQS e ottenere l'ARN della coda (Amazon Resource Name).

  • Creazione di un argomento Amazon SNS.

  • Concedere l'autorizzazione all'argomento SNS, in modo che possa inviare messaggi alla coda.

  • Sottoscrivere la coda all'argomento SNS

  • Concedi agli utenti o agli AWS account IAM le autorizzazioni per pubblicare sull'argomento SNS e leggere i messaggi dalla coda SQS.

  • Verificare i risultati pubblicando un messaggio nell'argomento e leggendo il messaggio dalla coda.

Creare una coda Amazon SQS e ottenere l'ARN della coda

Il comando seguente crea una coda SQS nella regione predefinita. L'output mostra l'URL della nuova coda.

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

Il comando seguente recupera l'ARN della coda.

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

Creare un argomento Amazon SNS.

Il comando seguente crea un argomento SNS nella regione predefinita e restituisce l'ARN del nuovo argomento.

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

Concedere le autorizzazioni all'argomento SNS

Il seguente script di esempio crea sia una coda SQS sia un argomento SNS e concede le autorizzazioni all'argomento SNS in modo che possa inviare messaggi alla coda 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 }

Sottoscrivere la coda all'argomento SNS

Il comando seguente sottoscrive la coda myQueue all'argomento SNS myTopic e restituisce l'ID sottoscrizione:

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

Concedere le autorizzazioni

Il comando seguente concede l'autorizzazione per eseguire l'azione sns:Publish sull'argomento myTopic.

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

Il comando seguente concede l'autorizzazione per eseguire le azioni sqs:ReceiveMessage e sqs:DeleteMessage sulla coda myQueue.

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

Verificare i risultati

Il comando seguente verifica la nuova coda e l'argomento pubblicando un messaggio nell'argomento SNS myTopic e restituisce MessageId.

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

Il comando seguente recupera il messaggio dalla coda SQS myQueue e lo mostra.

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==