Skip to content

project rulesDevOps and cloudaws

Serverless Lambda Rules

Project rules for AWS Lambda functions: thin handlers, cold start care, validated environment config, structured logs and idempotent processing.

npx workspacetemplates add serverless-lambda-rules --tool claude-code

Writes 1 file to your project root

CLAUDE.mdadd to your file23 lines · 1.5 KBFile
# Project rules: AWS Lambda

## Structure
- Keep the handler a thin adapter: parse the event, call a plain function, shape the response. Business logic lives in modules that know nothing about Lambda.
- Create SDK clients and read config once, outside the handler, so warm invocations reuse them.
- One function, one job. Share code through a layer or a bundled package, not by importing between functions.

## Cold start and size
- Bundle with esbuild or the framework's bundler, tree-shake, and import only the SDK client modules you use.
- Choose memory by measurement; more memory also means more CPU. Set the timeout just above the real worst case, below the upstream caller's timeout.

## Configuration
- Read environment variables through one typed loader that fails fast when a value is missing. Secrets come from Secrets Manager or Parameter Store, cached between invocations. Never put them in plain environment variables committed to git.

## Reliability
- Assume at-least-once delivery. Make handlers idempotent with a key (event ID or business ID) stored with a conditional write.
- For SQS batches, return `batchItemFailures` so one bad record does not retry the whole batch. Attach a dead-letter queue to every async source.

## Logging
- Log one JSON object per line with request ID, function name and outcome. Never log payloads containing personal data.

## Before finishing
- Run unit tests on the core logic, a local invoke with a sample event, and the type check and linter.

What it does

These rules guide an agent writing AWS Lambda functions that stay small, fast to start and safe to retry. They separate the handler from business logic so most code can be tested without AWS.

They also cover the operational habits that matter in production: structured logs, timeouts, partial batch failures and duplicate event handling.

Example

you › Write a Lambda that processes S3 upload events and stores metadata in DynamoDB

agent › The agent writes a thin handler, a pure metadata function with tests, a conditional put for idempotency, JSON logging, a dead-letter queue and a sample event for local invoke.

Questions

Does this assume a specific framework?
No. It works with SAM, CDK, Serverless Framework or Terraform, because it only covers the function code and its settings.
Why create clients outside the handler?
Warm invocations then reuse connections instead of rebuilding them on every call, which cuts latency and cost.
  • AWS CDK Rules

    Project rulesaws

    Free
  • Docker Rules

    Project rulesdocker

    Free
  • GitHub Actions Rules

    Project rulesgithub-actions

    Free
  • Kubernetes Rules

    Project ruleskubernetes

    Free