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