← Back to list

writing-python-lambdas
by cm-yoshikikasama
blog backup md
⭐ 1🍴 0📅 Jan 18, 2026
SKILL.md
name: writing-python-lambdas description: Provides Python Lambda implementation patterns with type hints, boto3 client initialization optimization, and proper error handling. Use when creating or modifying Python Lambda code, implementing AWS Lambda handlers, or working with boto3 clients.
Python Lambda実装
PythonでAWS Lambda関数を実装するためのガイドです。
基本的なLambda handler
import json
from typing import Any
def lambda_handler(event: dict[str, Any], _context: Any) -> dict[str, Any]:
"""Lambda handler"""
try:
# メイン処理
result = process(event)
return {
"statusCode": 200,
"body": json.dumps({"result": result})
}
except Exception as e:
print(f"Lambda failed: {str(e)}")
raise
実装ワークフロー
以下のチェックリストに従って実装を進める
進捗:
- [ ] Step 1: handler構造を作成
- [ ] Step 2: すべての関数に型ヒントを追加
- [ ] Step 3: boto3クライアントをグローバルスコープで初期化
- [ ] Step 4: try-except-raiseでエラーハンドリングを実装
- [ ] Step 5: ログ出力を追加
- [ ] Step 6: コーディング規約と照合
Step 1: handler構造を作成
上記の基本テンプレートを基盤として使用。
Step 2: 型ヒントを追加
すべての関数に型ヒントが必須。必要なimport
from typing import Any, Dict, List, Optional
Step 3: boto3クライアントを初期化
Lambdaウォームスタート最適化のため、クライアント初期化をグローバルスコープに移動
# Good - グローバルスコープ
s3_client = boto3.client('s3')
def lambda_handler(event, context):
s3_client.get_object(...)
詳細は coding-conventions.md を参照。
Step 4: エラーハンドリングを実装
常にtry-exceptと再raiseパターンを使用
try:
# メイン処理
except Exception as e:
print(f"Lambda failed: {str(e)}")
raise # CloudWatch Logsのために再raise必須
Step 5: ログ出力を追加
明確なフォーマットでprint文を使用
print("Lambda started")
print(f"Processing: {item_count:,} items")
print("Lambda completed")
Step 6: コーディング規約と照合
coding-conventions.md と照合してコンプライアンスを確認。
詳細ガイド
coding-conventions.md - コーディング規約、実装例
Score
Total Score
50/100
Based on repository quality metrics
✓SKILL.md
SKILL.mdファイルが含まれている
+20
○LICENSE
ライセンスが設定されている
0/10
○説明文
100文字以上の説明がある
0/10
○人気
GitHub Stars 100以上
0/15
○最近の活動
3ヶ月以内に更新がある
0/10
○フォーク
10回以上フォークされている
0/5
✓Issue管理
オープンIssueが50未満
+5
✓言語
プログラミング言語が設定されている
+5
○タグ
1つ以上のタグが設定されている
0/5
Reviews
💬
Reviews coming soon