diff --git a/hello-node/code/handler-http.js b/hello-node/code/handler-http.js new file mode 100644 index 0000000..7a15d05 --- /dev/null +++ b/hello-node/code/handler-http.js @@ -0,0 +1,7 @@ +// 2026-03-08 +// handler-http.js — HTTP-функция: возвращает приветствие. +// Используется с sless_trigger (постоянный эндпоинт). +exports.handle = async (event) => { + const name = event.name || 'World'; + return { message: `Hello, ${name}!` }; +}; diff --git a/hello-node/code/handler-job.js b/hello-node/code/handler-job.js new file mode 100644 index 0000000..3342114 --- /dev/null +++ b/hello-node/code/handler-job.js @@ -0,0 +1,9 @@ +// 2026-03-08 +// handler-job.js — batch-функция: суммирует числа из переданного массива. +// Используется с sless_job (одноразовый запуск). +// event.numbers — массив чисел, например [1, 2, 3, 4, 5] +exports.handle = async (event) => { + const numbers = event.numbers || []; + const sum = numbers.reduce((acc, n) => acc + n, 0); + return { input: numbers, sum, count: numbers.length }; +}; diff --git a/hello-node/code/handler.js b/hello-node/code/handler.js deleted file mode 100644 index 6e3a76a..0000000 --- a/hello-node/code/handler.js +++ /dev/null @@ -1,6 +0,0 @@ -// handler.js — пример serverless функции на Node.js 20 -// Возвращает приветствие с именем из event или "World" по умолчанию -exports.handle = async (event) => { - const name = event.name || 'World'; - return { message: `Hello, ${name}! (nodejs20) епта !` }; -}; diff --git a/hello-node/http.tf b/hello-node/http.tf index 74e1cdb..ee255cd 100644 --- a/hello-node/http.tf +++ b/hello-node/http.tf @@ -1,21 +1,28 @@ # 2026-03-08 -# http.tf — вариант с HTTP-триггером: постоянный эндпоинт, обрабатывает запросы. +# http.tf — HTTP-функция: принимает запросы, возвращает приветствие. +# Код: code/handler-http.js # # Использование: # terraform apply # curl -s -X POST $(terraform output -raw trigger_url) \ # -H 'Content-Type: application/json' -d '{"name":"Naeel"}' +data "archive_file" "handler_http" { + type = "zip" + source_file = "${path.module}/code/handler-http.js" + output_path = "${path.module}/handler-http.zip" +} + resource "sless_function" "hello_http" { namespace = "default" name = "hello-http" runtime = "nodejs20" - entrypoint = "handler.handle" + entrypoint = "handler-http.handle" memory_mb = 128 timeout_sec = 30 - code_path = data.archive_file.handler.output_path - code_hash = data.archive_file.handler.output_md5 + code_path = data.archive_file.handler_http.output_path + code_hash = data.archive_file.handler_http.output_md5 } resource "sless_trigger" "hello_http" { diff --git a/hello-node/job.tf b/hello-node/job.tf index db8c47c..23097dc 100644 --- a/hello-node/job.tf +++ b/hello-node/job.tf @@ -1,5 +1,6 @@ # 2026-03-08 -# job.tf — вариант с одноразовым запуском: terraform apply выполняет функцию и ждёт результата. +# job.tf — одноразовая функция: суммирует числа из переданного массива. +# Код: code/handler-job.js # # Использование: # terraform apply @@ -8,16 +9,22 @@ # # Повторный запуск — изменить event_json и снова terraform apply. +data "archive_file" "handler_job" { + type = "zip" + source_file = "${path.module}/code/handler-job.js" + output_path = "${path.module}/handler-job.zip" +} + resource "sless_function" "hello_job" { namespace = "default" name = "hello-job" runtime = "nodejs20" - entrypoint = "handler.handle" + entrypoint = "handler-job.handle" memory_mb = 128 timeout_sec = 30 - code_path = data.archive_file.handler.output_path - code_hash = data.archive_file.handler.output_md5 + code_path = data.archive_file.handler_job.output_path + code_hash = data.archive_file.handler_job.output_md5 } # Одноразовый запуск. Все поля immutable — изменение любого пересоздаёт джоб. @@ -25,7 +32,7 @@ resource "sless_job" "hello_run" { namespace = "default" name = "hello-run" function = sless_function.hello_job.name - event_json = jsonencode({ name = "Naeel" }) + event_json = jsonencode({ numbers = [1, 2, 3, 4, 5] }) wait_timeout_sec = 120 } diff --git a/hello-node/main.tf b/hello-node/main.tf index caf9e30..0bd5b5c 100644 --- a/hello-node/main.tf +++ b/hello-node/main.tf @@ -1,8 +1,8 @@ # 2026-03-08 -# main.tf — общие настройки: провайдеры и сборка zip из code/handler.js. -# Варианты запуска: -# http.tf — HTTP-триггер (постоянный эндпоинт) -# job.tf — одноразовый запуск (sless_job) +# main.tf — провайдеры. +# Функции и их код определены в отдельных файлах: +# http.tf — HTTP-триггер (code/handler-http.js) +# job.tf — одноразовый запуск (code/handler-job.js) terraform { required_providers { @@ -22,10 +22,3 @@ provider "sless" { token = "dev-token-change-me" } -# Автоматически собирает zip из code/handler.js при каждом изменении файла -data "archive_file" "handler" { - type = "zip" - source_file = "${path.module}/code/handler.js" - output_path = "${path.module}/handler.zip" -} -