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

feat(triggers): Add NATS trigger to container example #56

Closed
wants to merge 2 commits into from
Closed
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
11 changes: 7 additions & 4 deletions containers/terraform-triggers/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand All @@ -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.

Expand All @@ -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:
Expand Down
13 changes: 13 additions & 0 deletions containers/terraform-triggers/mnq_nats.tf
Original file line number Diff line number Diff line change
@@ -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}"
}
12 changes: 12 additions & 0 deletions containers/terraform-triggers/outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand Down
1 change: 1 addition & 0 deletions containers/terraform-triggers/tests/requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
boto3==1.28.67
nats-py==2.6.0
19 changes: 17 additions & 2 deletions containers/terraform-triggers/tests/send_messages.py
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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()
31 changes: 27 additions & 4 deletions containers/terraform-triggers/triggers.tf
Original file line number Diff line number Diff line change
@@ -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
}
}
Loading