简体   繁体   中英

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.

You should use two elastic beanstalk environments: development & production. Push all your immediate changes to the development environment for testing. When you are ready to update the production environment (maybe once a day), use elastic beanstalk's swap environment URLs functionality. This will then direct all users to the newly updated environments while minimizing downtime.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM