5

As the number of instances changes from time to time in Amazon Elastic Beanstalk. If an application is built in PHP how the session will be maintained among several instances? How can we make sure not to delete the Parent instance (The first instance when the app was start

study
  • 5,628
  • 4
  • 36
  • 44
  • We map and elastic IP to a particular instance, then we map our web domain to that elastic IP, what if that particular instance get deleted.. –  Sep 09 '13 at 15:36
  • 1
    The EIP will get disassociated with your terminated instance. Usually I just map web domain CNAME to Elastic Beanstalk Environment URL, see [Using Custom Domains](http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/customdomains.html). – study Sep 09 '13 at 18:49
  • @study and it automatically get mapped to new Instance? –  Sep 09 '13 at 19:53
  • No. It will not get mapped to new instance. – study Sep 09 '13 at 20:07
  • @study then what is the solution for it? –  Sep 09 '13 at 20:30
  • Do not assign EIP to your instance(This should be used only when you need to login specific instance). The solution is as above I said, just mapping web domain CNAME to Elastic Beanstalk Environment URL. – study Sep 09 '13 at 20:41
  • @study i can point to an IP not to an address like pro-elasticbean.com etc –  Sep 09 '13 at 21:22
  • There is no good solution for such case. In Elastic Beanstalk Environment, Elastic Load Balancing do not have a fixed IP and EC2 instances will be started and terminated by Auto Scaling. Maybe you can consider renting a domain name. – study Sep 09 '13 at 21:41
  • @study i have a domain say www.example.com i can point this domain to an IP address like 54.22.88.102 by changing its DNS but I cant point it to env-elasticbean.com (Elastic Beanstalk Environment URL).(this is standard and happen everywhere) –  Sep 09 '13 at 21:51
  • Possible duplicate of [AWS Elastic Beanstalk and PHP sessions](http://stackoverflow.com/questions/20478795/aws-elastic-beanstalk-and-php-sessions) – Simon East Feb 19 '16 at 06:45

7 Answers7

9

This post on the AWS blog describes how to use DynamoDB to do session state handling using the AWS SDK for PHP.

http://aws.typepad.com/aws/2012/04/scalable-session-handling-in-php-using-amazon-dynamodb.html

Jim Flanagan
  • 2,129
  • 15
  • 19
  • Elastic Bean stalk doesn't use dynamo at all –  Sep 08 '13 at 17:23
  • 1
    @user1765876, you can setup Elastic Beanstalk to use DynamoDB. See [AWS Elastic Beanstalk Developer Guide](http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/customize-environment-resources-dynamodb.html) – study Sep 08 '13 at 20:26
  • 1
    Jim is right. You can create a DynamoDB table, then use the DynamoDB Session Handler in AWS SDK for PHP to use DynamoDB has the backend for the `$_SESSION` superglobal. – Ryan Parman Sep 10 '13 at 16:12
  • How to access session variable in Wordpress http://silvermapleweb.com/using-the-php-session-in-wordpress/ – nu everest Dec 05 '16 at 19:26
3

If an application is built in PHP how the session will be maintained among several instances?

No. Elastic Beanstalk will not maintain session among several instances. You must maintain session among several instances by yourself. If you enable Elastic Load Balancing Session Stickiness, it will only make sure to forward your user to the same instance.

How can we make sure not to delete the Parent instance (The first instance when the app was start

Elastic Beanstalk Auto Scaling is use Default Termination Policy which will terminate oldest instance first. You can modify Elastic Beanstalk Auto Scaling Termination Policy to avoid terminating oldest instance first.

study
  • 5,628
  • 4
  • 36
  • 44
  • what does that mean Elastic Load Balancing Session Stickiness? by deafult is is disable.. –  Sep 09 '13 at 11:39
  • 1
    hi @user1765876, please see [Elastic Load Balancing Developer Guide - Create Sticky Sessions](http://docs.aws.amazon.com/ElasticLoadBalancing/latest/DeveloperGuide/US_StickySessions.html) – study Sep 09 '13 at 11:54
  • i am still confused, by default stickness feature is disabled, so should I enable it? what If I don't enable it? –  Sep 09 '13 at 13:47
  • 1
    If you enable it, your user request will be directed to the same EC2 instance. So you do not pay attention to to session replication problem. If you disable it, Elastic Load Balancing will round robin choose an EC2 instance to serve your user request. But some EC2 instances do not have user session, your user may get logout of your system. – study Sep 09 '13 at 14:02
  • But Scaling in(decrease EC2 instances) may also cause your users to lose state. – study Sep 09 '13 at 15:10
2

Here is a tutorial for how to configure persistent sessions using DynamoDB and Tomcat. https://packageprogrammer.wordpress.com/2014/01/13/aws-elastic-beanstalk-and-dynamodb-session-manager-for-tomcat/

AWS Project: https://github.com/aws/aws-dynamodb-session-tomcat

Manage Tomcat Session State with DynamoDB: http://docs.aws.amazon.com/sdk-for-java/v1/developer-guide/java-dg-tomcat-session-manager.html

Key Excerpt from the links above. Note that you may have change version numbers since the tutorial uses Tomcat 7 and 1.0.1 of aws-dynamodb-session-tomcat

Configuration for Tomcat

Download AmazonDynamoDBSessionManagerForTomcat-2.0.4.jar from aws-dynamodb-session-tomcat releases the and copy to the lib directory of your Tomcat installation Edit context.xml to use the .jar with code:

<?xml version='1.0' encoding='utf-8'?>
<Context>
    <WatchedResource>WEB-INF/web.xml</WatchedResource>
    <Manager className="com.amazonaws.services.dynamodb.sessionmanager.DynamoDBSessionManager"
             createIfNotExist="true" />
</Context>

Configuration for Elastic Beanstalk

Download AmazonDynamoDBSessionManagerForTomcat-2.0.4.jar from aws-dynamodb-session-tomcat releases the and copy into .ebextensions directory of your WAR Edit context.xml with the code below and copy into .ebextensions directory of your WAR In the .ebextensions directory (Remember it must be inside /WEB-INF directory, /web-app for grails applications), create a .config file to write the command to execute the replacement of context.xml and copy the jar inside Beanstalk instances.

container_commands:
  01-copy-aws-library:
    command: "cp WEB-INF/.ebextensions/AmazonDynamoDBSessionManagerForTomcat-2.0.4.jar.jar /usr/share/tomcat8/lib/"
  02-replace-tomcat-context:
    command: "cp WEB-INF/.ebextensions/context.xml /etc/tomcat8/context.xml"

Now, just upload and deploy the war with the changes. Try login and go into AWS Console->Services->DynamoDB, look for a table with name Tomcat_SessionState and voilà! You have affinity and persistence!

nu everest
  • 9,589
  • 12
  • 71
  • 90
1

From personal experience I would suggest moving sessions to the database (RDS|DynamoDB|SimpleDB) or cache server (AWS started supporting Redis recently). It would take a lot of headache when application starts to scale or get terminated by unsuccesful health check.

0

Better option to use Redis on Elastic Cache in order to store session. One t1.micro node available on free tier and maintainable by Amazon, so you can get stable, cheep and fast storage for sessions, shared between nodes in autoscalling environment. How to configure PHP for keeping sessions in Redis I guess you can find in internet.

Option with DynamoDB can be expensive, because you will pay for each call to DynamoDB services, also you need take into account that AWS services exposed by HTTP(S) can throttle requests.

Vadym Fedorov
  • 2,395
  • 2
  • 21
  • 31
0

I think the only way to manage this is to store your sessions in DynamoDB or similar. We had the exact same problem when we deployed a java based app on Tomcat in the Elastic Bean Environment. When the load balancer used to scale down instances, any user attached to that instance would automatically log out. Moving sessions to DynamoDB solved this problem for us.

sanket
  • 789
  • 4
  • 16
0

When using Elastic Beanstalk with a typical Java webapp, I think you will definitely want to enable session stickiness. Otherwise each HTTP request from a user's browser could be routed to a different server.

To get around the issue of the user's session being destroyed when the server they are "stuck" to gets shut down you would need to look into Tomcat session replication. This isn't something that Elastic Beanstalk comes with out of the box unfortunately, so in order to setup session replication you would have to create a custom Elastic Beanstalk AMI for your application to use. Also, you would have to use an implementation of Tomcat session replication that does not rely on multicast, since multicast isn't available on AWS, or any other cloud environment that I know of. An example of an implementation that doesn't rely on multicast would be one that uses a database (such as Amazon RDS) or memcached server (such as Amazon Elastic Cache) to make the sessions available across multiple Tomcat instances.

Also note that the Elastic Beanstalk UI only allows you to enable load balancer-generated HTTP cookies. However after Elastic Beanstalk has created the load balancer, you can go into the EC2 console and modify the load balancer's settings to switch it to application-generated HTTP cookies, and then tell it to use the "JSESSIONID" cookie.