From a56de870ce40aee1b8b404d2e80f6d430d69b61a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9CNaeel=E2=80=9D?= Date: Sun, 8 Mar 2026 09:14:34 +0400 Subject: [PATCH] examples/hello-node: move handler to code/, split http.tf and job.tf --- hello-node/{ => code}/handler.js | 2 +- hello-node/http.tf | 30 ++++++++++++++++++ hello-node/job.tf | 42 ++++++++++++++++++++++++++ hello-node/main.tf | 52 +++++--------------------------- 4 files changed, 80 insertions(+), 46 deletions(-) rename hello-node/{ => code}/handler.js (80%) create mode 100644 hello-node/http.tf create mode 100644 hello-node/job.tf diff --git a/hello-node/handler.js b/hello-node/code/handler.js similarity index 80% rename from hello-node/handler.js rename to hello-node/code/handler.js index 415c92f..6e3a76a 100644 --- a/hello-node/handler.js +++ b/hello-node/code/handler.js @@ -2,5 +2,5 @@ // Возвращает приветствие с именем из event или "World" по умолчанию exports.handle = async (event) => { const name = event.name || 'World'; - return { message: `Hello, ${name}! (nodejs20)` }; + return { message: `Hello, ${name}! (nodejs20) епта !` }; }; diff --git a/hello-node/http.tf b/hello-node/http.tf new file mode 100644 index 0000000..74e1cdb --- /dev/null +++ b/hello-node/http.tf @@ -0,0 +1,30 @@ +# 2026-03-08 +# http.tf — вариант с HTTP-триггером: постоянный эндпоинт, обрабатывает запросы. +# +# Использование: +# terraform apply +# curl -s -X POST $(terraform output -raw trigger_url) \ +# -H 'Content-Type: application/json' -d '{"name":"Naeel"}' + +resource "sless_function" "hello_http" { + namespace = "default" + name = "hello-http" + runtime = "nodejs20" + entrypoint = "handler.handle" + memory_mb = 128 + timeout_sec = 30 + + code_path = data.archive_file.handler.output_path + code_hash = data.archive_file.handler.output_md5 +} + +resource "sless_trigger" "hello_http" { + namespace = "default" + name = "hello-http-trigger" + type = "http" + function = sless_function.hello_http.name +} + +output "trigger_url" { + value = sless_trigger.hello_http.url +} diff --git a/hello-node/job.tf b/hello-node/job.tf new file mode 100644 index 0000000..db8c47c --- /dev/null +++ b/hello-node/job.tf @@ -0,0 +1,42 @@ +# 2026-03-08 +# job.tf — вариант с одноразовым запуском: terraform apply выполняет функцию и ждёт результата. +# +# Использование: +# terraform apply +# # apply блокируется до завершения джоба (~2 мин kaniko + выполнение) +# terraform output job_message +# +# Повторный запуск — изменить event_json и снова terraform apply. + +resource "sless_function" "hello_job" { + namespace = "default" + name = "hello-job" + runtime = "nodejs20" + entrypoint = "handler.handle" + memory_mb = 128 + timeout_sec = 30 + + code_path = data.archive_file.handler.output_path + code_hash = data.archive_file.handler.output_md5 +} + +# Одноразовый запуск. Все поля immutable — изменение любого пересоздаёт джоб. +resource "sless_job" "hello_run" { + namespace = "default" + name = "hello-run" + function = sless_function.hello_job.name + event_json = jsonencode({ name = "Naeel" }) + wait_timeout_sec = 120 +} + +output "job_phase" { + value = sless_job.hello_run.phase +} + +output "job_message" { + value = sless_job.hello_run.message +} + +output "job_completion_time" { + value = sless_job.hello_run.completion_time +} diff --git a/hello-node/main.tf b/hello-node/main.tf index 2abdde1..caf9e30 100644 --- a/hello-node/main.tf +++ b/hello-node/main.tf @@ -1,13 +1,8 @@ -# 2026-03-07 -# main.tf — пример одноразового запуска serverless функции (sless_job) на Node.js 20. -# -# Использование: -# 1. terraform init && terraform apply -# 2. apply блокируется: сначала ~2 мин kaniko собирает образ, затем ждёт завершения джоба -# 3. После apply в output — результат выполнения функции (phase, message, completion_time) -# -# Zip собирается автоматически из handler.js через hashicorp/archive. -# Пересборка + повторный запуск при изменении handler.js или event_json: terraform apply. +# 2026-03-08 +# main.tf — общие настройки: провайдеры и сборка zip из code/handler.js. +# Варианты запуска: +# http.tf — HTTP-триггер (постоянный эндпоинт) +# job.tf — одноразовый запуск (sless_job) terraform { required_providers { @@ -27,43 +22,10 @@ provider "sless" { token = "dev-token-change-me" } -# Автоматически собирает handler.zip из handler.js при каждом изменении файла +# Автоматически собирает zip из code/handler.js при каждом изменении файла data "archive_file" "handler" { type = "zip" - source_file = "${path.module}/handler.js" + source_file = "${path.module}/code/handler.js" output_path = "${path.module}/handler.zip" } -resource "sless_function" "hello_node" { - namespace = "default" - name = "hello-node" - runtime = "nodejs20" - entrypoint = "handler.handle" - memory_mb = 128 - timeout_sec = 30 - - code_path = data.archive_file.handler.output_path - code_hash = data.archive_file.handler.output_md5 -} - -# Одноразовый запуск функции. terraform apply ждёт завершения джоба. -# Чтобы перезапустить — изменить event_json (или любое поле) и снова apply. -resource "sless_job" "hello_run" { - namespace = "default" - name = "hello-run" - function = sless_function.hello_node.name - event_json = jsonencode({ name = "Naeel" }) - wait_timeout_sec = 120 -} - -output "job_phase" { - value = sless_job.hello_run.phase -} - -output "job_message" { - value = sless_job.hello_run.message -} - -output "job_completion_time" { - value = sless_job.hello_run.completion_time -}