พื้นฐาน Event-driven Architecture

Serverless Applications with AWS Lambda

Claudio Canales

Senior DevOps Engineer

ทบทวน: mental model ของ Lambda

  • Event source: สิ่งที่ทริกเกอร์ Lambda
  • Event payload: JSON ที่อธิบายสิ่งที่เกิดขึ้น
  • Handler: โค้ดที่ประมวลผล event
  • Outcome: response หรือ side effect
  • Logs: ใช้ดีบักปัญหา

ขั้นตอนการทำงานของ Lambda

Serverless Applications with AWS Lambda

Event คือข้อความ

  • Event คือสิ่งที่เกิดขึ้น
  • มักเป็นข้อความ JSON ที่มี metadata
  • Handler ดึงข้อมูลจาก field และดำเนินการ

การเปรียบเทียบ event กับกริ่งประตู

Serverless Applications with AWS Lambda

Event sources ที่พบบ่อยใน AWS

  • Amazon S3 ทริกเกอร์ event เมื่ออัปโหลดไฟล์
  • Amazon SQS เป็น queue แบบ managed สำหรับส่งข้อความ
  • DynamoDB Streams สร้าง change log
  • แต่ละแหล่งสร้าง JSON payload ที่แตกต่างกัน

Event sources: S3 vs SQS vs Streams

Serverless Applications with AWS Lambda

Synchronous vs Asynchronous invocation

  • Synchronous: ผู้เรียกรอผลลัพธ์
  • Asynchronous: ผู้เรียกได้รับการยืนยัน แล้ว event จะถูกประมวลผลในภายหลัง

การเปรียบเทียบ Sync กับ Async

Serverless Applications with AWS Lambda

พื้นฐาน Event-driven Architecture

  • Event มาในรูป JSON จาก AWS services
  • โหมดการเรียกใช้ส่งผลต่อตำแหน่งที่ error ปรากฏ
  • Payload แตกต่างกันไป จึงควร parse และตรวจสอบอย่างระมัดระวัง
  • ควรคาดว่าจะมีการ retry และข้อมูลซ้ำ

pipeline สรุปประเด็นสำคัญ

Serverless Applications with AWS Lambda

Payload คือสัญญา

  • Payload คือสัญญา: ผู้ส่งเป็นคนกำหนดโครงสร้าง
  • Handler ต้องรู้ว่า field ที่ต้องการอยู่ที่ไหน
  • ดึงข้อมูลที่ต้องการ ตรวจสอบ ประมวลผล แล้ว log

ขั้นตอน payload contract และการ parse

Serverless Applications with AWS Lambda

ตัวอย่าง payload: SQS message event

SQS event payload แบบมีคำอธิบาย

  • Records คือ batch
  • body เป็น string
  • messageId ใช้ trace และกรองข้อมูลซ้ำ
Serverless Applications with AWS Lambda

Walkthrough: วนลูปผ่าน Records

def lambda_handler(event, context):
    records = event.get("Records", [])
    for record in records:
        body = record.get("body", "")
        print("BODY:", body)
    return {"statusCode": 200}
  • อ่าน Records พร้อมค่า default ที่ปลอดภัย
  • วนลูปแต่ละ record และดึง body
  • Log สิ่งที่สำคัญ แล้ว return

วนลูปผ่าน Records

Serverless Applications with AWS Lambda

Walkthrough: parse JSON body อย่างปลอดภัย

import json
def lambda_handler(event, context):
    records = event.get("Records") or [{}]
    body = records[0].get("body", "{}")
    payload = json.loads(body)
    oid = payload.get("order_id")
    return json.dumps({"order_id": oid})
  • body เป็น string ให้ parse ด้วย json.loads
  • อ่านพร้อมค่า default ที่ปลอดภัย เช่น "{}"
  • ดึง order_id แล้ว return JSON ด้วย json.dumps

ขั้นตอนการ parse body

Serverless Applications with AWS Lambda

Checklist สำหรับการ parse แบบป้องกันข้อผิดพลาด

Checklist สำหรับการ parse แบบป้องกันข้อผิดพลาด

  • Log รูปแบบ event ครั้งแรกเมื่อเริ่มเรียนรู้ event source ใหม่
  • ใช้ .get() และค่า default สำหรับ field ที่ไม่จำเป็น
  • ตรวจสอบ field ที่จำเป็นและคืนข้อผิดพลาดที่ชัดเจน
  • รองรับ batch ว่างและ key ที่หายไป
Serverless Applications with AWS Lambda

ออกแบบให้รองรับการ retry และข้อมูลซ้ำ

  • Event sources หลายแห่งใช้การส่งแบบ at-least-once
  • ข้อมูลซ้ำอาจเกิดขึ้นได้
  • ออกแบบ handler แบบ idempotent เพื่อให้ประมวลผลซ้ำได้อย่างถูกต้อง

การ retry และข้อมูลซ้ำ

Serverless Applications with AWS Lambda

Idempotency ในทางปฏิบัติ

  • เลือก key ที่ไม่ซ้ำกัน เช่น messageId หรือ order_id
  • เก็บแต่ละ key ที่ประมวลผลแล้วไว้ใน DynamoDB
  • ตรวจสอบว่า key นั้นมีอยู่แล้วหรือไม่ ถ้ามีให้ข้ามไป

ขั้นตอนการตัดสินใจด้าน idempotency

Serverless Applications with AWS Lambda

ประเด็นสำคัญ

  • Event มาในรูป JSON จาก AWS services
  • โหมดการเรียกใช้ส่งผลต่อตำแหน่งที่ error ปรากฏ
  • Payload แตกต่างกันไป จึงควร parse และตรวจสอบอย่างระมัดระวัง
  • ควรคาดว่าจะมีการ retry และข้อมูลซ้ำ

pipeline สรุปประเด็นสำคัญ

Serverless Applications with AWS Lambda

มาฝึกกันเถอะ!

Serverless Applications with AWS Lambda

Preparing Video For Download...