sless-primer/pg-query/main.tf

77 lines
2.2 KiB
HCL
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 2026-03-09
# main.tf — e2e тест: создать serverless функцию, которая читает из PostgreSQL.
#
# Структура:
# code/ — исходники (handler.py, requirements.txt)
# dist/ — zip-архив (генерируется archive_file datasource)
#
# Использование:
# terraform init && terraform apply
# После apply (ждёт ~2 мин пока kaniko соберёт образ):
# curl -s -X POST <url_из_trigger> -d '{}'
#
# Оператор запущен в кластере: https://sless-api.kube5s.ru
# Функция подключается к postgres.sless.svc.cluster.local:5432 изнутри кластера.
# PG_DSN задаётся как env_var ресурса sless_function.
terraform {
required_providers {
sless = {
source = "terra.k8c.ru/naeel/sless"
version = "~> 0.1.7"
}
archive = {
source = "hashicorp/archive"
version = "~> 2.0"
}
}
}
provider "sless" {
endpoint = "https://sless-api.kube5s.ru"
token = "dev-token-change-me"
}
data "archive_file" "handler_pg_query" {
type = "zip"
source_dir = "${path.module}/code"
output_path = "${path.module}/dist/handler.zip"
}
resource "sless_function" "pg_query" {
namespace = "default"
name = "pg-query"
runtime = "python3.11"
entrypoint = "handler.handle"
memory_mb = 128
timeout_sec = 30
# DSN для подключения к postgres внутри кластера
# Хост: postgres.sless.svc.cluster.local (Service в namespace sless)
env_vars = {
PG_DSN = "postgres://sless:sless-pg-password@postgres.sless.svc.cluster.local:5432/sless?sslmode=disable"
}
code_path = data.archive_file.handler_pg_query.output_path
code_hash = filesha256("${path.module}/code/handler.py")
}
resource "sless_trigger" "pg_query_http" {
namespace = "default"
name = "pg-query-http"
type = "http"
function = sless_function.pg_query.name
}
output "function_phase" {
value = sless_function.pg_query.phase
}
output "function_image" {
value = sless_function.pg_query.image_ref
}
output "trigger_url" {
value = sless_trigger.pg_query_http.url
}