If you're tired of dealing with load balancing, routing and cloud monitoring and would like to focus on just lambda function, list comprehension or class-based view, then this is the talk for you!
Heroku is the Platform as a Service (PaaS) platform that allows developers to create, run and manage applications entirely in the cloud. To be provided as a service not only the hardware, but also the platform that abstracts the hardware itself and allows you to take advantage of features that allow you to achieve automatic balancing, deployment management and more.
The advantage for the user is to focus exclusively on the development of the application without getting lost in the analysis of problems related to the environment in which it must be distributed, thus obtaining the necessary scalability and reliability.
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
Heroku pycon
1. Heroku: how to deploy a Django
app in less 10 minutes!
Sabatino Severino
sab@pentatechnology.it
Firenze
21/04/2018
2. About Me
● Python Developer & Data Scientist
● Deep Learning Analytics for Satellite
Imagery
● Official Pycon9 App Developer
sab@pentatechnology.it
@bsab
linkedin.com/in/severinosab
12. What is PAAS?
Platform-as-a-service enables developers
to create innovative applications without
operational overhead around
configuration, deployment and
management.
13. Cloud vendors manages
infrastructure.
You manage platforms
and apps
IaaS
Layers of infrastructure
Cloud vendors manages
infrastructure and
platforms.
You manage apps
PaaS
Cloud vendors manages
infrastructure, platforms
and apps.
You manage nothing...
SaaS
Infrastructure
Applications
Platform
Infrastructure
Applications
Platform
Infrastructure
Applications
Platform
Amazon EC2, Windows Azure,
Rackspace, Google Compute Engine
AWS Elastic Beanstalk, Windows Azure, Heroku,
Force.com, Google App Engine, Apache Stratos
Google Apps, Microsoft Office 365
14. Efficient, Secure, Elastic
● Applications co-located on a few-servers
● Drastically reduces resources
● Add/Remove servers depending on load
● All secure using SELinux or LXC
22. Heroku core language support
Virtually every language you can think of is supported by
community buildpacks
https://devcenter.heroku.com/articles/third-party-buildpacks
28. Preparing the application
● Add a Procfile in the project root
● Add requirements.txt file with all the requirements in the project root
● Add Gunicorn to requirements.txt
● A runtime.txt to specify the correct Python version in the project root
● Configure whitenoise to serve static files
32. PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.9/howto/static-files/
STATIC_ROOT = os.path.join(PROJECT_ROOT, 'staticfiles')
STATIC_URL = '/static/'
# Extra places for collectstatic to find static files.
STATICFILES_DIRS = (
os.path.join(PROJECT_ROOT, 'static'),
)
Set Up The Static Assets
Update the settings.py
33. import os
from django.core.wsgi import get_wsgi_application
from whitenoise.django import DjangoWhiteNoise
os.environ.setdefault("DJANGO_SETTINGS_MODULE",
"pycon9_demo.settings")
application = get_wsgi_application()
application = DjangoWhiteNoise(application)
Update the wsgi.py file:
STATICFILES_STORAGE = 'whitenoise.django.CompressedManifestStaticFilesStorage
Update the settings.py
WhiteNoise
http://whitenoise.evans.io/en/stable/
34. Set Up the Environment Variables
Update the settings.py
SECRET_KEY = config('SECRET_KEY')
DEBUG = config('DEBUG', default=False, cast=bool)
DATABASES = {
'default': dj_database_url.config(
default=config('DATABASE_URL')
)
}
● python-decouple: Strictly separate the settings parameters from your source code.
● dj-database-url: This simple Django utility allows you to utilize the 12factor inspired
DATABASE_URL environment variable to configure your Django application.
36. $ git clone https://github.com/bsab/pycon9_demo.git && cd pycon9_demo
1) clone the repository you want to deploy :
$ heroku login
2) login to Heroku using the CLI:
37. $ heroku create pycon9-demo
3) inside the project root, create the Heroku App:
Creating ⬢ pycon9-demo... done
https://pycon9-demo.herokuapp.com/ |
https://git.heroku.com/pycon9-demo.git
*You can omit the app name parameter (in my case, django-fieldbook), then Heroku
will pick a name for you.
38. $ heroku addons:create heroku-postgresql:hobby-dev
4) add a PostgreSQL database to your app
Creating postgresql-metric-21979... done, (free)
Adding postgresql-metric-21979 to pycon9_demo... done
Setting DATABASE_URL and restarting pycon9_demo... done, v4
Database has been created and is available
40. $ heroku run python manage.py migrate
6) migrate the database
Running python manage.py migrate on ⬢ pycon9_demo... up, run.9352
Operations to perform:
Apply all migrations: questions, sessions, authentication, articles, feeds,
contenttypes, messenger, activities, auth
Running migrations:
Rendering model states... DONE
Applying contenttypes.0001_initial... OK
Applying auth.0001_initial... OK
Applying questions.0001_initial... OK
Applying feeds.0001_initial... OK
Applying articles.0001_initial... OK
Applying activities.0001_initial... OK
Applying contenttypes.0002_remove_content_type_name... OK
Applying auth.0002_alter_permission_name_max_length... OK
Applying auth.0003_alter_user_email_max_length... OK
Applying auth.0004_alter_user_username_opts... OK
Applying auth.0005_alter_user_last_login_null... OK
Applying auth.0006_require_contenttypes_0002... OK
Applying auth.0007_alter_validators_add_error_messages... OK
Applying authentication.0001_initial... OK
Applying messenger.0001_initial... OK
Applying sessions.0001_initial... OK
41. And there you go!
https://pycon9-demo.herokuapp.com/
43. Wrap Up
● The application “sleeps” after 30 minutes of inactivity. The first access after it might
be slow.
● The free PostgreSQL database has a limitation of 10K rows.
● If you need to store user uploaded files (media), you will need a service to store the
files (Amazon S3)
44. So what?
Use Heroku:
● for small personal projects
● if you are an agency
● get started very quickly
● just want to play around
● a Free SSL Certificate!
45. Don’t use Heroku:
● if you can afford to have an expert in devops
● for production load with SLA’s
● additional control over your infrastructure