├── Procfile
├── .gitignore
├── requirements.txt
├── templates
└── hello.html
├── hellofly.py
└── README.md
/Procfile:
--------------------------------------------------------------------------------
1 | web: gunicorn hellofly:app
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | venv
2 | fly.toml
3 | .vscode
4 | __pycache__
--------------------------------------------------------------------------------
/requirements.txt:
--------------------------------------------------------------------------------
1 | click==8.1.3
2 | Flask==2.2.2
3 | gunicorn==20.1.0
4 | itsdangerous==2.1.2
5 | Jinja2==3.1.2
6 | MarkupSafe==2.1.1
7 | Werkzeug==2.2.2
8 |
--------------------------------------------------------------------------------
/templates/hello.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 | Hello from Fly
7 | {% if name %}
8 | and hello to {{name}}
9 | {% endif %}
10 |
11 |
12 |
--------------------------------------------------------------------------------
/hellofly.py:
--------------------------------------------------------------------------------
1 | from flask import Flask, render_template
2 |
3 | app = Flask(__name__)
4 |
5 | @app.route('/')
6 | @app.route('/')
7 | def hello(name=None):
8 | return render_template('hello.html', name=name)
9 |
10 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Python-hellofly-flask
2 |
3 | In our Hands-On section, we show how to deploy a deployable image file using Flyctl. Now we are going to deploy an application from source. In this _Getting Started_ article, we look at how to deploy a Python application on Fly.
4 |
5 | ## _The Hellofly-python Application_
6 |
7 | You can get the code for the example from [the Fly-Examples Github repository](https://github.com/fly-examples/python-hellofly-flask). Just `git clone https://github.com/fly-examples/python-hellofly-flask` to get a local copy.
8 |
9 | The Python hellofly application is, as you'd expect for an example, small. It's a Python application that uses the [Flask](https://flask.palletsprojects.com/) web framework. Here's all the code form `hellofly.py`:
10 |
11 | ```python
12 | from flask import Flask, render_template
13 |
14 | app = Flask(__name__)
15 |
16 | @app.route('/')
17 | @app.route('/')
18 | def hello(name=None):
19 | return render_template('hello.html', name=name)
20 | ```
21 |
22 | Flask is set up to route request to a `hello` function which in turn passes a `name` value (taken from the requests path)to a function to render a template. The template resides in the `templates` directory under the name `hello.html`. It too is very simple too:
23 |
24 | ```html
25 |
26 |
27 |
28 |
29 |
30 | Hello from Fly
31 | {% if name %}
32 | and hello to {{name}}
33 | {% endif %}
34 |
35 |
36 | ```
37 |
38 | We're using a template as it makes it easier to show what you should do with assets that aren't the actual application.
39 |
40 | You will need to install Flask itself, or at least set up virtual environments as recommended in the [Flask Install](https://flask.palletsprojects.com/en/1.1.x/installation/#virtual-environments) guide.
41 |
42 | Once you have activated the virtual environment, run:
43 |
44 | ```
45 | python -m pip install -r requirements.txt
46 | ```
47 |
48 | Which will load Flask and other required packages. One of those packages will be `gunicorn` which isn't a Flask dependency, but will be used when we deploy the app to Fly.
49 |
50 | ## _Testing the Application_
51 |
52 | Flask apps are run with the `flask run` command, but before you do that, you need to set an environment variable `FLASK_APP` to say which app you want to run.
53 |
54 | ```cmd
55 | FLASK_APP=hellofly flask run
56 | ```
57 | ```out
58 | * Serving Flask app "hellofly"
59 | * Environment: production
60 | WARNING: This is a development server. Do not use it in a production deployment.
61 | Use a production WSGI server instead.
62 | * Debug mode: off
63 | * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
64 | ```
65 |
66 | This will run our hellofly app and you should be able to connect to it locally on localhost:5000.
67 |
68 | Now, let's move on to deploying this app to Fly.
69 |
70 | ## _Install Flyctl and Login_
71 |
72 | We are ready to start working with Fly and that means we need `flyctl`, our CLI app for managing apps on Fly. If you've already installed it, carry on. If not, hop over to [our installation guide](/docs/getting-started/installing-flyctl/). Once that is installed you'll want to [login to Fly](/docs/getting-started/login-to-fly/).
73 |
74 | ## _Configure the App for Fly_
75 |
76 | Each Fly application needs a `fly.toml` file to tell the system how we'd like to deploy it. That file can be automatically generated with the command `flyctl launch` command. We are going to use one of Fly's builtin deployment configurations for Python.
77 |
78 | ```cmd
79 | flyctl launch
80 | ```
81 | ```output
82 | Creating app in //python-hellofly-flask
83 | Scanning source code
84 | Detected a Python app
85 | Using the following build configuration:
86 | Builder: paketobuildpacks/builder:base
87 | Selected App Name:
88 | ? Select region: lhr (London, United Kingdom)
89 | Created app hellofly-flask in organization personal
90 | Wrote config file fly.toml
91 | We have generated a simple Procfile for you. Modify it to fit your needs and run "fly deploy" to deploy your application.
92 | ```
93 |
94 | You'll be asked for an application name first. We recommend that you go with the autogenerated names for apps to avoid namespace collisions. We're using `hellofly-python` here so you can easily spot it in configuration files.
95 |
96 | Next you'll be prompted for an organization. Organizations allow sharing applications between Fly users. When you are asked to select an organization, there should be one with your account name; this is your personal organization. Select that.
97 |
98 | Now `flyctl launch` will generate a sample `Procfile` and `fly.toml`, which together will define how fly deploys and launches the application.
99 |
100 | Update the `Procfile` to look like this:
101 |
102 | ```Procfile
103 | web: gunicorn hellofly:app
104 | ```
105 |
106 | This says the web component of the application is served by `gunicorn` (which we mentioned earlier when talking about dependencies) and that it should run the `hellofly` Flask app.
107 |
108 | ## _Inside `fly.toml`_
109 |
110 | The `fly.toml` file now contains a default configuration for deploying your app. In the process of creating that file, `flyctl` has also created a Fly-side application slot of the same name, `hellofly-python`. If we look at the `fly.toml` file we can see the name in there:
111 |
112 | ```toml
113 | # fly.toml file generated for hellofly-python on 2021-11-30T17:37:33+02:00
114 |
115 | app = "hellofly-python"
116 |
117 | kill_signal = "SIGINT"
118 | kill_timeout = 5
119 | processes = []
120 |
121 | [build]
122 | builder = "paketobuildpacks/builder:base"
123 |
124 | [env]
125 | PORT = "8080"
126 |
127 | [experimental]
128 | allowed_public_ports = []
129 | auto_rollback = true
130 |
131 | [[services]]
132 | http_checks = []
133 | internal_port = 8080
134 | processes = ["app"]
135 | protocol = "tcp"
136 | script_checks = []
137 |
138 | [services.concurrency]
139 | hard_limit = 25
140 | soft_limit = 20
141 | type = "connections"
142 |
143 | [[services.ports]]
144 | handlers = ["http"]
145 | port = 80
146 |
147 | [[services.ports]]
148 | handlers = ["tls", "http"]
149 | port = 443
150 |
151 | [[services.tcp_checks]]
152 | grace_period = "1s"
153 | interval = "15s"
154 | restart_limit = 0
155 | timeout = "2s"
156 | ```
157 |
158 | The `flyctl` command will always refer to this file in the current directory if it exists, specifically for the `app` name/value at the start. That name will be used to identify the application to the Fly service. The rest of the file contains settings to be applied to the application when it deploys.
159 |
160 | We'll have more details about these properties as we progress, but for now, it's enough to say that they mostly configure which ports the application will be visible on.
161 |
162 | ## _Deploying to Fly_
163 |
164 | We are now ready to deploy our app to the Fly platform. At the command line, just run:
165 |
166 | ```cmd
167 | flyctl deploy
168 | ```
169 |
170 | This will lookup our `fly.toml` file, and get the app name `hellofly-python` from there. Then `flyctl` will start the process of deploying our application to the Fly platform. Flyctl will return you to the command line when it's done.
171 |
172 | ## _Viewing the Deployed App_
173 |
174 | Now the application has been deployed, let's find out more about its deployment. The command `flyctl status` will give you all the essential details.
175 |
176 | ```cmd
177 | flyctl status
178 | ```
179 | ```output
180 | App
181 | Name = hellofly-python
182 | Owner = demo
183 | Version = 0
184 | Status = running
185 | Hostname = hellofly-python.fly.dev
186 |
187 | Deployment Status
188 | ID = 0cdc72fe-3db9-aa52-eb84-5c3552053b1e
189 | Version = v0
190 | Status = successful
191 | Description = Deployment completed successfully
192 | Instances = 1 desired, 1 placed, 1 healthy, 0 unhealthy
193 |
194 | Instances
195 | ID VERSION REGION DESIRED STATUS HEALTH CHECKS RESTARTS CREATED
196 | 0530d622 0 lhr run running 1 total, 1 passing 0 40s ago
197 | ```
198 |
199 | As you can see, the application has been with a DNS hostname of `hellofly-python.fly.dev`, and an instance is running in London. Your deployment's name will, of course, be different.
200 |
201 | ## _Connecting to the App_
202 |
203 | The quickest way to connect to your deployed app is with the `flyctl open` command. This will open a browser on the HTTP version of the site. That will automatically be upgraded to an HTTPS secured connection (for the fly.dev domain).
204 |
205 |
206 | to connect to it securely. Add `/name` to `flyctl open` and it'll be appended to the URL as the path and you'll get an extra greeting from the hellofly-python application.
207 |
208 |
209 | ## _Bonus Points_
210 |
211 | If you want to know what IP addresses the app is using, try `flyctl ips list`:
212 |
213 | ```cmd
214 | fly ips list
215 | ```
216 | ```out
217 | TYPE ADDRESS CREATED AT
218 | v4 213.188.195.22 2m1s ago
219 | v6 2a09:8280:1:502e:4ce8:6058:7f09:62a 1m58s ago
220 | ```
221 |
222 | And you can always run `flyctl` as `fly` if you want to save a few keystrokes.
223 |
224 | ## _Arrived at Destination_
225 |
226 | You have successfully built, deployed, and connected to your first Python application on Fly.
227 |
--------------------------------------------------------------------------------