|
| 1 | +--- |
| 2 | +name: new-lambda |
| 3 | +description: Scaffolds a new event-driven AWS Lambda function adhering to cur8d standards with Powertools (Logger, Tracer, Metrics), Pydantic models, unit tests, and CDK infrastructure stack. Use whenever the user asks to create, scaffold, or add a Lambda function or service. |
| 4 | +category: coding |
| 5 | +tags: [aws, lambda, cdk, python, powertools, cur8d] |
| 6 | +--- |
| 7 | + |
| 8 | +# Create New Lambda Function in cur8d/lambda |
| 9 | + |
| 10 | +This skill guides you through scaffolding, implementing, and registering a new AWS Lambda function and its corresponding AWS CDK infrastructure stack in `cur8d/lambda`. |
| 11 | + |
| 12 | +## Prerequisites |
| 13 | + |
| 14 | +- Python 3.12+ and `uv` installed |
| 15 | +- `mise` installed |
| 16 | +- Docker installed and running (for local test invoke via LocalStack) |
| 17 | + |
| 18 | +## Workflow |
| 19 | + |
| 20 | +### 1. Scaffold Boilerplate Files |
| 21 | +Run the built-in generator script with the function name (in snake_case or kebab-case): |
| 22 | + |
| 23 | +```bash |
| 24 | +uv run python scripts/new.py --name <function_name> |
| 25 | +``` |
| 26 | + |
| 27 | +This generates: |
| 28 | +- `templates/<function_name>/handler.py` (Lambda handler entry point) |
| 29 | +- `templates/<function_name>/models.py` (Pydantic payload/event models) |
| 30 | +- `templates/<function_name>/settings.py` (Environment variables and settings via Pydantic) |
| 31 | +- `tests/<function_name>/test_handler.py` (Pytest test suite) |
| 32 | +- `infra/stacks/<function_name>.py` (AWS CDK Stack definition) |
| 33 | +- `docs/template/<function_name>.md` & `docs/reference/<function_name>.md` (Documentation) |
| 34 | +- Updates `infra/app.py` to register the new stack in `STACK_REGISTRY`. |
| 35 | + |
| 36 | +### 2. Implement Handler Logic & Models |
| 37 | +In `templates/<function_name>/handler.py`: |
| 38 | +- Use AWS Lambda Powertools decorators: |
| 39 | + ```python |
| 40 | + from aws_lambda_powertools import Logger, Metrics, Tracer |
| 41 | + |
| 42 | + logger = Logger() |
| 43 | + tracer = Tracer() |
| 44 | + metrics = Metrics() |
| 45 | + |
| 46 | + |
| 47 | + @logger.inject_lambda_context(log_event=True) |
| 48 | + @tracer.capture_lambda_handler |
| 49 | + @metrics.log_metrics(capture_cold_start_metric=True) |
| 50 | + def lambda_handler(event: dict, context) -> dict: ... |
| 51 | + ``` |
| 52 | +- Define typed request/event models in `models.py` using Pydantic. |
| 53 | +- Define environment settings in `settings.py` using `pydantic_settings.BaseSettings`. |
| 54 | + |
| 55 | +### 3. Configure the CDK Stack |
| 56 | +In `infra/stacks/<function_name>.py`: |
| 57 | +- Customize runtime settings (memory size, timeout, log retention). |
| 58 | +- Wire triggers according to event source (API Gateway, SQS Queue, S3 Event, EventBridge rule, DynamoDB Streams). |
| 59 | +- Configure IAM permissions with least privilege. |
| 60 | + |
| 61 | +### 4. Implement Unit Tests |
| 62 | +In `tests/<function_name>/test_handler.py`: |
| 63 | +- Test successful execution with nominal mock event payloads. |
| 64 | +- Test invalid event schemas (expect appropriate validation errors). |
| 65 | +- Test error handling and cold start resilience. |
| 66 | + |
| 67 | +### 5. Verify Quality Gates |
| 68 | +Run repository verification: |
| 69 | + |
| 70 | +```bash |
| 71 | +mise run verify |
| 72 | +``` |
| 73 | + |
| 74 | +Ensure formatting (`ruff format`), linting (`ruff check`), type checks, and pytest tests pass. |
| 75 | + |
| 76 | +## Boundaries |
| 77 | + |
| 78 | +✅ **Always do:** |
| 79 | +- Always decorate handlers with `@logger.inject_lambda_context`, `@tracer.capture_lambda_handler`, and `@metrics.log_metrics`. |
| 80 | +- Always register the new CDK stack in `infra/app.py`'s `STACK_REGISTRY`. |
| 81 | +- Always verify all checks pass with `mise run verify` before committing. |
| 82 | + |
| 83 | +🚫 **Never do:** |
| 84 | +- Never hardcode AWS credentials, account IDs, or region names in handlers or stacks. |
| 85 | +- Never write handlers without companion unit tests in `tests/<function_name>/`. |
0 commit comments