diff --git a/containers/terraform-triggers/README.md b/containers/terraform-triggers/README.md index 432af74..b285c6f 100644 --- a/containers/terraform-triggers/README.md +++ b/containers/terraform-triggers/README.md @@ -31,9 +31,9 @@ The deployment will do the following: 1. Create a Scaleway registry namespace 2. Build and deploy a container image with a Python/Go HTTP server 3. Deploy a public and private Serverless Container using the built image -4. Create Scaleway MnQ SQS queues -5. Configure triggers from these queues to each container -6. Print the endpoints of each queue and each container +4. Create Scaleway MnQ SQS queues and NATS subjects +5. Configure triggers from these SQS queues and NATS subjects to each container +6. Print the endpoints of each SQS queue, NATS subject and container To run the deployment: @@ -45,7 +45,7 @@ terraform apply ## Running -You can test your triggers by sending messages to the SQS queues configured with Terraform. +You can test your triggers by sending messages to the SQS queues and NATS subjects configured with Terraform. Below there is an example showing how to do this using a Python script in the [`tests/`](tests/) folder of this repo. @@ -58,6 +58,9 @@ export AWS_ACCESS_KEY_ID=$(terraform output -raw sqs_admin_access_key) export AWS_SECRET_ACCESS_KEY=$(terraform output -raw sqs_admin_secret_key) export PUBLIC_QUEUE_URL=$(terraform output -raw public_queue) export PRIVATE_QUEUE_URL=$(terraform output -raw private_queue) +export PUBLIC_SUBJECT=$(terraform output -raw public_subject) +export PRIVATE_SUBJECT=$(terraform output -raw private_subject) +export NATS_CREDS_FILE=$(terraform output -raw nats_creds_file) ``` You can then set up a Python environment in the `tests` directory: diff --git a/containers/terraform-triggers/mnq_nats.tf b/containers/terraform-triggers/mnq_nats.tf new file mode 100644 index 0000000..1896610 --- /dev/null +++ b/containers/terraform-triggers/mnq_nats.tf @@ -0,0 +1,13 @@ +resource "scaleway_mnq_nats_account" "main" { + name = "nats-account" +} + +resource "scaleway_mnq_nats_credentials" "main" { + account_id = scaleway_mnq_nats_account.main.id + name = "triggers-nats" +} + +resource "local_sensitive_file" "nats" { + filename = "nats-creds" + content = "${scaleway_mnq_nats_credentials.main.file}" +} diff --git a/containers/terraform-triggers/outputs.tf b/containers/terraform-triggers/outputs.tf index bc70add..2a7d901 100644 --- a/containers/terraform-triggers/outputs.tf +++ b/containers/terraform-triggers/outputs.tf @@ -24,6 +24,18 @@ output "sqs_admin_secret_key" { sensitive = true } +output "public_subject" { + value = local.public_nats_subject +} + +output "private_subject" { + value = local.private_nats_subject +} + +output "nats_creds_file" { + value = local_sensitive_file.nats.filename +} + output "cockpit_logs_public_container" { value = "https://${var.project_id}.dashboard.obs.fr-par.scw.cloud/d/scw-serverless-containers-logs/serverless-containers-logs?orgId=1&var-container_name=${split(".", scaleway_container.public.domain_name)[0]}&var-logs=Scaleway%20Logs" } diff --git a/containers/terraform-triggers/tests/requirements.txt b/containers/terraform-triggers/tests/requirements.txt index 5fc6386..6df42db 100644 --- a/containers/terraform-triggers/tests/requirements.txt +++ b/containers/terraform-triggers/tests/requirements.txt @@ -1 +1,2 @@ boto3==1.28.67 +nats-py==2.6.0 diff --git a/containers/terraform-triggers/tests/send_messages.py b/containers/terraform-triggers/tests/send_messages.py index 1b60994..461feee 100644 --- a/containers/terraform-triggers/tests/send_messages.py +++ b/containers/terraform-triggers/tests/send_messages.py @@ -1,12 +1,18 @@ import os import boto3 +import nats AWS_ACCESS_KEY_ID = os.environ["AWS_ACCESS_KEY_ID"] AWS_SECRET_ACCESS_KEY = os.environ["AWS_SECRET_ACCESS_KEY"] PUBLIC_QUEUE_URL = os.environ["PUBLIC_QUEUE_URL"] PRIVATE_QUEUE_URL = os.environ["PRIVATE_QUEUE_URL"] +NATS_ENDPOINT_URL = "https://nats.mnq.fr-par.scaleway.com" +PUBLIC_SUBJECT = os.environ["PUBLIC_SUBJECT"] +PRIVATE_SUBJECT = os.environ["PRIVATE_SUBJECT"] +NATS_CREDS_FILE = os.environ["NATS_CREDS_FILE"] + params = { "endpoint_url": "https://sqs.mnq.fr-par.scaleway.com", "aws_access_key_id": AWS_ACCESS_KEY_ID, @@ -17,13 +23,22 @@ client = boto3.client("sqs", **params) sqs = boto3.resource("sqs", **params) +nc = nats.connect(NATS_ENDPOINT_URL, user_credentials=NATS_CREDS_FILE) + + def main(): for queue_url in (PUBLIC_QUEUE_URL, PRIVATE_QUEUE_URL): queue = sqs.Queue(queue_url) queue_name = queue.attributes["QueueArn"].split(":")[-1] - print(f"Sending greetings message to {queue_name}...") - queue.send_message(MessageBody="Hello World!") + print(f"Sending greetings message to SQS {queue_name}...") + queue.send_message(MessageBody="Hello World SQS!") print("Greetings sent!") + for subject in (PUBLIC_SUBJECT, PRIVATE_SUBJECT): + print(f"Sending greetings message to NATS {subject}...") + nc.publish(subject, b"Hello World NATS!") + print("Greetings sent!") + + if __name__ == "__main__": main() diff --git a/containers/terraform-triggers/triggers.tf b/containers/terraform-triggers/triggers.tf index 8343da6..64c9d1d 100644 --- a/containers/terraform-triggers/triggers.tf +++ b/containers/terraform-triggers/triggers.tf @@ -1,15 +1,38 @@ -resource "scaleway_container_trigger" "public" { +resource "scaleway_container_trigger" "public_sqs" { container_id = scaleway_container.public.id - name = "public-trigger" + name = "public-sqs-trigger" sqs { queue = scaleway_mnq_sqs_queue.public.name } } -resource "scaleway_container_trigger" "private" { +resource "scaleway_container_trigger" "private_sqs" { container_id = scaleway_container.private.id - name = "private-trigger" + name = "private-sqs-trigger" sqs { queue = scaleway_mnq_sqs_queue.private.name } } + +locals { + public_nats_subject = "public-nats-subject" + private_nats_subject = "private-nats-subject" +} + +resource "scaleway_container_trigger" "public_nats" { + container_id = scaleway_container.public.id + name = "public-nats-trigger" + nats { + account_id = scaleway_mnq_nats_account.main.id + subject = local.public_nats_subject + } +} + +resource "scaleway_container_trigger" "private_nats" { + container_id = scaleway_container.private.id + name = "private-nats-trigger" + nats { + account_id = scaleway_mnq_nats_account.main.id + subject = local.private_nats_subject + } +}