Skip to content

Latest commit

 

History

History
74 lines (45 loc) · 2.53 KB

File metadata and controls

74 lines (45 loc) · 2.53 KB

Triggers NATS example

Simple example showing how to use NATS triggers with Scaleway Functions.

For complete examples of triggers in all function languages, see triggers-getting-started.

The example function is triggered by a message to a NATS topic, and will log the message body.

Requirements

This example requires Terraform.

Also, NATS must be activated on your project.

Setup

The Terraform configuration will create an example function, a NATS account, and a trigger. It will also write the NATS credentials to a file (files/nats-creds).

To authenticate with Scaleway, you can either set up the Scaleway CLI, from which Terraform can extract credentials, or you can export SCW_ACCESS_KEY, SCW_SECRET_KEY and SCW_DEFAULT_PROJECT_ID.

Once auth is set up, you can run:

terraform init
terraform apply

You should be able to see your resources in the Scaleway console:

Running

You can trigger your functions by sending messages to the NATS topic associated to the trigger (triggers-nats-topic). Below is a description of how to do this with our dummy tests/send_messages.py script.

Setup

First you need to extract your NATS endpoint from Terraform:

export NATS_ENDPOINT=$(terraform output -raw nats_endpoint)

Then you can set up a Python environment in the tests directory, e.g.

cd tests
python3 -m venv venv
source venv/bin/activate
pip3 install -r requirements.txt

Sending messages

Now you can run the send_messages.py script to send a message to the NATS topic:

python3 send_messages.py

Viewing function logs

In your Cockpit, you can access the logs from your NATS topics and functions.

Navigate from your Cockpit to Grafana, and find the Serverless Functions Logs dashboard. There you should see logs from your function, printing the body of the NATS message.

Cleanup

To delete all the resources used in this example, you can run the following from the root of the project:

terraform destroy