Developing Serverless Applications with Python

Best practices for building serverless applications using Python on cloud platforms like AWS Lambda and Google Cloud Functions.

0 likes
9 views

Rule Content

title: Developing Serverless Applications with Python
description: Best practices for building serverless applications using Python on cloud platforms like AWS Lambda and Google Cloud Functions.
category: Python Cursor Rules
rules:
  - id: python-serverless-best-practices
    description: >
      Ensure Python serverless functions are efficient, secure, and maintainable by following these best practices.
    severity: high
    tags:
      - serverless
      - python
      - best-practices
    patterns:
      - pattern: |
          def $FUNC_NAME(...):
              ...
      - pattern: |
          async def $FUNC_NAME(...):
              ...
    actions:
      - message: >
          Consider breaking down this function into smaller, single-purpose components to enhance maintainability and scalability.
        condition: >
          function_length($FUNC_NAME) > 50
      - message: >
          Ensure proper error handling is implemented to manage exceptions gracefully and provide meaningful error messages.
        condition: >
          not contains($FUNC_NAME, 'try') or not contains($FUNC_NAME, 'except')
      - message: >
          Optimize memory allocation by setting appropriate memory sizes based on the function's actual needs to improve performance and reduce costs.
        condition: >
          not contains($FUNC_NAME, 'memory_size')
      - message: >
          Implement secure management of sensitive information by using environment variables or secret management services instead of hardcoding secrets.
        condition: >
          contains($FUNC_NAME, 'SECRET_KEY') or contains($FUNC_NAME, 'PASSWORD')
      - message: >
          Set up comprehensive logging and monitoring to track function performance and troubleshoot issues effectively.
        condition: >
          not contains($FUNC_NAME, 'logging') or not contains($FUNC_NAME, 'monitoring')
      - message: >
          Regularly test your functions to ensure reliability and catch potential issues early.
        condition: >
          not contains($FUNC_NAME, 'test') or not contains($FUNC_NAME, 'pytest')
      - message: >
          Minimize dependencies to reduce cold start times and potential security vulnerabilities.
        condition: >
          count_imports($FUNC_NAME) > 5
      - message: >
          Implement proper security protocols and follow the principle of least privilege to enhance function security.
        condition: >
          not contains($FUNC_NAME, 'iam') or not contains($FUNC_NAME, 'policy')
      - message: >
          Optimize function performance by reducing cold start times through appropriate memory allocation and keeping instances warm.
        condition: >
          not contains($FUNC_NAME, 'warm') or not contains($FUNC_NAME, 'cold start')
      - message: >
          Ensure your function is designed for scalability to handle varying loads efficiently.
        condition: >
          not contains($FUNC_NAME, 'scalable') or not contains($FUNC_NAME, 'concurrency')