- Newest
- Most votes
- Most comments
I think I found the problem. I've been testing this with Postman. Someone copied a POST request in Postman and changed it to a GET request, but did not delete the post body. The presence of a post body in a GET request caused CloudFront to reject the request. Clearing the post body fixed it.
Instead of cloudfront, have you considered using an ALB with a certifcate attached instead?
What do you see in the web server logs on your EC2 instance? Are you seeing requests with the Authorization header coming from CloudFront?
I'm seeing the header but the header name is being changed to all lowercase letters. So instead of "Authorization" I'm getting "authorization". This is why my application isn't reading the header. Why is this happening?
This is probably not the problem. Ok, when I send the request through CloudFront I get all of the same headers that I get when I send it directly to the server, plus three additional ones called "x-forwarded-for", "via", and "x-amz-cf-id". I don't have any reason to believe that any of these extra headers is a problem. It doesn't make any sense. The authorization header is identical in both requests.
The CloudFront error log reports "InvalidRequest text/html". This is curious because the request content type is supposed to be application/json. Is CloudFront blocking or modifying the Content-Type header?
Hello,
to fix the issue with CloudFront and the Authorization header:
Ensure your cache policy includes the Authorization header. Use a policy that forwards all headers, cookies, and query parameters. Apply the custom cache and origin request policies to your CloudFront distribution.
CloudFront itself does not handle authorization directly like a traditional web server. Instead, it's typically used as a content delivery network (CDN) to distribute static and dynamic content globally with low latency. However, it can work with authorization mechanisms when used in conjunction with other AWS services or custom setups. Here are some common approaches to handle authorization with CloudFront:
-
Using AWS Lambda@Edge:
- Lambda@Edge allows you to run AWS Lambda functions at CloudFront edge locations, which can inspect and modify requests and responses in real-time. You can implement custom authorization logic using Lambda@Edge to check authorization headers, tokens, or cookies before allowing access to content served by CloudFront.
-
Origin Access Identity (OAI):
- You can restrict access to your CloudFront distribution so that it only fetches content from an Amazon S3 bucket or an HTTP server that you specify. By using an OAI, you ensure that requests made through CloudFront are authenticated based on permissions configured on the origin (like an S3 bucket).
-
Signed URLs or Signed Cookies:
- CloudFront supports serving content using signed URLs or signed cookies. This approach generates URLs or cookies with limited expiration times and permissions, which can be used to grant temporary access to content based on the presence of a valid signature.
-
Use of Custom Headers:
- You can configure CloudFront to forward custom headers to your origin. For example, if your origin server performs authentication based on specific headers (like an authentication token), you can configure CloudFront to pass these headers along with the request.
-
Authorization at the Origin:
- Implementing authorization directly at your origin (e.g., web server or application server) and ensuring that CloudFront only serves authorized content based on the responses from the origin server.
Example Scenario Using Lambda@Edge:
Let's say you have an application where users must be authenticated to access certain content. Here's how you might use Lambda@Edge with CloudFront for authorization:
- Configure your CloudFront distribution to use Lambda@Edge for viewer request events.
- Write a Lambda function that checks incoming requests for an authorization token or cookie.
- If the authorization token or cookie is valid (i.e., matches a session or is signed correctly), allow the request to proceed.
- If the authorization token or cookie is missing or invalid, return a 401 Unauthorized response or redirect to a login page.
Lambda@Edge functions are deployed globally across AWS's network of edge locations, ensuring that authorization checks are performed close to the user, which helps reduce latency.
Considerations:
-
Cost: Using Lambda@Edge incurs costs for both the Lambda invocations and data transfer. Ensure to review AWS pricing for Lambda@Edge and CloudFront data transfer.
-
Performance: Lambda@Edge can add a small additional latency to requests due to the time taken to execute the function at the edge location. However, this latency is generally minimal due to the proximity of the edge locations to the end users.
By leveraging these methods, you can effectively handle authorization and access control while using CloudFront to distribute your content globally with low latency. Each approach has its nuances and suitability depending on your specific use case and security requirements.
Relevant content
- AWS OFFICIALUpdated 5 months ago

No. I don't know what an ALB is.
Do you mean a load balancer?
ALB is Application Load balancer..
Thanks, I think that is worth a try. I didn't realize you could use certificates with load balancers.
I don't believe you can with network load balancers, but application load balancers operate at layer 7/HTTP and support both certs for HTTPS as well as WAF firewall rules.