Modern Python Projects Transcripts
Chapter: Deployment
Lecture: Deploying to Heroku

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Let's deploy our application to Heroku. As I said, Heroku is more expensive than other options in terms of how much you pay for a Web server.
0:11 But it's a platform as a service. So if you're not very familiar with Linux and setting up servers,
0:16 it's a perfect solution because it takes away all the difficult stuff.
0:21 They have a Web interface that you can use to connect your application from GitHub. They have a really good documentation.
0:27 And for advanced users, they have a command line interface app,that you can install and used to deploy or manage your application from the terminal.
0:39 And even though I said they're a bit more expensive than other solutions, they also offer a free tier that is perfect for a small project.
0:48 So let's dive in. Once you create an account with Heroku and you lock in you will see this interface or a similar one.
0:57 If you're watching this course in the future, if you scroll a bit down, you can see the documentation for your language. But if we go for Python,
1:08 you will see a getting started guide that explains how to basically prepare and deploy your application. If you go to the setup.
1:20 You can see that this guide is using the Heroku cli app and later on you can, for example, use the command line interface to,
1:28 build the logs to scale your application and so on. But for a much simple project, you can actually connect Heroku to,
1:36 GitHub and use the Web interface to deploy your application. So, let's do that. So here we have to click. Create new app. We can name it,
1:44 Somehow, we can choose the region. Since I'm based in Europe, I will go for Europe. Just make it a bit faster for me.
1:55 Here you can click this option add to Pipeline, and this pipeline is used for the continues delivery, so you can set up different steps
2:04 for different parts of your CD pipelines. The best example here would be to.
2:10 Have a staging server where you automatically deploy code for your application, and once you see that everything is working fine,
2:18 you will have a different production server where you manually deploy stuff. Since I don't have a CD pipeline, I'm going to skip this step and
2:27 I just click create app. Now we have to specify how we're going to deploy
2:32 our application. We have three main options first to use the Heroku cli app that I showed you a few moments ago. Then we can use the GitHub.
2:42 So this is what we are going to use. And also we could deploy a docker image also using the Heroku Cli. So, let's connect our GitHub account.
2:53 If this is the first time you're using Heroku, it will ask you for permission from GitHub.
2:57 So you have to give access and then you should be able to search for a
3:01 repositoy. I'm using a personal repositoy and I call this project Uptime your website. So here it is, I click connect. And now we have two options.
3:17 We can enable automatic deploys. So each time we push a new commit to a specified branch, it's going to automatically Deploy your application.
3:27 All right. We can do a manual deploy, so we will have to every time click this button if we want to deploy a new version.
3:35 But how does Heroku knows how to deploy our application? I mean, what kind of Web server use or whether to use the database or
3:43 not? Well, all that is specified in a file called procfile that you have to add to your repositoy. So let's do that now.
3:52 We create a new procfile, and as you can see, it even has the Heroku icon here, and here We specify that we want to have a Web worker,
4:02 and this Web worker will start a gunicorn server with three workers using the Uvicorn worker type of a worker.
4:12 And we're pointing it to the app from the main.py file. If you're curious, I basically copied this piece of code from the documentation.
4:21 So, now we can save it and commit this file to our repositoy. And then we go back to the Heroku, UI to deploy our application.
4:44 So, now we can click Deploy branch, and underneath you can see the progress. It will take a bit of time to install all the dependencies,
4:56 but we should be done soon. Cool. So, as you can see here we have our uptimer-website.heroku app and your app was successfully deployed.
5:09 We can click this view button and here it is. And as you can see, it's working cool. What if I break it?
5:27 Yep, they're working. So, that's how easily you can use Heroku to deploy your application. And just for the fun. Let's enable the automatic deploys and
5:37 let's make some changes to see how they automatically applied to the heroku app. So we click this and we go back to the code.
5:47 Let's change something in the template,
6:05 and if we go back to the interface, we should somewhere see that, it's building our application. Let's refresh if it's not here,
6:16 let's go to the activity. And here you can see the build automatically started. cool, we can click view build progress and we'll see the same output.
6:28 If something goes wrong and you're not using the Heroku cli app to view the logs you can click to view logs here to see the logs from your Web
6:36 server. As you can see, we have the same output as we had in the terminal, so our app should be up and running. Let's go back here, deployed cool.
6:48 We can click open app, and as you can see now, the online is upper case, so that's really cool.
6:55 So basically we are using our free tier to deploy our FastAPI to Heroku I didn't even have to put my credit card anywhere one last thing.
7:05 So, our application is pretty simple. We don't have a database or anything,
7:10 but as your application grows, you probably will have to add more things like, Well, first of all, a database.
7:16 But then maybe a ready server and stuff like that. So Heroku offers ah, lot of addons. You can go to this, find more addons Page,
7:26 and here you can see basically any kind of service that you might need for your application. You have, like different versions of redis,
7:35 different versions of databases. Here there is, like the postgres version offered by Heroku and most of those addons, They have, like a free tier.
7:46 If you go down here, you can see that for free. You get like database with 10,000 rows,
7:54 which is not really much, but well enough to test your application. But then, as you can see, the pricing goes up very fast.
8:08 So, that's basically how you would use Heroku to deploy your application. You can use the Web ui or you can use the command line tool and
8:16 then you can enable more addons as you go. In the next lessons will compare this to using a docker image.


Talk Python's Mastodon Michael Kennedy's Mastodon