Using SNS and SQS with the AWS-CLI
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 your Scaleway Messaging and Queuing SQS/SNS namespace 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.
You may need certain IAM permissions to carry out some actions described on this page. This means:
- you are the Owner of the Scaleway Organization in which the actions will be carried out, or
- you are an IAM user of the Organization, with a policy granting you the necessary permission sets
- You have an account and are logged into the Scaleway console
- You have created an SQS/SNS Messaging namespace
- You have generated credentials for your SQS/SNS Messaging and Queuing namespace
- You have connected your SQS/SNS namespace to the AWS-CLI
- You have jq installed on your machine
Getting started with SQS
-
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:
aws sqs list-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.jsonaws 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)
Getting started with SNS
-
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:
aws sns list-topics
Preparing and subscribing to an SQS target for SNS messages
-
Use the following command to create a queue, which must be in the same namespace as the SNS topic:
aws sqs create-queue --queue-name MyQueue | tee my-queue.json -
Use the following command to get the queue ARN:
aws sqs get-queue-attributes --queue-url $(jq -r .QueueUrl my-queue.json) --attribute-name QueueArn | tee my-queue-arn.json -
Use the following command to configure a subscription to push each new message sent on this topic to the queue:
aws sns subscribe --topic-arn $(jq -r .TopicArn my-topic.json) --protocol sqs --notification-endpoint $(jq -r .Attributes.QueueArn my-queue-arn.json) | tee my-subscription.json
Preparing and subscribing to an HTTP/HTTPS target for SNS messages
-
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>"
Preparing and subscribing to a lambda (Scaleway Serverless Functions) target for SNS messages
-
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.cloudexample: "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
Continuing with SNS
-
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)Tip:For lambda, delete the function (if necessary), using the Scaleway console
-
Use the following command to delete the topic:
aws sns delete-topic --topic-arn $(jq -r .TopicArn my-topic.json)