NavigationContentFooter
Jump toSuggest an edit

Using SNS and SQS with the AWS-CLI

Reviewed on 16 April 2024Published 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.

Before you start

To complete the actions presented below, you must have:

  • A Scaleway account logged into the console
  • Owner status or IAM permissions allowing you to perform actions in the intended Organization
  • Activated SQS/SNS
  • Valid credentials for SQS/SNS
  • Connected SQS/SNS to the AWS-CLI
  • jq installed on your machine

Getting started with SQS

  1. Use the following command to create a queue:

    aws sqs create-queue --queue-name MyQueue | tee my-queue.json
  2. Use the following command to list existing queues:

    aws sqs list-queues
  3. 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."
  4. 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
  5. 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)
  6. 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

  1. Use the following command to create a topic:

    aws sns create-topic --name MyTopic | tee my-topic.json
  2. Use the following command to list existing topics:

    aws sns list-topics

Preparing and subscribing to an SQS target for SNS messages

This is no longer supported. Watch this space for future developments.

Preparing and subscribing to an HTTP/HTTPS target for SNS messages

  1. Get the public endpoint of the HTTP server you want to forward your messages to.

  2. 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
  3. 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
    }
  4. 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

  1. Create the function following the steps detailed in the Scaleway Functions Quickstart.

  2. 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)"
  3. 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

  1. Use the following command to list subscriptions:

    aws sns list-subscriptions
  2. 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)
  3. 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
  4. 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)
  5. Use the following command to delete the subscription:

    aws sns unsubscribe --subscription-arn $(jq -r .SubscriptionArn my-subscription.json)
  6. 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

  7. Use the following command to delete the topic:

    aws sns delete-topic --topic-arn $(jq -r .TopicArn my-topic.json)
Docs APIScaleway consoleDedibox consoleScaleway LearningScaleway.comPricingBlogCarreer
© 2023-2024 – Scaleway