How do I ensure CloudFront returns cookies from my Elastic Beanstalk REST API application

Cookies are blocked by default

Making them work requires two changes, in the behavior settings:

  1. Allowing cookies to pass through (on the request)
  2. Letting the Set-Cookie header come back (with the response)

Where to change cookie settings...

cloudfront ui for cookies

Confused? If so, then...

Here's the step-by-step process...

In your CloudFront distribution:

  1. Go to the Behaviors tab
  2. Select the behavior and click Edit
  3. Find the Cache key and origin requests section
  4. Under Origin request policy click on Create policy
  5. Fill in the Name, and then scroll to the bottom and change Cookies from None to All (or Include Specified Cookes, if you want to whitelist)
  6. Hit Create to save the new policy
  7. Switch back to the original tab (with the Edit behavior page)
  8. Find the Cache key and origin requests section again
  9. Under Response headers policy, tap on Create policy
  10. Fill in the Name, and then Scroll to the bottom
  11. Under Custom headers, tap the Add header button
  12. In the Name field put Set-Cookie
  13. Leave Value empty, and Origin override should stay unchecked
  14. Hit the Create button
  15. Go back to the Edit behavior page, hit the refresh buttons, so you can pick those two new policies
  16. Hit Save Changes and give it a few minutes to deploy

You need to forward cookies in the cache behavior. Without it, CloudFront removes the Set-Cookie header from the response.

Here's the documentation:

You can configure each cache behavior to do one of the following:

...

Don’t forward cookies to your origin – CloudFront doesn’t cache your objects based on cookie sent by the viewer. In addition, CloudFront removes cookies before forwarding requests to your origin, and removes Set-Cookie headers from responses before returning responses to your viewers.