Using SNS and SQS with the AWS-CLI Reviewed on 16 April 2024 • Published on 04 April 2023
The AWS-CLI is an open-source tool built on top of the AWS SDK for Python (Boto) that provides commands for interacting with AWS services. Once you have connected Scaleway Messaging and Queuing SQS and/or SNS to the AWS-CLI , you can start creating, listing and managing your queues and topics, sending messages and much more, all from your command line.
To complete the actions presented below, you must have:
Use the following command to create a queue:
aws sqs create-queue --queue-name MyQueue | tee my-queue.json
Use the following command to list existing queues:
Use the following command to send messages to a queue:
aws sqs send-message --queue-url $(jq -r .QueueUrl my-queue.json) --message-body "Hello world!"
aws sqs send-message --queue-url $(jq -r .QueueUrl my-queue.json) --message-body "Second Message."
Use the following command to receive messages:
aws sqs receive-message --queue-url $(jq -r .QueueUrl my-queue.json) | tee message1.json
aws sqs receive-message --queue-url $(jq -r .QueueUrl my-queue.json) | tee message2.json
Use the following command to delete messages. This is necessary as once a message has been processed on your consumer side (typically by a worker), it will be re-queued unless it is explicitly deleted.
aws sqs delete-message --queue-url $(jq -r .QueueUrl my-queue.json) --receipt-handle $(jq -r .Messages[0].ReceiptHandle message1.json)
aws sqs delete-message --queue-url $(jq -r .QueueUrl my-queue.json) --receipt-handle $(jq -r .Messages[0].ReceiptHandle message2.json)
Use the following command to delete the queue itself:
aws sqs delete-queue --queue-url $(jq -r .QueueUrl my-queue.json)
Use the following command to create a topic:
aws sns create-topic --name MyTopic | tee my-topic.json
Use the following command to list existing topics:
This is no longer supported. Watch this space for future developments.
Get the public endpoint of the HTTP server you want to forward your messages to.
Use the following command to configure a subscription to push each new message sent on the topic to the HTTP server:
aws sns subscribe --topic-arn $(jq -r .TopicArn my-topic.json) --protocol http --notification-endpoint <YOUR-HTTP-ENDPOINT> | tee my-subscription.json
Find the HTTP request received by the HTTP server. It should have a body in json matching the following format. It contains information necessary to complete the subscription process:
{
"Type": "SubscriptionConfirmation",
"Token": "<REDACTED-CONFIRMATION-TOKEN>",
"MessageId": "<REDACTED-MESSAGE-ID>",
"TopicArn": "arn:scw:sns:fr-par:<REDACTED-ID>:MyTopic",
"Message": "You have chosen to subscribe to the topic arn:scw:sns:fr-par:<REDACTED-ID>:MyTopic.\nTo confirm the subscription, visit the SubscribeURL included in this message.",
"Timestamp": "2022-06-29T10:03:34Z",
"SignatureVersion": "1",
"Signature": "<REDACTED-SIGNATURE>",
"SigningCertURL": "http://<REDACTED-URL>/SNStest.crt",
"SubscribeURL": "<THE-CONFIRMATION-LINK>" // Get the confirmation link located here
}
Use the following command to confirm the subscription:
curl "<THE-CONFIRMATION-LINK>"
Create the function following the steps detailed in the Scaleway Functions Quickstart .
Get the function endpoint from the Scaleway console under “Functions” -> “[YOUR-FUNCTION-NAMESPACE]” -> “[YOUR-FUNCTION-NAME]” -> “Function Settings” tab -> “Function Endpoint”
Important Only the main generated endpoint of the function will work, not the aliases. The endpoint should match the following format:
https://<GENERATED-NAMESPACE-ENDPOINT>-<YOUR-FUNCTION-NAME>.functions.fnc.fr-par.scw.cloud
example: "https://mynamespacexxxxxxxx-myfunction.functions.fnc.fr-par.scw.cloud)"
Use the following command to configure a subscription to push each new message sent on this topic to the function:
aws sns subscribe --topic-arn $(jq -r .TopicArn my-topic.json) --protocol lambda --notification-endpoint <YOUR-FUNCTION-ENDPOINT> | tee my-subscription.json
Use the following command to list subscriptions:
aws sns list-subscriptions
Use the following command to publish a message on the topic:
aws sns publish --topic-arn $(jq -r .TopicArn my-topic.json) --message "Hello world!" --message-deduplication-id $(date +%s)
Use the following command to read the message received on an SQS target:
Note
For HTTP /HTTPS targets, you should have received the message on your server
For lambda targets, your function should have been called with the message as argument
aws sqs receive-message --queue-url $(jq -r .QueueUrl my-queue.json) | tee message1.json
Use the following command to delete the message received on an SQS target. This is necessary to prevent it from being re-queued:
aws sqs delete-message --queue-url $(jq -r .QueueUrl my-queue.json) --receipt-handle $(jq -r .Messages[0].ReceiptHandle message1.json)
Use the following command to delete the subscription:
aws sns unsubscribe --subscription-arn $(jq -r .SubscriptionArn my-subscription.json)
Use the following command to delete the SQS queue (if you had an SQS target):
aws sqs delete-queue --queue-url $(jq -r .QueueUrl my-queue.json)
Use the following command to delete the topic:
aws sns delete-topic --topic-arn $(jq -r .TopicArn my-topic.json)