본문 바로가기
[GPUaaS]

월별 GPU 리포트 Lambda 코드 (Prometheus 기반)

by METAVERSE STORY 2026. 1. 7.
반응형

 

 

실제 운영에 바로 투입 가능한 “월별 GPU 사용 리포트 자동 생성 Lambda (Python)” 예제입니다.
👉 Prometheus → 월별 GPU-Hour 집계 → CSV 생성 → S3 저장
👉 GPUaaS 정산(Chargeback/Showback) 기준에 맞게 설계했습니다.


 

1️⃣ 아키텍처 전제

 
EventBridge (매월 101:00) ↓ AWS Lambda (Python) ↓ Prometheus HTTP API ↓ GPU 사용량 집계 ↓ CSV 리포트 생성 ↓ S3 업로드

2️⃣ Lambda 환경 변수 (필수)

변수명설명
PROMETHEUS_URL Prometheus URL
S3_BUCKET 리포트 저장 버킷
S3_PREFIX reports/gpu
GPU_PRICE_A100 A100 시간당 단가
GPU_PRICE_T4 T4 시간당 단가

3️⃣ Prometheus 정산용 쿼리 (월별)

 
sum by (namespace, modelName) ( count_over_time( DCGM_FI_DEV_GPU_UTIL > 0 [30d]) )

4️⃣ Lambda Python 코드 (실무용)

 
import os import csv import json import requests import boto3 from datetime import datetime, timedelta PROM_URL = os.environ['PROMETHEUS_URL'] S3_BUCKET = os.environ['S3_BUCKET'] S3_PREFIX = os.environ.get('S3_PREFIX', 'reports/gpu') GPU_PRICE = { "A100": float(os.environ.get("GPU_PRICE_A100", 4000)), "T4": float(os.environ.get("GPU_PRICE_T4", 900)) } s3 = boto3.client('s3') def query_prometheus(query): response = requests.get( f"{PROM_URL}/api/v1/query", params={"query": query}, timeout=10 ) response.raise_for_status() return response.json()['data']['result'] def lambda_handler(event, context): # 지난 달 기준 today = datetime.utcnow() first_day_this_month = today.replace(day=1) last_month = first_day_this_month - timedelta(days=1) report_month = last_month.strftime("%Y-%m") promql = """ sum by (namespace, modelName) ( count_over_time( DCGM_FI_DEV_GPU_UTIL > 0 [30d]) ) """ results = query_prometheus(promql) rows = [] for r in results: namespace = r['metric'].get('namespace', 'unknown') gpu_model = r['metric'].get('modelName', 'unknown') gpu_hours = float(r['value'][1]) price = GPU_PRICE.get(gpu_model, 0) cost = gpu_hours * price rows.append([ report_month, namespace, gpu_model, round(gpu_hours, 2), price, round(cost, 2) ]) filename = f"gpu-usage-{report_month}.csv" local_path = f"/tmp/{filename}" with open(local_path, "w", newline="") as f: writer = csv.writer(f) writer.writerow([ "Month", "Namespace", "GPU_Model", "GPU_Hours", "Price_per_Hour", "Cost" ]) writer.writerows(rows) s3_key = f"{S3_PREFIX}/{filename}" s3.upload_file(local_path, S3_BUCKET, s3_key) return { "status": "success", "report": f"s3://{S3_BUCKET}/{s3_key}", "rows": len(rows) }

5️⃣ 생성되는 CSV 리포트 예시

MonthNamespaceGPUGPU_HoursPriceCost
2025-12 team-ai A100 320 4000 1,280,000
2025-12 team-data T4 210 900 189,000

6️⃣ EventBridge 스케줄 설정 (매월 1회)

 
cron(0 1 1 * ? *)
  • 매월 1일 01:00 UTC
  • 전월 GPU 사용량 집계

7️⃣ 운영 고도화 옵션 (강력 추천)

✔ 평균 GPU 사용률 보정

 
GPU 비용 = GPU-Hour × 단가 × 사용률 보정계수

→ PromQL 추가 조회 후 Lambda에서 반영


✔ Slack / Email 자동 발송

  • S3 업로드 후 Slack Webhook
  • SES 메일 전송

✔ Athena 연동

  • CSV → Athena External Table
  • 재무팀/경영 리포트 활용

8️⃣ 실무 운영 체크 포인트

✅ Namespace 라벨 표준화
✅ GPU 모델 라벨(modelName) 확인
✅ Pod 종료 후 메트릭 보존 (Prometheus retention)
✅ Spot / On-Demand 구분 필요 시 label 추가

 

 

반응형

댓글