Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(triggers): update functions examples for new MnQ API #52

Merged
merged 4 commits into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 17 additions & 17 deletions functions/triggers-getting-started/.terraform.lock.hcl

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

47 changes: 42 additions & 5 deletions functions/triggers-getting-started/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,34 +8,65 @@ In each example, a function is triggered by a SQS queue with a message containin

This example requires [Terraform](https://www.scaleway.com/en/docs/tutorials/terraform-quickstart/).

Also, SQS **must** be [activated](https://www.scaleway.com/en/docs/serverless/messaging/how-to/get-started/#how-to-activate-sqs-or-sns) on your project.

## Setup

The Terraform configuration will deploy a function for each language, showing how to use triggers with each language.

It will also create a SQS queue per function to trigger it.

After exporting `SCW_ACCESS_KEY`, `SCW_SECRET_KEY` and `SCW_DEFAULT_PROJECT_ID` variables (for authentication), you can:

```console
terraform init
terraform apply
```

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

- Queues can be found in the [MnQ section](https://console.scaleway.com/messaging/protocols/fr-par/sqs/queues)
- Functions can be found in the `triggers-getting-started` namespace in the [Serverless functions section](https://console.scaleway.com/functions/namespaces)

## Running

You can use the `tests/send_messages.py` script to send a message to the SQS queue of each function.
You can trigger your functions by sending messages to any of the associated SQS queues. Below is a description of how to do this with our dummy `tests/send_messages.py` script.

### Setup

First you need to expose your SQS access and secret keys:

```console
export AWS_ACCESS_KEY_ID=$(terraform output -raw sqs_access_key)
export AWS_SECRET_ACCESS_KEY=$(terraform output -raw sqs_secret_key)
python tests/send_messages.py
```

In Cockpit, you should see the functions being triggered and the result of the factorial being printed in the logs.
Then you can set up a Python environment in the `tests` directory, e.g.

```console
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 SQS queue of each function:

```console
python3 send_messages.py
```

### Viewing function logs

In your [Cockpit](https://console.scaleway.com/cockpit), you can access the logs from your queues and functions.

Navigate from your Cockpit to Grafana, and find the `Serverless Functions Logs` dashboard. There you should see something like the following, showing that your functions were triggered by messages on the queues:

```console
...
DEBUG - php: factorial of 17 is 355687428096000 source=user stream=stdout
DEBUG - php: factorial of 17 is 355687428096000 source=user stream=stdout
2023-09-11 10:36:19.994 DEBUG - Function Triggered: / source=core
2023-09-11 10:36:19.993 DEBUG - php: factorial of 13 is 6227020800 source=user stream=stdout
2023-09-11 10:36:19.991 DEBUG - Function Triggered: / source=core
Expand All @@ -49,10 +80,16 @@ DEBUG - php: factorial of 17 is 355687428096000 source=user stream=stdout
... (truncated)
```

If you don't see these logs, make sure you have selected one of the "triggers getting started" functions in the `Function name` dropdown.

### Costs

Configuring and managing triggers is free, you only pay for the execution of your functions. For more information, please consult the [Scaleway Serverless pricing](https://www.scaleway.com/en/pricing/?tags=serverless). You will also be billed for the SQS queue usage when sending messages to it.

## Cleanup

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

```console
terraform destroy
```
13 changes: 4 additions & 9 deletions functions/triggers-getting-started/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ resource "scaleway_function" "main" {
runtime = each.value.runtime
handler = each.value.handler

// As of 2023/09/04, only public functions can be triggered by a message queue
privacy = "public"

zip_file = data.archive_file.function[each.key].output_path
Expand All @@ -60,16 +59,13 @@ resource "scaleway_function" "main" {
min_scale = 0
}

resource "scaleway_mnq_queue" "main" {
resource "scaleway_mnq_sqs_queue" "main" {
for_each = local.functions

namespace_id = scaleway_mnq_namespace.main.id
name = "factorial-requests-${each.key}"

sqs {
access_key = scaleway_mnq_credential.main.sqs_sns_credentials.0.access_key
secret_key = scaleway_mnq_credential.main.sqs_sns_credentials.0.secret_key
}
access_key = scaleway_mnq_sqs_credentials.main.access_key
secret_key = scaleway_mnq_sqs_credentials.main.secret_key
}

resource "scaleway_function_trigger" "main" {
Expand All @@ -78,7 +74,6 @@ resource "scaleway_function_trigger" "main" {
function_id = scaleway_function.main[each.key].id
name = "on-factorial-request"
sqs {
namespace_id = scaleway_mnq_namespace.main.id
queue = scaleway_mnq_queue.main[each.key].name
queue = scaleway_mnq_sqs_queue.main[each.key].name
}
}
18 changes: 5 additions & 13 deletions functions/triggers-getting-started/messaging.tf
Original file line number Diff line number Diff line change
@@ -1,16 +1,8 @@
resource "scaleway_mnq_namespace" "main" {
name = "triggers-getting-started"
protocol = "sqs_sns"
}

resource "scaleway_mnq_credential" "main" {
resource "scaleway_mnq_sqs_credentials" "main" {
name = "triggers-getting-started"
namespace_id = scaleway_mnq_namespace.main.id
sqs_sns_credentials {
permissions {
can_publish = true
can_receive = true
can_manage = true
}
permissions {
can_publish = true
can_receive = true
can_manage = true
}
}
5 changes: 3 additions & 2 deletions functions/triggers-getting-started/outputs.tf
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
output "sqs_access_key" {
value = scaleway_mnq_credential.main.sqs_sns_credentials.0.access_key
value = scaleway_mnq_sqs_credentials.main.access_key
sensitive = true
}

output "sqs_secret_key" {
value = scaleway_mnq_credential.main.sqs_sns_credentials.0.secret_key
value = scaleway_mnq_sqs_credentials.main.secret_key
sensitive = true
}
2 changes: 1 addition & 1 deletion functions/triggers-getting-started/provider.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ terraform {
required_providers {
scaleway = {
source = "scaleway/scaleway"
version = ">= 2.12"
version = ">= 2.31"
}
archive = {
source = "hashicorp/archive"
Expand Down
Loading