Serverless

From G1Joshi/Agent-Skills · View interactive version

Serverless is an AI agent skill from the GitHub repository G1Joshi/Agent-Skills. Expert Serverless architecture assistance covering Function-as-a-Service (FaaS), event triggers, cold start mitigation, stateless execution, and cloud BaaS integration. Use when building AWS Lambda, Google Cloud Functions, or Cloudflare Workers systems with event-driven scale.

How to use this skill

Skill file

Serverless

Serverless is a cloud-native development model for building and running applications without managing servers. The cloud provider handles the routine work of provisioning, maintaining, and scaling the server infrastructure.

When to Use

Quick Start

// AWS Lambda Handler (Node.js)
export const handler = async (event) => {
  console.log("Event received:", JSON.stringify(event));

  const name = event.queryStringParameters?.name || "World";

  return {
    statusCode: 200,
    body: JSON.stringify({ message: `Hello, ${name}!` }),
  };
};
# serverless.yml (Serverless Framework)
service: my-api
provider:
  name: aws
  runtime: nodejs20.x

functions:
  hello:
    handler: handler.hello
    events:
      - httpApi:
          path: /hello
          method: get

Core Concepts

Ephemeral Stateless Execution

Instances spin up on demand and shut down after idle periods; in-memory state is destroyed when containers terminate:

// AWS Lambda / Cloudflare Worker Handler
export const handler = async (
  event: APIGatewayProxyEvent,
): Promise<APIGatewayProxyResult> => {
  // Global variables persist across warm starts, but local handler state resets
  const result = await processItem(event.body);
  return {
    statusCode: 200,
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify(result),
  };
};

Cold Start Lifecycle & Mitigation

Understanding initialization phases:

[ Download Runtime ] ──→ [ Init Execution Context (Cold Start) ] ──→ [ Execute Handler (Warm) ]
// Optimize Cold Starts: Initialize heavy clients outside the handler function
import { DynamoDBClient } from "@aws-sdk/client-dynamodb";

const ddbClient = new DynamoDBClient({ region: "us-east-1" }); // Reused on warm starts

Cloud BaaS Integration & Event Mappings

Connects functions directly to cloud managed services without polling:

# serverless.yml event mapping
functions:
  processInvoice:
    handler: src/invoice.handler
    events:
      - s3:
          bucket: enterprise-invoices
          event: s3:ObjectCreated:*
          rules:
            - suffix: .pdf

Common Patterns

Connection Pool Management Outside Handler

Problem: Serverless functions initialize new database connections on every invocation, exhausting connection limits.

Solution: Initialize database pools outside the lambda handler to reuse connections across warm invocations.

import { Pool } from "pg";

// Initialized in global scope during cold start, reused across invocations
const pool = new Pool({
  connectionString: process.env.DATABASE_URL,
  max: 2, // Keep pool small for serverless instances
  idleTimeoutMillis: 30000,
});

export const handler = async (event: any) => {
  const client = await pool.connect();
  try {
    const { rows } = await client.query("SELECT * FROM items WHERE id = $1", [
      event.pathParameters.id,
    ]);
    return { statusCode: 200, body: JSON.stringify(rows[0]) };
  } finally {
    client.release();
  }
};

Best Practices

Do:

Don't:

Troubleshooting

| Error | Cause | Solution |

| :-------------------- | :---------------------- | :----------------------------------------------------------------------------------- |

| Timeout | Function took too long. | Increase timeout setting; optimize code; move to async pattern. |

| OOM (Out of Memory) | Processing large files. | Increase RAM (allocates more CPU too); stream data instead of loading all in memory. |

References

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.