agentleFS
jimmypocockjimmypocock/cursor-rules/.cursor/rules/aws-lambda.mdc

cursor-rules / rules

Cursor rule8 starsChanged 19 months agoMITView on GitHub

AWS Lambda Function Best Practices

The file

Show its rules and text

We split this aws-lambda.mdc into the rules it gives an agent

35 rules. About 0 are common, 35 are distinctive, and 0 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈35

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 0

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    Follow single responsibility principle for each Lambda function

    line 16
  2. Distinctive:

    Keep functions focused on one specific task or domain operation

    line 17
  3. Distinctive:

    Create pure functions where possible for easier testing

    line 19
  4. Distinctive:

    Avoid long-running processes.

    line 22
  5. Distinctive:

    Keep handlers thin, delegating to service or business logic layers

    line 25
  6. Distinctive:

    Add custom context to errors for better debugging

    line 28
  7. Distinctive:

    Follow these layers in Lambda implementation:

    line 29
  8. Distinctive:

    Use environment variables for configuration

    line 41
  9. Distinctive:

    Keep function code and dependencies lightweight

    line 46
  10. Distinctive:

    Use AWS Lambda Layers for large dependencies

    line 47
  11. Distinctive:

    Use provisioned concurrency for critical paths

    line 48
  12. Distinctive:

    Consider using NodeJS or native runtime for faster cold starts

    line 50
  13. Distinctive:

    Add context information to errors

    line 56
  14. Distinctive:

    Use structured logging with correlation IDs

    line 57
  15. Distinctive:

    Use try/catch blocks with specific error types

    line 59
  16. Distinctive:

    Consider implementing circuit breakers for external services

    line 61
  17. Distinctive:

    Never store secrets in environment variables - use Parameter Store or Secrets Manager

    line 65
  18. Distinctive:

    Use Enhanced VPC Flow Logs with Lambda in VPC

    line 68
  19. Distinctive:

    Use AWS X-Ray for tracing

    line 70
  20. Distinctive:

    Use async patterns for non-critical operations

    line 76
  21. Distinctive:

    Consider using provisioned concurrency vs.

    line 79
  22. Distinctive:

    Use Step Functions for complex workflows instead of long-running Lambdas

    line 81
  23. Distinctive:

    Add correlation IDs across service invocations

    line 85
  24. Distinctive:

    Use CloudWatch metrics for monitoring key performance indicators

    line 86
  25. Distinctive:

    Create custom metrics for business-specific monitoring

    line 90
  26. Distinctive:

    Use Parameter Store for more complex configuration

    line 95
  27. Distinctive:

    Write unit tests for business logic

    line 99
  28. Distinctive:

    Create integration tests for service interactions

    line 100
  29. Distinctive:

    Use mocks for AWS services in unit tests

    line 102
  30. Distinctive:

    Test error handling and edge cases

    line 103
  31. Distinctive:

    Create test fixtures for common test data

    line 105
  32. Distinctive:

    Use Infrastructure as Code (CloudFormation, SAM, CDK)

    line 108
  33. Distinctive:

    Use function versions and aliases for controlled deployments

    line 110
  34. Distinctive:

    Create separate stacks for different environments

    line 113
  35. Distinctive:

    Consider using SAM for simplified Lambda deployment

    line 114

≈ “Common” is an estimate: at least 1 in 5 of the 3,030 Cursor rule files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
aws-lambda.mdc
---
description: AWS Lambda Function Best Practices
globs: 
alwaysApply: false
---
---
Description: AWS Lambda Function Best Practices
Globs: **/lambda/**/*.ts, **/functions/**/*.ts, **/src/**/*.ts, **/lambda/**/*.py, **/functions/**/*.py, **/src/**/*.py
---

# AWS Lambda Development Standards
@base.mdc
@typescript.mdc

## Lambda Function Design
- Follow single responsibility principle for each Lambda function
- Keep functions focused on one specific task or domain operation
- Separate business logic from infrastructure concerns
- Create pure functions where possible for easier testing
- Implement idempotent functions to handle retries safely
- Design functions to be stateless
- Avoid long-running processes; break them into smaller steps

## Handler Structure
- Keep handlers thin, delegating to service or business logic layers
- Validate input parameters early in the function
- Return properly structured responses
- Add custom context to errors for better debugging
- Follow these layers in Lambda implementation:
  1. Handler layer (entry point)
  2. Service layer (business logic)
  3. Data access layer (repository pattern)
  4. External service integrations

## Performance Optimization
- Initialize SDK clients and database connections outside the handler
- Cache static assets and reference data between invocations
- Implement connection pooling for database access
- Utilize provisioned concurrency for latency-sensitive functions
- Set appropriate memory allocation based on function needs
- Use environment variables for configuration
- Implement backoff strategies for external service calls
- Leverage AWS SDK client reuse whenever possible

## Cold Start Mitigation
- Keep function code and dependencies lightweight
- Use AWS Lambda Layers for large dependencies
- Use provisioned concurrency for critical paths
- Implement warmup mechanisms for important functions
- Consider using NodeJS or native runtime for faster cold starts
- Choose appropriate memory settings to balance CPU/memory needs
- Set reasonable function timeouts

## Error Handling
- Implement comprehensive error handling and logging
- Add context information to errors
- Use structured logging with correlation IDs
- Distinguish between client errors (4xx) and server errors (5xx)
- Use try/catch blocks with specific error types
- Handle retries with exponential backoff
- Consider implementing circuit breakers for external services

## Security Best Practices
- Apply least privilege principle to Lambda IAM roles
- Never store secrets in environment variables - use Parameter Store or Secrets Manager
- Implement input validation for all parameters
- Set environment variables for configuration
- Use Enhanced VPC Flow Logs with Lambda in VPC
- Enable Lambda Insights for monitoring
- Use AWS X-Ray for tracing
- Implement rate limiting for public-facing functions

## Cost Optimization
- Optimize memory allocation for the workload
- Implement aggressive timeouts
- Use async patterns for non-critical operations
- Batch operations where possible
- Minimize dependency size to reduce execution time
- Consider using provisioned concurrency vs. on-demand based on traffic patterns
- Optimize code and dependencies to reduce execution time
- Use Step Functions for complex workflows instead of long-running Lambdas

## Observability
- Implement structured logging with consistent formats
- Add correlation IDs across service invocations
- Use CloudWatch metrics for monitoring key performance indicators
- Set up alerts for errors, throttling, and timeout events
- Enable X-Ray tracing for complex operations
- Log important business events and metrics
- Create custom metrics for business-specific monitoring

## Environment-Specific Configuration
- Use environment variables for configuration
- Implement feature flags for controlled releases
- Use Parameter Store for more complex configuration
- Implement different logging levels based on environment

## Testing Guidelines
- Write unit tests for business logic
- Create integration tests for service interactions
- Implement local testing with aws-sam-local or localstack
- Use mocks for AWS services in unit tests
- Test error handling and edge cases
- Validate retry and backoff strategies
- Create test fixtures for common test data

## Deployment Practices
- Use Infrastructure as Code (CloudFormation, SAM, CDK)
- Implement CI/CD pipelines for automated deployment
- Use function versions and aliases for controlled deployments
- Implement progressive deployment strategies (canary, blue/green)
- Automate rollback procedures
- Create separate stacks for different environments
- Consider using SAM for simplified Lambda deployment

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.