Serverless dengan AWS Lambda
AWS Lambda menjalankan kode tanpa mengelola server. Bayar hanya saat fungsi berjalan — tidak ada idle cost.
Anatomy of a Lambda Function
// handler.mjs
export const handler = async (event, context) => {
const { httpMethod, body, pathParameters } = event;
if (httpMethod === "POST") {
const data = JSON.parse(body);
// proses data...
return {
statusCode: 200,
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ success: true }),
};
}
return { statusCode: 405, body: "Method Not Allowed" };
};
Triggers
- API Gateway — HTTP endpoint → Lambda
- S3 Event — File upload → Lambda (resize gambar, generate thumbnail)
- SQS/SNS — Message queue → Lambda
- CloudWatch Events — Cron schedule → Lambda
- DynamoDB Streams — Data change → Lambda
Cold Start
Lambda instance yang idle dihapus. Request pertama setelah idle = cold start (inisialisasi runtime). Mitigasi:
- Provisioned Concurrency — Keep instances warm (biaya tambahan)
- Minimize package size — Bundle kecil = init cepat
- Use lightweight runtime — Node.js/Python lebih cepat dari Java/.NET
Limits
- Timeout: max 15 menit
- Memory: 128 MB – 10 GB
- Package size: 50 MB (zip) / 250 MB (unzipped)
- Concurrent executions: 1000 default (bisa request increase)