You can use this example to publish a message to an SQS queue using the MNQ namespace from the Scaleway Go SDK.
This example assumes that you are familiar with some products of Scaleway's ecosystem:
- how serverless functions work. If needed, you can check Scaleway official documentation.
- how Messaging and Queuing works. Please refer to scaleway's documentation here.
This example uses the Scaleway Serverless Framework Plugin. Please set up your environment with the requirements stated in the Scaleway Serverless Framework Plugin before trying out the example.
Additionnaly it uses the serverless-functions-go library for local testing.
This example shows how to create a queue and send a message to a SQS. It can be extended to retrieve or delete queues, send batch messages, delete or retrieve messages, etc...
The function is publishing a message to an SQS queue. The message is a JSON object with the following structure:
{
"username": "John Doe",
"message": "Hello World!"
}
This function uses Golang 1.18 runtime.
To use this example, the following environment variables are needed:
SCW_ACCESS_KEY
: Your Scaleway access keySCW_SECRET_KEY
: Your Scaleway secret keySCW_DEFAULT_PROJECT_ID
: Your Scaleway project IDSCW_DEFAULT_REGION
: Your Scaleway regionSCW_DEFAULT_ZONE
: Your Scaleway zoneSQS_ACCESS_KEY
: Your SQS access keySQS_SECRET_KEY
: Your SQS secret key
Once your environment is set up, you can test your function locally with:
go run test/main.go
This will launch a local server, allowing you to test the function. For that, you can run in another terminal:
curl -X POST http://localhost:8080 -d '{"username": "John Doe", "message": "Hello World!"}'
The status code returned by this request should be 200
.
Finally, if the test succeeded, you can deploy your function with:
serverless deploy
Then, from the given URL, you can run:
# POST request
curl -i -X POST <function URL> -d '{"username": "John Doe", "message": "Hello World!"}'
Once again, the expected status code for this request is 200
.