|
| 1 | +# Triggers NATS example |
| 2 | + |
| 3 | +Simple example showing how to use NATS triggers with Scaleway Functions. |
| 4 | + |
| 5 | +For complete examples of triggers in all function languages, see [triggers-getting-started](../triggers-getting-started). |
| 6 | + |
| 7 | +The example function is triggered by a message to a NATS topic, and will log the message body. |
| 8 | + |
| 9 | +## Requirements |
| 10 | + |
| 11 | +This example requires [Terraform](https://www.scaleway.com/en/docs/tutorials/terraform-quickstart/). |
| 12 | + |
| 13 | +Also, NATS **must** be [activated](https://www.scaleway.com/en/docs/serverless/messaging/how-to/get-started/#how-to-create-a-nats-account) on your project. |
| 14 | + |
| 15 | +## Setup |
| 16 | + |
| 17 | +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`). |
| 18 | + |
| 19 | +To authenticate with Scaleway, you can either set up the [Scaleway CLI](https://www.scaleway.com/en/cli/), from which Terraform can extract credentials, or you can export `SCW_ACCESS_KEY`, `SCW_SECRET_KEY` and `SCW_DEFAULT_PROJECT_ID`. |
| 20 | + |
| 21 | +Once auth is set up, you can run: |
| 22 | + |
| 23 | +```console |
| 24 | +terraform init |
| 25 | +terraform apply |
| 26 | +``` |
| 27 | + |
| 28 | +You should be able to see your resources in the Scaleway console: |
| 29 | + |
| 30 | +- NATS accounts can be found in the [MnQ section](https://console.scaleway.com/messaging/protocols/fr-par/nats/accounts) |
| 31 | +- Functions can be found in the `triggers-nats` namespace in the [Serverless functions section](https://console.scaleway.com/functions/namespaces) |
| 32 | + |
| 33 | +## Running |
| 34 | + |
| 35 | +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. |
| 36 | + |
| 37 | +### Setup |
| 38 | + |
| 39 | +First you need to extract your NATS endpoint from Terraform: |
| 40 | + |
| 41 | +```console |
| 42 | +export NATS_ENDPOINT=$(terraform output -raw nats_endpoint) |
| 43 | +``` |
| 44 | + |
| 45 | +Then you can set up a Python environment in the `tests` directory, e.g. |
| 46 | + |
| 47 | +```console |
| 48 | +cd tests |
| 49 | +python3 -m venv venv |
| 50 | +source venv/bin/activate |
| 51 | +pip3 install -r requirements.txt |
| 52 | +``` |
| 53 | + |
| 54 | +### Sending messages |
| 55 | + |
| 56 | +Now you can run the `send_messages.py` script to send a message to the NATS topic: |
| 57 | + |
| 58 | +```console |
| 59 | +python3 send_messages.py |
| 60 | +``` |
| 61 | + |
| 62 | +### Viewing function logs |
| 63 | + |
| 64 | +In your [Cockpit](https://console.scaleway.com/cockpit), you can access the logs from your NATS topics and functions. |
| 65 | + |
| 66 | +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. |
| 67 | + |
| 68 | +## Cleanup |
| 69 | + |
| 70 | +To delete all the resources used in this example, you can run the following from the root of the project: |
| 71 | + |
| 72 | +```console |
| 73 | +terraform destroy |
| 74 | +``` |
0 commit comments