aws-samples / aws-samples/sample-finops-agent

Add structured error logging to Lambda MCP server functions

Open
#2 0 comments 0 reactions 0 assignees View on GitHub
claude
Dominant language
Python
Stars
14
Forks
4
PR merge metrics
No merged PRs in 30d

Description

## Problem

All Lambda MCP server functions use generic exception handling that returns plain error strings without context:

```python
except Exception as e:
return {"error": str(e)}
```

This makes debugging difficult in production — no stack traces, no request context, and no error classification in CloudWatch Logs.

**Affected files:**
- `src/lambda/mcp_servers/cost_explorer/lambda_function.py`
- `src/lambda/mcp_servers/athena/lambda_function.py`
- `src/lambda/mcp_servers/cur_analyst/lambda_function.py`

## Expected Behavior

Each Lambda handler should log structured errors with:
- Handler/tool name that failed
- Input parameters (sanitized)
- Full stack trace via `exc_info=True`
- Use Python's `logging` module configured at module level

## Example

```python
import logging

logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)

def some_handler(event):
tool_name = event.get("tool_name", "unknown")
try:
# ... handler logic
except Exception as e:
logger.error("Error in handler %s", tool_name, exc_info=True)
return {"error": str(e)}
```

## Scope

- Add `logging` import and logger setup to each affected Lambda
- Wrap existing `except` blocks with structured logging
- Do not change the return format — keep `{"error": str(e)}` for backward compatibility

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.