restrict access to CloudFront URL

0

I have a CloudFront distribution pulling content from a private S3 bucket and aliased to my website domain. Currently, I can access the website from its own domain, but also from the CloudFront distribution's URL. I want to allow access only to the website domain and deny the CloudFront URL. Can I do that?

질문됨 5달 전524회 조회
1개 답변
0
수락된 답변

Hello.

It is possible to block using CloudFront Functions.
For example, you can check the host part included in the request header using the code below, and if "cloudfront.net" is included, you can return HTTP status code 403.
https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/functions-tutorial.html

function handler(event) {
    var request = event.request;
    var host = request.headers.host.value;

    if (host.includes('cloudfront.net')) {
        return {
            statusCode: 403,
            statusDescription: 'Forbidden',
            body: {
                "encoding": "text",
                "data": "<html><head><title>403 Forbidden</title></head><body><center><h1>403 Forbidden</h1></center></body></html>"
            }
        };
    }

    return request;
}

I think it is also possible to block using custom rules using AWS WAF.
https://docs.aws.amazon.com/waf/latest/developerguide/classic-web-acl-rules-creating.html

profile picture
전문가
답변함 5달 전
profile picture
전문가
검토됨 5달 전
profile picture
전문가
검토됨 5달 전
profile pictureAWS
전문가
검토됨 5달 전
  • Thank you Riku! Just noting that "data" in the response body should only contain plain text, as html tags are displayed literally.

  • HTML tags may have shown because the encoding is set to text.

  • Thank you Gary! The Developer Guide says: "You can specify the encoding as plain text ("encoding": "text") or as Base64-encoded content ("encoding": "base64")." Both encodings show html tags literally. Is there another way? I tried "text/html", but I got a 503.

  • To force parsing html tags one needs to send the content-type header as well, so the correct function syntax is the following:

    function handler(event) { var request = event.request; var host = request.headers.host.value;

    if (host.includes('cloudfront.net')) { return { statusCode: 403, statusDescription: 'Forbidden', headers: { "content-type": { "value": "text/html" } }, body: { "encoding": "text", "data": "<html><head><title>403 Forbidden</title></head><body><center><h1>403 Forbidden</h1></center></body></html>" } }; }

    return request;
    

    }

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인

관련 콘텐츠