examples/hello-node: move handler to code/, split http.tf and job.tf
This commit is contained in:
parent
9d922de489
commit
a56de870ce
@ -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) епта !` };
|
||||
};
|
||||
30
hello-node/http.tf
Normal file
30
hello-node/http.tf
Normal file
@ -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
|
||||
}
|
||||
42
hello-node/job.tf
Normal file
42
hello-node/job.tf
Normal file
@ -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
|
||||
}
|
||||
@ -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
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user