NavigationContentFooter
Jump toSuggest an edit

Using Scaleway Queues and Scaleway Topics and Events with the AWS-CLI

Reviewed on 18 October 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 Queues and/or Topics and Events 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
  • Valid credentials for Queues and/or Topics and Events
  • Connected Queues and/or Topics and Events to the AWS-CLI
  • jq installed on your machine

Getting started with Scaleway Queues

  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 Topics and Events

  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 HTTP/HTTPS target for Topics and Events 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 Topics and Events 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 Topics and Events

  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 a Scaleway Queues 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 a Scaleway Queues 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 Scaleway queue (if you had a Scaleway Queues 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)
Was this page helpful?
API DocsScaleway consoleDedibox consoleScaleway LearningScaleway.comPricingBlogCareers
© 2023-2024 – Scaleway