nikisfree nikisfree - 3 months ago 23x
PHP Question

Session in Laravel 5.2 with AWS Elastic Beanstalk doesn't work with SESSION_DRIVER as cookie, file or database

I am trying to deploy a Laravel 5.2 app on AWS Elastic Beanstalk. Everything works fine, Except SESSIONS. Prior to deploying it on the Beanstalk, I had been working on the app on EC2, and I had no problems with sessions. For SESSION_DRIVER env option, I have tried database, cookie and file. They all worked on EC2 but none of them worked on Elastic Beanstalk.

After Googling and SOing a lot, I found out about Sticky Sessions. This is a setting you need to enable on the load balancer of your elastic beanstalk. This is available in the configuration dashboard of the elastic beanstalk.

So, I enabled Sticky Sessions, by choosing both, Use Load Balancer Generated Session AND Use App Generated Session option from the dropdown. Still the sessions don't work.

I have been looking around for other solutions but all routes point to Sticky Sessions.

Has anyone else faced this issue? If so, how did you solve it? Any help would be highly appreciated.


On speaking with AWS Support, they suggested the following working solution.

  1. In your web app root, create a folder called .ebextensions
  2. Open any text editor and paste the following policy

     Type: AWS::ElasticLoadBalancing::LoadBalancer
        -   InstancePort: 80
            InstanceProtocol: HTTP
            LoadBalancerPort: 80
            - CookiePolicy
            Protocol: HTTP
        -   CookieName: laravel_session
            PolicyName: CookiePolicy
  3. Save this file in the .ebextensions folder that you created in step 1.

  4. Redeploy the app to your beanstalk. Sessions should now be working