เกี่ยวกับ
Function คือ handler หนึ่งตัว คุณเขียนเฉพาะ handler ระบบเพิ่ม runtime ขนาดเล็กที่รับ HTTP ให้ Build และรันแบบ Container ที่หยุดเองเมื่อไม่มีงาน โค้ดชุดเดียวกันแสดงในตัวแก้ไขของ Console จึงแก้ได้ทั้งใน Console และในเครื่อง: th functions pull ดึงโค้ดที่ Console บันทึกไว้ และ th functions deploy ส่งโฟลเดอร์ของคุณขึ้นไป
| Runtime | ไฟล์หลัก | Handler |
|---|---|---|
python314, python313 (แนะนำ), python312, python311 | main.py (+ requirements.txt) | def main(request) คืนค่าเป็น str, dict (JSON) หรือ (body, status, headers) |
nodejs24 (แนะนำ), nodejs22 | index.ts หรือ index.js (+ package.json) | export async function handler(req: Request) คืนค่าเป็น Response, string หรือ object |
go126 (แนะนำ), go125 | *.go + go.mod | func Handle(w http.ResponseWriter, r *http.Request) |
Function หนึ่งมีไฟล์ข้อความได้ไม่เกิน 100 ไฟล์ ไฟล์ละไม่เกิน 512 KiB รวมไม่เกิน 2 MiB ไฟล์ใน .thignore (หรือ .dockerignore / .gitignore), .git, node_modules, .venv, __pycache__ และ dist จะไม่ถูกส่ง และไม่ใช้ Dockerfile ค่า Build คิดเหมือน Container ถ้าต้องการให้ทำงานตามเวลาหรือทุกข้อความใน Queue ให้เพิ่ม Trigger ด้วย th triggers
คู่มือการใช้งาน
Deploy Function จากโฟลเดอร์
mkdir hello && cd hello
printf 'def main(request):\n return {"message": "Hello, " + request.args.get("name", "World")}\n' > main.py
th functions deploy hello --env GREETING=hi
th functions call hello --path "/?name=Thai"Deploy ครั้งแรกจะสร้าง Function ให้ Runtime ดูจากไฟล์ (หรือกำหนดด้วย --runtime) th รอจน Build เสร็จแล้วแสดง URL ใช้ --async ถ้าไม่ต้องการรอ
ย้อนกลับไปใช้เวอร์ชันก่อนหน้า
th functions versions list hello
th functions versions activate hello 12
th functions pull hello --version 12 --dir hello-v12ทุกครั้งที่ Deploy จะเป็นเวอร์ชันใหม่ เก็บไว้ล่าสุด 30 เวอร์ชัน การใช้เวอร์ชันเก่าจะใช้การตั้งค่าและตัวแปรปัจจุบัน ถ้า image ของเวอร์ชันนั้นถูกล้างไปแล้ว ระบบจะ Build โค้ดชุดนั้นใหม่เป็นเวอร์ชันล่าสุด ใส่ --runtime ตอน Deploy เพื่อเปลี่ยนเวอร์ชันของภาษา เช่น --runtime python314
ให้ Function ทำงานเฉพาะเมื่อ Trigger เรียก
th functions deploy nightly-report --ingress triggers
th triggers create nightly --service nightly-report --schedule "0 3 * * *"เมื่อใช้ --ingress triggers Function จะไม่มี Endpoint เลย ไม่มี URL สาธารณะ ไม่มี Custom domain และไม่มีที่อยู่ภายใน VPC ของ Project มีเพียง Trigger แบบ Cron และ Queue ของมันที่เรียกได้ และสั่งรันทันทีได้ด้วย th triggers run ใช้ --ingress all เพื่อเปิดกลับ
แก้ใน Console แล้วทำต่อในเครื่อง
th functions pull hello --dir hello
# แก้ไฟล์
th functions deploy hello --source hellopull จะไม่เขียนทับไฟล์ในเครื่องที่ต่างจากใน Console เว้นแต่ใส่ --force
คำสั่ง
| คำสั่ง | คำอธิบาย |
|---|---|
th functions deploy | Uploads the files of DIR (default .) as the function's code, builds them and serves the result. |
th functions list | Lists the functions of the active Project. |
th functions describe | Shows a function's runtime, deploy state, settings and files. |
th functions pull | Writes the function's saved code (what the Console editor shows), or the code of one version, into DIR (default ./NAME). |
th functions call | Sends one request to the function and prints the answer. |
th functions logs | Prints the latest log lines of a function (what the handler printed). |
th functions versions list | Lists a function's versions, newest first. |
th functions versions activate | Serves an earlier version again; today's settings and variables stay. |
th functions runtimes | Lists the runtimes a function can use and the handler each expects. |
th functions delete | Deletes a function with its code, triggers and revisions. |
th functions deploy
Uploads the files of DIR (default .) as the function's code, builds them and serves the result.
ไวยากรณ์
th functions deploy NAME [--source DIR] [flags]A new function is created first; its runtime comes from --runtime or from the files (go.mod: Go, package.json / index.ts: Node.js, main.py / requirements.txt: Python).
Files listed in .thignore (or .dockerignore / .gitignore), .git, node_modules, .venv, __pycache__ and dist are left out. A Dockerfile is ignored: functions always build with the platform's runtime.
--runtime takes a runtime (python313, nodejs24, go126) or a language (python, nodejs, go: its recommended version). An existing function may move to another version of its language.
Every deploy is kept as a version (th functions versions list); --message describes it. --ingress sets who reaches the function: all (the internet, the default), internal (other services in its project) or triggers (no endpoint at all; only its Cron and Queue triggers run it). --env, --set-env-vars and --secret-env replace every variable of the function; without them the current variables stay. Settings given for an existing function (--cpu, --memory, --min-instances, ...) are applied before its code is built.
ตัวอย่าง
th functions deploy helloth functions deploy hello --runtime python314 --message "Python 3.14"th functions deploy orders --source ./orders --memory 512Mi --max-instances 10th functions deploy nightly-report --ingress triggersอาร์กิวเมนต์ตามตำแหน่ง
| ชื่อ | คำอธิบาย |
|---|---|
NAMEจำเป็น | ชื่อ Function: ตัวพิมพ์เล็ก ตัวเลข และ - ขึ้นต้นด้วยตัวอักษร ใช้เป็น URL slug ด้วย |
พารามิเตอร์
| ชื่อ | ชนิด | ค่าเริ่มต้น | คำอธิบาย |
|---|---|---|---|
--source | ข้อความ | . | Directory with the function's files |
--runtime | ข้อความ | Runtime (e.g. python313, nodejs24, go126) or language (python, nodejs, go) | |
--entry-point | ข้อความ | the runtime's, e.g. main, handler, Handle | Handler name |
--cpu | ข้อความ | vCPU, e.g. 0.25, 0.5, 1 (a size of the Container menu) | |
--memory | ข้อความ | Memory, e.g. 256Mi, 512Mi, 1Gi | |
--compute-class | ข้อความ | Compute class of a new function | |
--env | รายการ | Environment variable KEY=VALUE (repeatable) | |
--set-env-vars | รายการ | Environment variables KEY=VALUE,KEY2=VALUE2 | |
--secret-env | รายการ | Secret environment variable KEY=VALUE (repeatable) | |
--min-instances | ตัวเลข | Minimum instances (0 stops it when idle) | |
--max-instances | ตัวเลข | Maximum instances | |
--timeout | ข้อความ | Request timeout, e.g. 60s or 5m | |
--concurrency | ตัวเลข | Requests at once per instance (1-1000) | |
--cpu-always-allocated | แฟล็ก | Keep CPU between requests | |
--allow-unauthenticated | แฟล็ก | for a new function; --no-allow-unauthenticated needs an invoker token | Allow public calls |
--ingress | ข้อความ | Who reaches the function: all, internal or triggers (no endpoint, triggers only) | |
--message | ข้อความ | Describe this version | |
--async | แฟล็ก | Return once the build is queued |
พารามิเตอร์ส่วนกลาง
คำสั่งนี้รับพารามิเตอร์ส่วนกลางด้วย: --project, --region, --endpoint, --format, --quiet
th functions list
Lists the functions of the active Project.
ไวยากรณ์
th functions listพารามิเตอร์ส่วนกลาง
คำสั่งนี้รับพารามิเตอร์ส่วนกลางด้วย: --project, --region, --endpoint, --format, --quiet
th functions describe
Shows a function's runtime, deploy state, settings and files.
ไวยากรณ์
th functions describe NAMEอาร์กิวเมนต์ตามตำแหน่ง
| ชื่อ | คำอธิบาย |
|---|---|
NAMEจำเป็น | ชื่อ Function: ตัวพิมพ์เล็ก ตัวเลข และ - ขึ้นต้นด้วยตัวอักษร ใช้เป็น URL slug ด้วย |
พารามิเตอร์ส่วนกลาง
คำสั่งนี้รับพารามิเตอร์ส่วนกลางด้วย: --project, --region, --endpoint, --format, --quiet
th functions pull
Writes the function's saved code (what the Console editor shows), or the code of one version, into DIR (default ./NAME).
ไวยากรณ์
th functions pull NAME [--dir DIR] [--version N] [--force]An existing file with other content is kept unless --force.
อาร์กิวเมนต์ตามตำแหน่ง
| ชื่อ | คำอธิบาย |
|---|---|
NAMEจำเป็น | ชื่อ Function: ตัวพิมพ์เล็ก ตัวเลข และ - ขึ้นต้นด้วยตัวอักษร ใช้เป็น URL slug ด้วย |
พารามิเตอร์
| ชื่อ | ชนิด | ค่าเริ่มต้น | คำอธิบาย |
|---|---|---|---|
--dir | ข้อความ | ./NAME | Directory to write into |
--version | ข้อความ | the saved code | A version from th functions versions list |
--force | แฟล็ก | Overwrite files that differ |
พารามิเตอร์ส่วนกลาง
คำสั่งนี้รับพารามิเตอร์ส่วนกลางด้วย: --project, --region, --endpoint, --format, --quiet
th functions call
Sends one request to the function and prints the answer.
ไวยากรณ์
th functions call NAME [--data BODY|@FILE] [--method POST] [--path /x] [--token TOKEN]With --data the method defaults to POST, otherwise GET. --token is the invoker token of a function that does not allow unauthenticated calls.
อาร์กิวเมนต์ตามตำแหน่ง
| ชื่อ | คำอธิบาย |
|---|---|
NAMEจำเป็น | ชื่อ Function: ตัวพิมพ์เล็ก ตัวเลข และ - ขึ้นต้นด้วยตัวอักษร ใช้เป็น URL slug ด้วย |
พารามิเตอร์
| ชื่อ | ชนิด | ค่าเริ่มต้น | คำอธิบาย |
|---|---|---|---|
--data | ข้อความ | Request body, or @FILE to read it from a file | |
--method | ข้อความ | HTTP method | |
--path | ข้อความ | / | Path to call |
--token | ข้อความ | Invoker token |
พารามิเตอร์ส่วนกลาง
คำสั่งนี้รับพารามิเตอร์ส่วนกลางด้วย: --project, --region, --endpoint, --format, --quiet
th functions logs
Prints the latest log lines of a function (what the handler printed).
ไวยากรณ์
th functions logs NAME [--lines N]อาร์กิวเมนต์ตามตำแหน่ง
| ชื่อ | คำอธิบาย |
|---|---|
NAMEจำเป็น | ชื่อ Function: ตัวพิมพ์เล็ก ตัวเลข และ - ขึ้นต้นด้วยตัวอักษร ใช้เป็น URL slug ด้วย |
พารามิเตอร์
| ชื่อ | ชนิด | ค่าเริ่มต้น | คำอธิบาย |
|---|---|---|---|
--lines | ตัวเลข | 100 | Number of lines (default 100) |
พารามิเตอร์ส่วนกลาง
คำสั่งนี้รับพารามิเตอร์ส่วนกลางด้วย: --project, --region, --endpoint, --format, --quiet
th functions versions list
Lists a function's versions, newest first.
ไวยากรณ์
th functions versions list NAMEEvery deploy is a version; the serving one is marked. The latest 30 are kept.
อาร์กิวเมนต์ตามตำแหน่ง
| ชื่อ | คำอธิบาย |
|---|---|
NAMEจำเป็น | ชื่อ Function: ตัวพิมพ์เล็ก ตัวเลข และ - ขึ้นต้นด้วยตัวอักษร ใช้เป็น URL slug ด้วย |
พารามิเตอร์ส่วนกลาง
คำสั่งนี้รับพารามิเตอร์ส่วนกลางด้วย: --project, --region, --endpoint, --format, --quiet
th functions versions activate
Serves an earlier version again; today's settings and variables stay.
ไวยากรณ์
th functions versions activate NAME VERSIONWhen the registry no longer holds its image, its code is built again as a new version and served once the build succeeds.
ตัวอย่าง
th functions versions activate hello 12th functions versions activate hello v12อาร์กิวเมนต์ตามตำแหน่ง
| ชื่อ | คำอธิบาย |
|---|---|
NAMEจำเป็น | ชื่อ Function: ตัวพิมพ์เล็ก ตัวเลข และ - ขึ้นต้นด้วยตัวอักษร ใช้เป็น URL slug ด้วย |
VERSIONจำเป็น |
พารามิเตอร์ส่วนกลาง
คำสั่งนี้รับพารามิเตอร์ส่วนกลางด้วย: --project, --region, --endpoint, --format, --quiet
th functions runtimes
Lists the runtimes a function can use and the handler each expects.
ไวยากรณ์
th functions runtimesพารามิเตอร์ส่วนกลาง
คำสั่งนี้รับพารามิเตอร์ส่วนกลางด้วย: --project, --region, --endpoint, --format, --quiet
th functions delete
Deletes a function with its code, triggers and revisions.
ไวยากรณ์
th functions delete NAME [--quiet]--quiet skips the prompt.
อาร์กิวเมนต์ตามตำแหน่ง
| ชื่อ | คำอธิบาย |
|---|---|
NAMEจำเป็น | ชื่อ Function: ตัวพิมพ์เล็ก ตัวเลข และ - ขึ้นต้นด้วยตัวอักษร ใช้เป็น URL slug ด้วย |
พารามิเตอร์ส่วนกลาง
คำสั่งนี้รับพารามิเตอร์ส่วนกลางด้วย: --project, --region, --endpoint, --format, --quiet