🏠 Home / Hub

☁️ AWS Lesson 05 — Lambda (Serverless)

← Back to AWS Menu  |  🏠 Hub

1. Lambda ဆိုတာ

Lambda = Serverless Functions — Server မလိုဘဲ code run လုပ်တာ

✅ Server manage မလုပ်ရ — code upload ပြီး run
✅ Pay per invocation — ခေါ်မှ ပေး (idle time မပေး)
✅ Auto scale — တစ်ချိန်တည်း request ထောင်ကျော် handle နိုင်
✅ Free tier: 1 million requests/month ALWAYS FREE!
🔤 Languages: Python, Node.js, Java, Go, Ruby, .NET

2. First Lambda Function

# Lambda → Create function
# Name: hello-function
# Runtime: Python 3.12
# Click Create

# lambda_function.py (editor မှာ ရေး)
import json

def lambda_handler(event, context):
    name = event.get('name', 'World')

    return {
        'statusCode': 200,
        'body': json.dumps({
            'message': f'Hello, {name}!',
            'status': 'success'
        })
    }

# Test event
{
  "name": "Ko Min"
}
event = input data (JSON)  |  context = Lambda environment info
Deploy → Test → ရလဒ် ချက်ချင်း မြင်ရမယ်

3. API Gateway + Lambda = REST API

# API Gateway → Create API → HTTP API
# Add integration: Lambda function ရွေး
# Route: GET /hello
# Deploy → URL ရမယ်

# Lambda function (Node.js example)
exports.handler = async (event) => {
    const name = event.queryStringParameters?.name || 'World'

    return {
        statusCode: 200,
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({ message: `Hello, ${name}!` })
    }
}

# Call the API
# GET https://abc123.execute-api.ap-southeast-1.amazonaws.com/hello?name=Ko+Min
# Response: {"message": "Hello, Ko Min!"}

4. Lambda Triggers

Triggerဘာဖြစ်ရင် run မလဲ
API GatewayHTTP request ရောက်ရင်
S3 EventFile upload ဖြစ်ရင် (e.g. resize image)
EventBridge (Cron)Scheduled time (e.g. every day 9am)
DynamoDB StreamDatabase change ဖြစ်ရင်
SQSMessage queue ဝင်ရင်
SNSNotification ပေးပို့ရင်

5. Scheduled Lambda (Cron)

# EventBridge → Rules → Create rule
# Schedule: cron(0 9 * * ? *)  ← every day 9am UTC
# Target: Lambda function

# Cron examples
cron(0 9 * * ? *)      # Every day at 9am UTC
cron(0/5 * * * ? *)   # Every 5 minutes
cron(0 0 1 * ? *)     # First day of month

# Use case: Daily email report, cleanup old data, backup

← AWS 04  |  Next: AWS 06 → Deploy →

📌 Study Checklist