70 lines
2.2 KiB
HCL
70 lines
2.2 KiB
HCL
# 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.
|
|
|
|
terraform {
|
|
required_providers {
|
|
sless = {
|
|
source = "terra.k8c.ru/naeel/sless"
|
|
version = "~> 0.1.1"
|
|
}
|
|
archive = {
|
|
source = "hashicorp/archive"
|
|
version = "~> 2.0"
|
|
}
|
|
}
|
|
}
|
|
|
|
provider "sless" {
|
|
endpoint = "https://sless-api.kube5s.ru"
|
|
token = "dev-token-change-me"
|
|
}
|
|
|
|
# Автоматически собирает handler.zip из handler.js при каждом изменении файла
|
|
data "archive_file" "handler" {
|
|
type = "zip"
|
|
source_file = "${path.module}/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
|
|
}
|