'Cloudfront removes Set-Cookie header from response to viewer

I have a NextJs application running in an AWS EC2 instance. The application includes static pages and APIs for login. Now, I am trying to setup AWS Cloudfront distribution. However, I am facing a challenge because CloudFront removes the Set-Cookie header from the login API Response and it's breaking the login functionality.

Below is the reference of the cache behavior setup in which I have disabled caching for the /api/* path.

Precedence Path pattern Cache policy name
0 /api/* Managed-CachingDisabled
1 Default (*) Managed-CachingOptimized

After referring AWS docs https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/Cookies.html, I have noticed that it's possible to forward cookies from viewer to origin.

But, it's still not clear to me,

  1. How to forward the Set-Cookie header from the origin response to viewer.
  2. Why CloudFront removes the Set-Cookie header for the path /api/* in which caching has been disabled.

Appreciate any help on this. Thanks.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source