AWS

CloudFront Function - 간단한 로직을 엣지에서 처리할 때 유용한 도구

heesoohi 2025. 6. 22. 02:40

CloudFront Function은 CloudFront에 붙일 수 있는 초경량 JavaScript 기반 함수이다. 주로 Viewer Request / Viewer Response 단계에서 실행되어, 요청 또는 응답을 빠르게 조작하는 데 사용된다.

 

CloudFront Function은 Lambda@Edge보다 가볍고, 빠르며, 배포 시간이 매우 짧다는 특징이 있다. 복잡한 연산보다는 조건 분기, 헤더 제거/수정, URL 리디렉션과 같은 간단한 작업에 사용하는 것이 적합하다.

 

예) 오래된 디바이스(User-Agent가 특정 값일 경우)에 대해 특정 응답 헤더를 제거하고 싶다면, 아뢔와 같은 CloudFront Function을 사용할 수 있다.

function handler(event) {
  var response = event.response;
  var headers = response.headers;
  var userAgent = event.request.headers['user-agent']?.[0]?.value || '';

  if (userAgent.includes('OldDevice')) {
    delete headers['x-custom-header'];
  }

  return response;
}

 

 

이렇게 CloudFront Function을 활용하면 비용과 속도를 최소화하면서도 필터링된 응답을 제공할 수 있다.