Search code examples
gitamazon-web-servicesamazon-ec2flaskaws-code-deploy

Deploying Flask application on AWS without downtime


I have a Flask application running on an Amazon Web Services EC2 Ubuntu Server. When I update the code, I use git to push the latest code to my EC2 instance. However, as users are signed in and are most likely in the middle of something, they are signed out and taken to the sign in page.

As I tend to deploy to my application often this can be an issue. Is there a way using either AWS or Flask on the ubuntu server to prevent this issue? I heard about Amazon's CodeDeploy however, it looks like it only works with Elastic Beanstalk instances.

What can I do? Thanks.


Solution

  • CodeDeploy works with regular EC2 instances and actually won't work with Elastic Beanstalk instances. Try reading up on the CodeDeploy user guide for more information on how to use CodeDeploy.

    That still won't solve your actual problem, which is that you are storing session data in your webserver's local memory. If you need to start using multiple hosts behind a loadbalancer you will have the same problem even if you manage to save and reload the state between processes.

    There are two options that make sense here:

    1. Cookie-based sessions. You have to put everything you need to validate the login session in the cookie so that you can validate the session without checking local state (something other than the actual password please!). It's easy to do this insecurely and encrypting the cookie (so only your servers can read it) is a must.
    2. Shared session storage. Use memcached, redis, or something more persistent (but fast - possibly with local caching). Store the session state you would normally store locally in the shared cache. Here's a Flask Snippet for using Redis for session state.

    You can definitely use CodeDeploy to help manage and deploy these different components, but I think you need to look at how you are persisting sessions first.