💻 Code Example
AWS Lambda (Node.js)
// handler.js
export const handler = async (event, context) => {
const { name } = JSON.parse(event.body);
// 비즈니스 로직
const greeting = `Hello, ${name}!`;
return {
statusCode: 200,
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ message: greeting }),
};
};
// serverless.yml (Serverless Framework)
service: my-service
provider:
name: aws
runtime: nodejs20.x
region: ap-northeast-2
functions:
hello:
handler: handler.handler
events:
- http:
path: /hello
method: post
Vercel Edge Function
// app/api/hello/route.ts (Next.js App Router)
import { NextRequest } from 'next/server';
export const runtime = 'edge'; // Edge Runtime 사용
export async function GET(request: NextRequest) {
const { searchParams } = new URL(request.url);
const name = searchParams.get('name') || 'World';
return new Response(
JSON.stringify({ message: `Hello, ${name}!` }),
{
status: 200,
headers: { 'Content-Type': 'application/json' },
}
);
}
Cloudflare Workers
// src/index.ts
export default {
async fetch(request: Request, env: Env): Promise<Response> {
const url = new URL(request.url);
if (url.pathname === '/api/hello') {
const name = url.searchParams.get('name') || 'World';
return new Response(
JSON.stringify({ message: `Hello, ${name}!` }),
{
headers: { 'Content-Type': 'application/json' },
}
);
}
return new Response('Not Found', { status: 404 });
},
};
Cold Start Optimization
// Lambda Provisioned Concurrency (항상 warm 상태 유지)
// serverless.yml
functions:
api:
handler: handler.main
provisionedConcurrency: 5 # 5개 인스턴스 warm 유지
// 또는 코드 레벨 최적화
// 모듈 초기화를 핸들러 밖에서 (재사용됨)
import { DynamoDB } from '@aws-sdk/client-dynamodb';
const dynamodb = new DynamoDB({}); // Cold Start 시 1회만 실행
export const handler = async (event) => {
// 핸들러는 매 요청마다 실행
const result = await dynamodb.getItem({ /* ... */ });
return { statusCode: 200, body: JSON.stringify(result) };
};
💬 Real-world conversation examples
👨💼 PM
"We expect traffic to increase 10x with this promotion. Do we need to add more servers?"
👩💻 Backend developer
"It's serverless, so it scales automatically. Lambda automatically increases the number of concurrent executions, so a traffic surge isn't a problem. And you're only billed for actual usage."
👨💻 Junior developer
"API responses are slow sometimes. Only the first request takes about 2 seconds, and after that it's fast."
👩💻 Senior developer
"That's the Cold Start phenomenon. When Lambda isn't used for a while, the container is shut down, and when a request comes in again, it's slow because it has to initialize. Setting up Provisioned Concurrency or using the Edge runtime will improve it."