AWS CloudFront는 전 세계 엣지 로케이션에서 콘텐츠를 캐싱하여 속도를 높입니다. CloudFront 로그 분석으로 캐시 히트율, 지역별 성능, 비용 최적화 지점을 파악하는 방법을 알아봅니다.
cloudfront/)탭 구분(TSV) 형식:
`` #Version: 1.0 #Fields: date time x-edge-location sc-bytes c-ip cs-method cs(Host) cs-uri-stem sc-status cs(Referer) cs(User-Agent) cs-uri-query cs(Cookie) x-edge-result-type x-edge-request-id x-host-header cs-protocol cs-bytes time-taken x-forwarded-for ssl-protocol ssl-cipher x-edge-response-result-type cs-protocol-version fle-status fle-encrypted-fields c-port time-to-first-byte x-edge-detailed-result-type sc-content-type sc-content-len sc-range-start sc-range-end `
주요 필드:
| 필드 | 의미 | |
|---|---|---|
| ------ | ------ | |
date time | 요청 시각 (UTC) | |
x-edge-location | 엣지 로케이션 코드 (예: SEL50) | |
sc-bytes | 응답 바이트 수 | |
c-ip | 클라이언트 IP | |
cs-uri-stem | 요청 경로 | |
sc-status | HTTP 상태 코드 | |
x-edge-result-type | 캐시 결과 (Hit, Miss, Error 등) | |
time-taken | 처리 시간 (초) | |
| 값 | 의미 | 원인 |
| ---- | ------ | ------ |
Hit | 캐시에서 제공 | 정상 |
RefreshHit | 캐시 재검증 후 제공 | TTL 짧음 |
Miss | 캐시 없음, 오리진 요청 | 최초 요청 또는 TTL 만료 |
Error | 오류 발생 | 오리진 문제 |
LimitExceeded | Rate limit 초과 | 공격 또는 과도한 요청 |
`bash awk -F'\t' '$14=="Miss" {print $8}' *.log | sort | uniq -c | sort -rn | head -20 `
`bash awk -F'\t' '{print $3}' *.log | cut -c1-3 | sort | uniq -c | sort -rn `
출력 예시:
` 23421 SEL (서울) 12341 NRT (도쿄) 8234 SIN (싱가포르) 4231 LAX (로스앤젤레스) `
`bash awk -F'\t' '{loc=substr($3,1,3); time[loc]+=$19; count[loc]++} END {for(l in time) printf "%s: %.3f\n", l, time[l]/count[l]}' *.log `
`bash awk -F'\t' '{bytes[$8]+=$4} END {for(p in bytes) print bytes[p], p}' *.log | sort -rn | head -10 `
`bash awk -F'\t' '$4 > 10000000 {print $8, $4}' *.log | sort -k2 -rn `
10MB 이상 응답 찾기
오리진 서버(S3/EC2)에서 긴 TTL 설정:
` Cache-Control: public, max-age=31536000, immutable `
불필요한 쿼리 문자열은 캐시 키를 분리시킵니다:
` /image.jpg?v=1 /image.jpg?v=2 `
CloudFront Behavior 설정에서 "Query String Forwarding" = None
헤더 조작으로 캐시 최적화:
`javascript function handler(event) { var response = event.response; response.headers['cache-control'] = {value: 'public, max-age=86400'}; return response; } `
`bash awk -F'\t' '$10 ~ /^5/ {print $8}' *.log | sort | uniq -c | sort -rn `
S3 Bucket Policy 또는 서명 문제:
`bash grep $'\t403\t' *.log | awk -F'\t' '{print $8}' | sort | uniq -c `
AWS Athena를 사용하면 SQL로 CloudFront 로그를 직접 쿼리할 수 있습니다.
`sql CREATE EXTERNAL TABLE cloudfront_logs ( date DATE, time STRING, location STRING, bytes BIGINT, request_ip STRING, method STRING, host STRING, uri STRING, status INT, referer STRING, user_agent STRING, query_string STRING, cookie STRING, result_type STRING, request_id STRING, host_header STRING, request_protocol STRING, request_bytes BIGINT, time_taken FLOAT ) ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t' LOCATION 's3://my-bucket/cloudfront/' TBLPROPERTIES ("skip.header.line.count"="2"); `
`sql SELECT result_type, COUNT(*) AS cnt, ROUND(100.0 COUNT() / SUM(COUNT(*)) OVER(), 2) AS pct FROM cloudfront_logs WHERE date >= DATE '2024-10-01' GROUP BY result_type; `
CloudFront 로그는 x-edge-result-type으로 캐시 히트 여부를, x-edge-location`으로 지역별 성능을 분석할 수 있습니다. 캐시 히트율 80% 이상을 목표로 TTL, Query String, Cache-Control 헤더를 최적화하세요.