serverless

SKILL.md

Serverless

Function-as-a-Service and managed cloud services.

When to Use

  • Event-driven workloads
  • Variable traffic patterns
  • Reduced operations overhead
  • Microservices functions

Quick Start

// AWS Lambda
import { APIGatewayProxyHandler } from "aws-lambda";

export const handler: APIGatewayProxyHandler = async (event) => {
  const body = JSON.parse(event.body || "{}");

  return {
    statusCode: 200,
    body: JSON.stringify({ message: "Hello", data: body }),
  };
};

Core Concepts

Function Patterns

// Request-response
export const apiHandler = async (event: APIGatewayProxyEvent) => {
  const { pathParameters, body } = event;
  const result = await processRequest(
    pathParameters?.id,
    JSON.parse(body || "{}"),
  );
  return { statusCode: 200, body: JSON.stringify(result) };
};

// Event processing
export const sqsHandler = async (event: SQSEvent) => {
  for (const record of event.Records) {
    const message = JSON.parse(record.body);
    await processMessage(message);
  }
};

// Scheduled
export const cronHandler = async (event: ScheduledEvent) => {
  await performDailyCleanup();
};

Cold Start Optimization

// Initialize outside handler
import { DynamoDBClient } from '@aws-sdk/client-dynamodb';

const dynamodb = new DynamoDBClient({});

export const handler = async (event: APIGatewayProxyEvent) => {
  // Client reused across invocations
  const result = await dynamodb.send(new GetItemCommand({...}));
  return { statusCode: 200, body: JSON.stringify(result) };
};

Common Patterns

Serverless Framework

# serverless.yml
service: my-api
provider:
  name: aws
  runtime: nodejs20.x

functions:
  getUser:
    handler: src/users.get
    events:
      - http:
          path: users/{id}
          method: get

  processOrder:
    handler: src/orders.process
    events:
      - sqs:
          arn: !GetAtt OrderQueue.Arn

Error Handling

export const handler = async (event: APIGatewayProxyEvent) => {
  try {
    const result = await processRequest(event);
    return { statusCode: 200, body: JSON.stringify(result) };
  } catch (error) {
    console.error("Error:", error);
    return {
      statusCode: error.statusCode || 500,
      body: JSON.stringify({ error: error.message }),
    };
  }
};

Best Practices

Do:

  • Initialize clients outside handler
  • Use environment variables
  • Implement proper logging
  • Set appropriate timeouts

Don't:

  • Ignore cold starts
  • Use synchronous operations
  • Skip error handling
  • Hardcode configuration

Troubleshooting

Issue Cause Solution
Cold start slow Heavy init Use provisioned concurrency
Timeout Long operation Increase timeout or async
Memory error Large payloads Increase memory

References

Weekly Installs
2
GitHub Stars
7
First Seen
Feb 10, 2026
Installed on
mcpjam2
claude-code2
replit2
junie2
windsurf2
zencoder2