Thuta Learning
ရှာဖွေရန်
ProjectsDevOpsbeginner

Project — Serverless REST API

စိတ်လျှော့ပါ။ ဒီခန်းကို စာအုပ်လိုမဟုတ်ဘဲ စကားပြောသလိုပဲ၊ နားလည်လွယ်အောင် ရှင်းပါမယ်။

ဒီခန်းပြီးရင် ဘာတတ်သွားမလဲ

  • Project — Serverless REST API ကို ကြောက်စရာမလိုအောင် နားလည်မယ်
  • ကိုယ်တိုင် AWS CLI/Console ကို run ကြည့်တတ်မယ်
  • Real project ထဲမှာ ဒီ concept ကို ချက်ချင်း အသုံးချတတ်မယ်

ခဏလေး ဒီလိုပဲ စဉ်းစားကြည့်

EC2-based architecture (Project 1) က server ကို run ထားရမှာမို့ idle ချိန်တောင် cost ဖြစ်ပါတယ် — Serverless architecture (Lambda + API Gateway + DynamoDB) ကတော့ request လာမှသာ run ပြီး, idle ချိန် cost လုံးဝ မရှိပါ (traffic နည်းတဲ့ side project/MVP အတွက် အထူး cost-effective ပါ)။ API Gateway က HTTP route ကို လက်ခံပြီး Lambda function ကို trigger, Lambda function က DynamoDB ကို IAM Role (access key မလို) နဲ့ ချိတ်ဆက်ပြီး data ကို read/write လုပ်ပါတယ်.

လက်တွေ့ scenario နဲ့ ချိတ်ကြည့်မယ်

`POST /todos` (todo item အသစ် create), `GET /todos/{id}` (todo item ဖတ်ခြင်း) endpoint ၂ ခုကို API Gateway ထဲမှာ define လုပ်ပြီး, Lambda function ၂ ခု (create-todo, get-todo) ကို ချိတ်ဆက်ပါ — Lambda တစ်ခုစီကို IAM Role (DynamoDB read/write permission ပါ) attach ထားပါ, DynamoDB table ကို `todoId` Partition Key နဲ့ create ထားပါ.

အတူတူ ကြည့်မယ်

python
# create_todo.py (Lambda function)
import json, boto3, uuid

dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table('Todos')

def lambda_handler(event, context):
    body = json.loads(event['body'])
    todo_id = str(uuid.uuid4())
    table.put_item(Item={'todoId': todo_id, 'title': body['title'], 'done': False})
    return {'statusCode': 201, 'body': json.dumps({'todoId': todo_id})}
You should see
$ curl -X POST https://abc123.execute-api.us-east-1.amazonaws.com/todos -d '{"title":"Learn AWS"}'
{"todoId": "a1b2c3d4-..."}

၅ မိနစ် စမ်းကြည့်

Todo app API (create + get endpoint, DynamoDB table) ကို ကိုယ်တိုင် တည်ဆောက်ကြည့်ပါ — `curl` ကနေ POST/GET request ပို့ပြီး end-to-end အလုပ်လုပ်ကြောင်း confirm လုပ်ကြည့်ပါ။

သတိလေးတစ်ချက်

Serverless architecture ကတော့ cost-effective ပါပေမယ့် 'ဘယ်တော့မှ cost မတက်ဘူး' လို့ မထင်ပါနှင့် — traffic တက်လာရင် (viral post) Lambda invocation count/DynamoDB read-write unit ကလည်း အလိုက်သင့် တက်လာနိုင်ပါတယ် — Cost Management chapter ရဲ့ Billing Alert ကို ဆက်လက် setup ထားပါ။

ဒီနေရာမှာ လူအများမှားတတ်တယ်

  • Lambda function ရဲ့ IAM Role ကို DynamoDB FullAccess (permission အကုန်) attach ခြင်း — table တစ်ခုတည်းအတွက် scoped permission ပဲ ပေးသင့်ပါတယ်
  • API Gateway route ကို authentication မထားဘဲ public ချထားခြင်း — production app အတွက် API Key ဒါမှမဟုတ် JWT authorizer လိုအပ်ပါတယ်

အခု ကိုယ်တိုင် စမ်းကြည့်

Todo app API (create + get endpoint, DynamoDB table) ကို ကိုယ်တိုင် တည်ဆောက်ကြည့်ပါ — `curl` ကနေ POST/GET request ပို့ပြီး end-to-end အလုပ်လုပ်ကြောင်း confirm လုပ်ကြည့်ပါ။

You'll know it worked when: $ curl -X POST https://abc123.execute-api.us-east-1.amazonaws.com/todos -d '{"title":"Learn AWS"}' {"todoId": "a1b2c3d4-..."}

Project — Serverless REST API | Thuta Learning