examples/hello-node: two different functions (http greeting, job sum)
This commit is contained in:
parent
a56de870ce
commit
8e7703b286
7
hello-node/code/handler-http.js
Normal file
7
hello-node/code/handler-http.js
Normal file
@ -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}!` };
|
||||||
|
};
|
||||||
9
hello-node/code/handler-job.js
Normal file
9
hello-node/code/handler-job.js
Normal file
@ -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 };
|
||||||
|
};
|
||||||
@ -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) епта !` };
|
|
||||||
};
|
|
||||||
@ -1,21 +1,28 @@
|
|||||||
# 2026-03-08
|
# 2026-03-08
|
||||||
# http.tf — вариант с HTTP-триггером: постоянный эндпоинт, обрабатывает запросы.
|
# http.tf — HTTP-функция: принимает запросы, возвращает приветствие.
|
||||||
|
# Код: code/handler-http.js
|
||||||
#
|
#
|
||||||
# Использование:
|
# Использование:
|
||||||
# terraform apply
|
# terraform apply
|
||||||
# curl -s -X POST $(terraform output -raw trigger_url) \
|
# curl -s -X POST $(terraform output -raw trigger_url) \
|
||||||
# -H 'Content-Type: application/json' -d '{"name":"Naeel"}'
|
# -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" {
|
resource "sless_function" "hello_http" {
|
||||||
namespace = "default"
|
namespace = "default"
|
||||||
name = "hello-http"
|
name = "hello-http"
|
||||||
runtime = "nodejs20"
|
runtime = "nodejs20"
|
||||||
entrypoint = "handler.handle"
|
entrypoint = "handler-http.handle"
|
||||||
memory_mb = 128
|
memory_mb = 128
|
||||||
timeout_sec = 30
|
timeout_sec = 30
|
||||||
|
|
||||||
code_path = data.archive_file.handler.output_path
|
code_path = data.archive_file.handler_http.output_path
|
||||||
code_hash = data.archive_file.handler.output_md5
|
code_hash = data.archive_file.handler_http.output_md5
|
||||||
}
|
}
|
||||||
|
|
||||||
resource "sless_trigger" "hello_http" {
|
resource "sless_trigger" "hello_http" {
|
||||||
|
|||||||
@ -1,5 +1,6 @@
|
|||||||
# 2026-03-08
|
# 2026-03-08
|
||||||
# job.tf — вариант с одноразовым запуском: terraform apply выполняет функцию и ждёт результата.
|
# job.tf — одноразовая функция: суммирует числа из переданного массива.
|
||||||
|
# Код: code/handler-job.js
|
||||||
#
|
#
|
||||||
# Использование:
|
# Использование:
|
||||||
# terraform apply
|
# terraform apply
|
||||||
@ -8,16 +9,22 @@
|
|||||||
#
|
#
|
||||||
# Повторный запуск — изменить event_json и снова terraform apply.
|
# Повторный запуск — изменить 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" {
|
resource "sless_function" "hello_job" {
|
||||||
namespace = "default"
|
namespace = "default"
|
||||||
name = "hello-job"
|
name = "hello-job"
|
||||||
runtime = "nodejs20"
|
runtime = "nodejs20"
|
||||||
entrypoint = "handler.handle"
|
entrypoint = "handler-job.handle"
|
||||||
memory_mb = 128
|
memory_mb = 128
|
||||||
timeout_sec = 30
|
timeout_sec = 30
|
||||||
|
|
||||||
code_path = data.archive_file.handler.output_path
|
code_path = data.archive_file.handler_job.output_path
|
||||||
code_hash = data.archive_file.handler.output_md5
|
code_hash = data.archive_file.handler_job.output_md5
|
||||||
}
|
}
|
||||||
|
|
||||||
# Одноразовый запуск. Все поля immutable — изменение любого пересоздаёт джоб.
|
# Одноразовый запуск. Все поля immutable — изменение любого пересоздаёт джоб.
|
||||||
@ -25,7 +32,7 @@ resource "sless_job" "hello_run" {
|
|||||||
namespace = "default"
|
namespace = "default"
|
||||||
name = "hello-run"
|
name = "hello-run"
|
||||||
function = sless_function.hello_job.name
|
function = sless_function.hello_job.name
|
||||||
event_json = jsonencode({ name = "Naeel" })
|
event_json = jsonencode({ numbers = [1, 2, 3, 4, 5] })
|
||||||
wait_timeout_sec = 120
|
wait_timeout_sec = 120
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -1,8 +1,8 @@
|
|||||||
# 2026-03-08
|
# 2026-03-08
|
||||||
# main.tf — общие настройки: провайдеры и сборка zip из code/handler.js.
|
# main.tf — провайдеры.
|
||||||
# Варианты запуска:
|
# Функции и их код определены в отдельных файлах:
|
||||||
# http.tf — HTTP-триггер (постоянный эндпоинт)
|
# http.tf — HTTP-триггер (code/handler-http.js)
|
||||||
# job.tf — одноразовый запуск (sless_job)
|
# job.tf — одноразовый запуск (code/handler-job.js)
|
||||||
|
|
||||||
terraform {
|
terraform {
|
||||||
required_providers {
|
required_providers {
|
||||||
@ -22,10 +22,3 @@ provider "sless" {
|
|||||||
token = "dev-token-change-me"
|
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"
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user