Skip to content

Instantly share code, notes, and snippets.

@ecgill
Last active February 16, 2018 21:32
Show Gist options
  • Save ecgill/44a1eb94e8b6c4dc2d163ec851d64176 to your computer and use it in GitHub Desktop.
Save ecgill/44a1eb94e8b6c4dc2d163ec851d64176 to your computer and use it in GitHub Desktop.
Updating a Flask App (via GitHub repo) on EC2 instance

Running a Flask App on EC2 using tmux

Quick guide to running a Flask app by cloning your GitHub repository onto an EC2 instance and how to update the repository to make changes to your app

By Emily Gill

Note: These instructions start after you have already created a Flask app inside of a GitHub repository. Also assumed is that you know how to launch an EC2 instance.

Part 1: Create web app and EC2 instance.

  1. Make sure that your web app is running locally. Assuming your Flask app script is called app.py, run the following from the command line to check:
$ python app.py
  1. Before going further, make sure Flask web app directory is running on your GitHub page.

  2. Launch a new EC2 instance. Some important notes: (1) Set inbound rules and remember the port you specify, and (2) don't forget to download a new key-pair, save it into your .ssh folder, and make a config file:

    $ mv ~/Downloads/my_key.pem ~/.ssh/my_key.pem
    $ cd ~/.ssh
    $ chmod 400 my_key.pem
    $ touch config
    
  3. Get the details of your instance to add to your config file. Note that the Host is the name of the instance you'll ssh into. The Hostname comes from the IPv4 address on the bottom right of side of the page with your instance details.

    Host web_app
      Hostname 54.160.14.102
      User ubuntu
      IdentifyFile ~/.ssh/my_key.pem
    

Part 2: Connect to your EC2 instance, clone your web app and get it running

  1. ssh into your web app. You may have to confirm that you want to continue connecting and you may be given the option to check for updates - 'yes' for both.

    $ ssh web_app
    
  2. Double check that you are logged onto an ubuntu server. Command line should look something like: [ubuntu:~]$

  3. Copy the address of your GitHub repo that contains your Flask app and clone it onto your EC2 instance.

    $ git clone https://github.com/your_username/web_app_dir.git
    
  4. Modify your flask app's port to be consistent with the port you specified when you launched your instance, and add threading. Here is a simple Flask web app example:

    from flask import Flask, render_template
    app = Flask(__name__)
    
    # home page
    @app.route('/')
    def index():
      return render_template('index.html')
    
    if __name__ == '__main__':
      app.run(host='0.0.0.0', port=8105, threaded=True)
    
    
  5. Start a tmux session.

  6. Run your app in your tmux session:

    [ubuntu:~/web_app_dir]$ python my_app.py
    
  7. At this point, you will want to detach from the tmux session, but leaving the tmux session (and your web app) running. Do this by hitting [Ctrl-b d].

  8. Your app should be deployed! Find the Public DNS (IPv4) address from the lower left corner of the screen that has your AWS EC2 instance details and then add your port number to the end of the DNS. It should be something like ec2-##-###-##-###.compute-1.amazonaws.com:8105/#

Part 3: Pulling changes made to your web app from your GitHub repo onto your EC2 & tmux

Assume you have made changes to your web app locally, and have pushed those changes up to your GitHub. Your flask app on EC2 is still running the old cloned version of your GitHub, so you must pull the updated version of your GitHub web app repo onto your EC2 instance and then re-start the tmux session. Here's how to do that.

  1. Make sure master GitHub version is correct version.

  2. ssh into your EC2 instance (same as before)

  3. cd into the cloned web app directory

  4. Pull the changes

    [ubuntu:~/web_app_dir]$ git pull
    
  5. Now that the directory is updated, we need enter the existing tmux, terminate running flask app, then re-run the app, then detach (but leave running) from the tmux session.

    [ubuntu:~/web_app_dir]$ tmux a -t 0
    (hit [Ctrl-c] to stops the flask app)
    [ubuntu:~/web_app_dir]$ python app.py
    (hit [Ctrl-b, d] to detach from tmux)
    

Citations / Resources

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment