본문 바로가기

Ops/AWS

AWS CloudFront - 홈페이지 점검 페이지로 Redirect

반응형

AWS CloudFront - 홈페이지 점검 페이지로 Redirect

 

 

 

AWS 환경에서 Homepage로 접속은 보통 CloudFront로 연결해서 사용하고 있을 텐데요, 점검 등의 이슈로 특정 이미지만 있는 페이지로 redirect 시켜야 할 때 활용하는 방법입니다.

Lambda로 코드를 작성해서 Lambda@Edge로 등록하여 활용하시면 됩니다. 아래는 Lambda 코드입니다.

 

exports.handler = (event, context, callback) => {
    const request = event.Records[0].cf.request;
    const headers = request.headers;
    
    console.log('request_ip', request.clientIp);
    
    if(request.clientIp != "111.222.333.444" && request.clientIp != "555.666.777.888") {
        const response = {
            status: '200',
            statusDescription: 'OK',
            headers: {
                'cache-control': [{
                    key: 'Cache-Control',
                    value: 'no-cache, no-store, must-revalidate'
                }],
                'content-type': [{
                    key: 'Content-Type',
                    value: 'text/html'
                }]
            },
            body: `
<!DOCTYPE html>
<html lang="en">
    <head>
      <meta charset="utf-8" />
      <meta name="viewport" content="width=device-width, initial-scale=1" />
      <meta name="theme-color" content="#000000" />
    
      <title>NAEMO Creator</title>
      <meta name="title" content="chandler" />
      <meta name="author" content="chandler" />
      <meta name="robots" content="noindex, nofollow" />
    </head>
  <body style="margin: 0px; background-color: #2e2e2e">
    <div id="shutdown" style="display: flex; justify-content: center; background: linear-gradient(180deg, #101010 0%, #2E2E2E 91.27%);min-height:100%;">
      <picture>
        <source media="(min-width: 1080px)" srcset="https://mkdir-chandler.tistory.com/shutdown_pc.png" />
        <source media="(min-width: 640px)" srcset="https://mkdir-chandler.tistory.com/shutdown_tablet.png" />
        <img src="https://mkdir-chandler.tistory.com/shutdown_mobile.png" />
      </picture>
    </div>
  </body>
</html>>`
        };
        console.log(response);
        callback(null, response);        
    } else {
        const uri = request.uri;
        const host = request.headers.host[0].value;
    
        // Check whether the URI is missing a file name.
        if (uri.endsWith('/')) {
            request.uri = `/index.html`;
        } 
        // Check whether the URI is missing a file extension.
        else if (!uri.includes('.')) {
            request.uri = `/index.html`;
        }
    
        callback(null, request);
        
    }
    
}

 

 

 


by mkdir-chandler


 

 

 

 

 

728x90
반응형