Using Scaleway Topics and Events 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 Scaleway Topics and Events to the AWS-CLI, you can start creating, listing and managing your topics, sending messages and much more, all from your command line.
Before you start
To complete the following actions, you must have:
- A Scaleway account logged into the console (External link)
- Owner status or IAM permissions allowing you to perform actions in the intended Organization
- Valid credentials for Topics and Events
- Connected Topics and Events to the AWS-CLI
- jq (External link) installed on your machine
Getting started with Topics and Events
-
Use the following command to create a topic:
Copyaws sns create-topic --name MyTopic | tee my-topic.json -
Use the following command to list existing topics:
Copyaws sns list-topics
Preparing and subscribing to an HTTP/HTTPS target for Topics and Events 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:
Copyaws 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 the information necessary to complete the subscription process:
Copy{ "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:
Copycurl "<THE-CONFIRMATION-LINK>"
Preparing and subscribing to a lambda (Scaleway Serverless Functions) target for Topics and Events messages
-
Create the function following the steps detailed in the Scaleway Functions Quickstart.
-
Get the function endpoint from the Scaleway console (External link) under "Functions" -> "[YOUR-FUNCTION-NAMESPACE]" -> "[YOUR-FUNCTION-NAME]" -> "Function Settings" tab -> "Function Endpoint"
-
Use the following command to configure a subscription to push each new message sent on this topic to the function:
Copyaws 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
-
Use the following command to list subscriptions:
Copyaws sns list-subscriptions -
Use the following command to publish a message on the topic:
Copyaws 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 a Scaleway Queues target:
Copyaws sqs receive-message --queue-url $(jq -r .QueueUrl my-queue.json) | tee message1.json -
Use the following command to delete the message received on a Scaleway Queues target. This is necessary to prevent it from being re-queued:
Copyaws 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:
Copyaws sns unsubscribe --subscription-arn $(jq -r .SubscriptionArn my-subscription.json) -
Use the following command to delete the Scaleway queue (if you had a Scaleway Queues target):
Copyaws sqs delete-queue --queue-url $(jq -r .QueueUrl my-queue.json) -
Use the following command to delete the topic:
Copyaws sns delete-topic --topic-arn $(jq -r .TopicArn my-topic.json)