💡If you want an even BETTER way to host your FastAPI, check out my video on how to use FastAPI with AWS Lambda: ua-cam.com/video/RGIM4JfsSk0/v-deo.html This is serverless and in most cases is superior to EC2 in terms of scaling and cost.
for anyone wondering how to save and quit you'll first need to return to command mode. You can tell you're in insert mode if you see -INSERT- at the bottom of the editor. To exit insert mode and go back to command mode, press the Esc (Escape) key on your keyboard. Once you are in command mode, you can save and quit by typing :wq. This command is a combination of :w (which saves the file) and :q (which quits the editor). Press Enter after typing :wq to execute the command.
by the way guys, if you connect directly using aws interface, and you struggle to save the vim file where he adds the proxy pass and stuff. here is chatgpt answer to do operations for the vim file : vim has a steeper learning curve but is very powerful: Start in 'normal' mode (where you can navigate and use commands, but not type text). To enter 'insert' mode (where you can type text): Press i To save: First return to 'normal' mode by pressing ESC, then type :w and press Enter To exit without saving: Press ESC then type :q! and press Enter To save and exit: Press ESC then type :wq and press Enter Copy (called 'yanking' in vim): In normal mode, move to the line you want to copy and press yy Paste (called 'putting' in vim): Move to where you want to paste and press p Delete a line: In normal mode, press dd
Great video!!! You even cleared a lot of fundamentals for me! I believe the same concepts will work for any ubuntu vps right ? Other than an ec2 instance ? thanks!
I'm using vultr, installed nginx and configured as shown. It just doesn't work for me, no matter how I edit this, the url just says that page isn't working... any idea?
Thanks for the recommendation. I haven't actually used that stack much so I doubt I'll be able to make a good video on it, but I'll add it to my list of things to learn.
Good point. You'd have to set up a CICD workflow. I recommend GitHub actions. Here's a new video I just made on it: ua-cam.com/video/9uMcN66mfwE/v-deo.html The video doesn't look at EC2 specifically (I prefer Lambda for compute), but the concept's pretty much the same.
That's because the Fast API process also terminates when you disconnect from the shell. You have to run it in the background. NGINX should have a way to do that for you - but if it doesn't, then you can try other ways to run it in the background (or as a Daemon). I don't have a video on it yet, but maybe something like this from the command: stackoverflow.com/questions/44222883/run-a-shell-script-and-immediately-background-it-however-keep-the-ability-to-in Or there are some more options here: stackoverflow.com/questions/1603109/how-to-make-a-python-script-run-like-a-service-or-daemon-in-linux
Please let me know how to solve 404 error. Nginx not found and Bad gateway error. Im getting 404 error if i try to run the instance after couple of days of deployment.
I am having problem in nginx part, when I try to close vim there, it doesn't close directly, It says either write a command or press enter, and takes me back to the same interface where I can edit the file
You can run the command with `&` at the end (on any Unix based terminal) and that should prevent the terminal from shutting down the process when it exits. They can continue to run in the background. But yes, if you want to go to production with this you'll need to quite a bit more work.
As of 2023 (when I'm typing this) EC2 has a 1-year free tier for the micro instance type: aws.amazon.com/free/ But if you use serverless hosting (Lambda) you can get something like 1 million free requests per month, which is much better in my opinion: ua-cam.com/video/RGIM4JfsSk0/v-deo.html
thank you for your help but I have a question in my case when I try to do "sudo service nginx restart" or "sudo service nginx start" Job for nginx.service failed because the control process exited with error code. See "systemctl status nginx.service" and "journalctl -xeu nginx.service" for details. it shows me this error how can I figure it out?
It might be, if you have experience with Docker and K8s already. Personally I think something like Lambda is better for APIs like this (I have a tutorial on that too). But I think having a basic EC2 example as a tutorial is the easiest to grasp (even if it's not the best way to build this long term).
Hi Sir, 1) I followed your tutorial and i run the smallest Application with ec2 and fast Api. 2) I created big application in that static, templates and model folders is there . How can i include in configuration of nginx.
Nice work! I don't actually use NGINX a lot myself, so I think you'll have to look on your own how to integrate that with your FastAPI server. For FastAPI my preferred way to deploy it is actually to use Lambda functions like in this video: ua-cam.com/video/RGIM4JfsSk0/v-deo.html
you'll first need to return to command mode. You can tell you're in insert mode if you see -INSERT- at the bottom of the editor. To exit insert mode and go back to command mode, press the Esc (Escape) key on your keyboard. Once you are in command mode, you can save and quit by typing :wq. This command is a combination of :w (which saves the file) and :q (which quits the editor). Press Enter after typing :wq to execute the command.
Hey, great tutorial. Im currently working on my first API for an android app and was wondering how the api connects to a database once deployed to cloud? Does the database also need to be uploaded? Im sorry if the question sounds dumb but im still learning.
Great question - That's the next step! If you host your FastAPI on EC2, you can just put the database on EC2 as well. But here I just released a video on how to host a serverless FastAPI (and use a serverless database with it): ua-cam.com/video/iLt00bqp6is/v-deo.html
If you're using a NoSQL database like MongoDB it's easier, bacause it's already deployed, so you only need to use the connection URL string in the API and give the needed permissions
you missed the step for nginx where you show how to allow connection from firewall..... no wonder it didn't work for me first time I followed your tutorial
Thanks for this tutorial. Am new to AWS / EC2. Would like to know why would one need NGINX here ? Can't we spin up Fast API container & then access it using EC2 Public IP ??
You probably can, but using NGINX as a reverse proxy is a very typical design pattern for this type of app just because of the control and security it provides. But if you have a lightweight use case and don't need all the extra stuff that NGINX helps with, yes I think you can just use FastAPI on a fixed port.
Nah tbh I just wanted a way to self host my app with a static ip for freez thought ec2 would help out but yeah its all but if there's a way you could possibly display how you could port forward a fastapi app to https that would be a pretty good video, I haven't seen anything like that and I would appreciate it if you made a tutorial showcasing how you'd do that
@@ApartmentAngelsFireStarters EC2 will cost money after a year, and a static IP is hard for people to type and remember and it can change if you reboot the server. Also, setting up HTTPS means you have to manage your own certificate and expiry. You'll also have to figure out how to patch and update your host so it doesn't become vulnerable to new attacks. Managing all that is possible, but difficult. That's why if you want to deploy a new app, I recommend going serverless instead. Check out how to deploy FastAPI to AWS Lambda. It's free for 1 million requests per month, and comes with a free HTTPS API endpoint (better than a static IP). If you need to persist data, check out ua-cam.com/video/iLt00bqp6is/v-deo.html
HI! very good video. I have a questions. After stopped the virtual machine and restart it, the FastAPI app does not work. Should I change any information each time I turn off the created EC2?
For production, if you are building a new app, I recommend going serverless. It's much easier to maintain and keep running (you don't need to worry about scaling, patching, load balancing, security). Check out a similar video for how to deploy FastAPI on AWS Lambda here: ua-cam.com/video/RGIM4JfsSk0/v-deo.html And if you wanna add a database: ua-cam.com/video/iLt00bqp6is/v-deo.html
You tutorial is excellent but I found this tutorial. It can be used for production ua-cam.com/video/t2AXy2y61oE/v-deo.html Sharing this tutorial so that it will be helpful to others. I spent almost 8 hours finding this solution last weekend. Thanks for the tutorial and quick response.
You can use a process manager to keep it running. Just like it’s done in NodeJS. Something like pm2, systemd, or even supervisord The will keep the app running and even restart if it crashes.
Yeah... that could be the case. If you run the API directly in the terminal, then when you close it, it might also terminate the API. You might need to run it as a background process, or as a daemon (agent).
Hey, Great content. I have a question, if we stop the ssh session, will the server still be accessible from internet i.e. will the services still work?
Yes, it should be because the FastAPI server will still be running on EC2. However, you can still have the server crash and not recover. So even though this lets you set it up quickly, there's still a lot of work to do to make it resilient. You can also check out my other video on how to host this on AWS Lambda instead, which takes away a huge amount of the server management work you need to do: ua-cam.com/video/RGIM4JfsSk0/v-deo.html
@@pixegami Hi, thanks for the great tutorial. I followed the steps and everything is working. However once I quit running uvicorn on my local terminal, the server gives a 502 error. Could it be my instance settings causing this dependency issue?
Good question. If you follow this AWS EC2 tutorial, you will have a server up very quickly - but it is quite fragile. It doesn't have mechanisms to handle traffic surge, outages, reboots, or host/software patching. If you wanna continue using EC2, you need to add something like a health check and a boot script so that the server restarts when it fails. You might want to put it into auto-scaling group and maybe behind a load-balancer. But if this sounds complicated - then the good news is that there is also a better solution. We can instead deploy FastAPI to something serverless, like AWS Lambda (or AWS Fargate) instead. That will ensure that it is live, scalable and production ready all the time. I'll do a video on that next if people are interested!
Yup, general closing the SSH connection will also terminate the process. unix.stackexchange.com/questions/422188/sshd-into-a-linux-aws-box-how-to-exit-out-of-terminal-without-closing-script-r To work around it, you have several options. The easiest one is just to run the script as a background task. askubuntu.com/questions/396654/how-to-run-a-python-program-in-the-background-even-after-closing-the-terminal But a better way is to run it as a Daemon or a service, so it starts up with the system, and also restarts if it crashes.
You can set FastAPI's server itself to a different port, but port 80 is the standard protocol for HTTP so if you want users to be able to access this via HTTP it eventually does need to be routed through 80.
That's an excellent question - actually I think I'll make a video on the topic of authentication next to cover it. But to specifically answer your question, the API itself will need some sort of auth capability. Anyone can hit the endpoint with a request, but a request must be 'authentic' to be processed (and bad requests rejected with a 401 or 403 error).
For FastAPI (and serverless APIs specifically) the popular way to do it these days is by using JWT tokens. This means the requester must seen a signed token that proves they are authenticated, and authorized to use the API. The token can only be signed by the auth provider (such as Firebase, Auth0, Clerk, and more), but it can be validated by the anyone who has access to the public key (which your API will). Typically, the token is sent in the HTTP request header under "Authorization". FastAPI has support to deal with this: fastapi.tiangolo.com/features/?h=authentication#security-and-authentication
Finally, to prevent an attacker from obtaining a token and re-using it to use the API on a victim's behalf, tokens are usually short-lived (they have an expiry time after a few hours). But to get access to a victim's token, the attacker would usually need to have access to the victim's local environment to begin with (e.g. a public computer in a LAN cafe that didn't get logged out).
Thank you very much for your answer also how do i achieve persistence with my database model. My dat that has been pushed seems to be wiped out after a while
@@princemoronfolu If you're using Lambda, that's expected because the function is 'created' clean at the start of most invocations. If you're using EC2, then files and things you create should persist, so I'd be surprised if something is wiping it out - maybe there's a script or something that resets it? I don't know. But in either case, you *shouldn't* really be storing your DB model in your compute environment anyways. It is unsafe, difficult to back up, and will not scale. You need a separate service to host your DB, and your compute (whether EC2 or Lambda) and talk to it with API calls. That's the best way to build it.
Hmm, it's pretty complex if you haven't done it before. You need to get a certificate for your app (from where like "LetsEncrypt"), maybe put a NGINX rule (or maybe FastAPI has native HTTPS integration, I'm not sure). Then you'd have to modify the EC2 instance and security groups to allow HTTPS traffic: docs.aws.amazon.com/cloudhsm/latest/userguide/ssl-offload-enable-traffic-and-verify-certificate.html Personally, I don't like to manage any of that stuff myself so I prefer to use something like AWS Lambda for my APIs, then use AWS ApiGateway to manage the endpoint (and configure HTTPS through it instead).
💡If you want an even BETTER way to host your FastAPI, check out my video on how to use FastAPI with AWS Lambda: ua-cam.com/video/RGIM4JfsSk0/v-deo.html
This is serverless and in most cases is superior to EC2 in terms of scaling and cost.
for anyone wondering how to save and quit
you'll first need to return to command mode. You can tell you're in insert mode if you see -INSERT- at the bottom of the editor. To exit insert mode and go back to command mode, press the Esc (Escape) key on your keyboard.
Once you are in command mode, you can save and quit by typing :wq. This command is a combination of :w (which saves the file) and :q (which quits the editor).
Press Enter after typing :wq to execute the command.
i just spend 1 hour on this :(😑
god bless you
by the way guys, if you connect directly using aws interface, and you struggle to save the vim file where he adds the proxy pass and stuff. here is chatgpt answer to do operations for the vim file :
vim has a steeper learning curve but is very powerful:
Start in 'normal' mode (where you can navigate and use commands, but not type text).
To enter 'insert' mode (where you can type text): Press i
To save: First return to 'normal' mode by pressing ESC, then type :w and press Enter
To exit without saving: Press ESC then type :q! and press Enter
To save and exit: Press ESC then type :wq and press Enter
Copy (called 'yanking' in vim): In normal mode, move to the line you want to copy and press yy
Paste (called 'putting' in vim): Move to where you want to paste and press p
Delete a line: In normal mode, press dd
great content, i learned so much in under 15 minutes
Thank you so much for this video. Helped me a lot in configuring nginx and deploying on EC2!
Glad it was useful! I recommend checking out how to deploy FastAPI to AWS Lambda as well (it's cheaper and easier to maintain).
Thanks a lot! This is the only of thousands that realy helped me to launch my project!!!
You're welcome!
would be nice to see you implement the https version of this, amazing video!
Thanks for the suggestion!
@@pixegami Is it done now??
thank's your kind video from south korea!
hi, can i ask? if I shutdown my computer, can people access my api ?
this worked for me. thank you very much sir!
Glad it helped
exactly what i was looking for. thanks!
Awesome! I'm glad to hear that :)
Amazing tutorial. Thankyou!
You have such a nice voice 😍 THANK YOU FOR THE VIDEO
Thank you, glad you enjoyed it! 🫶
Great Video, thank you very much. Works like a charm
Great to hear!
Great Video, using vim like a Crack!
Thanks!
Thanks for such a understandable tutorial.
My pleasure. I hope it helped!
thank you very very much! Greate Tutorial and explanation.
Glad it was helpful!
Thanks a lot! This was really helpful. :)
You're welcome!
Excelente tutorial, gracias por compartir tu conocimiento
You're welcome!
Thanks Bro !!! this video help me a lot,
Thanks brother, amazing job ...
Great video!!! You even cleared a lot of fundamentals for me! I believe the same concepts will work for any ubuntu vps right ? Other than an ec2 instance ?
thanks!
Yup, I think most of the concepts will still apply to any kind of Linux VPS :)
I used to create a new domain config under 'sites-available' first and then link to 'sites-enabled' ? what is the right way?
Thanks you very much it was very useful
Glad to hear that!
whoa! This is awesome 🌟 🌟 🌟 🌟 🌟
Thank you! Cheers!
I'm using vultr, installed nginx and configured as shown. It just doesn't work for me, no matter how I edit this, the url just says that page isn't working... any idea?
Thanks for the video. Could you also make an video about nginx, gunicorn, supervisor and having a frontend like vue?
Thanks for the recommendation. I haven't actually used that stack much so I doubt I'll be able to make a good video on it, but I'll add it to my list of things to learn.
thanks. very useful.
thanks man be bless
It would be nice to see how to automate this. In this case you are manually typing a command. What would you use to automate a deployment to the EC2?
Good point. You'd have to set up a CICD workflow. I recommend GitHub actions. Here's a new video I just made on it: ua-cam.com/video/9uMcN66mfwE/v-deo.html
The video doesn't look at EC2 specifically (I prefer Lambda for compute), but the concept's pretty much the same.
when I close my aws terminal then my fastapi app gone anyway. how to keep running my fast api app even I close my terminal ?
That's because the Fast API process also terminates when you disconnect from the shell. You have to run it in the background. NGINX should have a way to do that for you - but if it doesn't, then you can try other ways to run it in the background (or as a Daemon).
I don't have a video on it yet, but maybe something like this from the command: stackoverflow.com/questions/44222883/run-a-shell-script-and-immediately-background-it-however-keep-the-ability-to-in
Or there are some more options here: stackoverflow.com/questions/1603109/how-to-make-a-python-script-run-like-a-service-or-daemon-in-linux
No creo que hables español, pero te amo.
Thank you :)
Please let me know how to solve 404 error. Nginx not found and Bad gateway error.
Im getting 404 error if i try to run the instance after couple of days of deployment.
thank you!
You're welcome!
If you stop instance and start it again you are going to lose all your set up. How to make these usung "user data"?
Thank you!
You're welcome!
I am having problem in nginx part, when I try to close vim there, it doesn't close directly, It says either write a command or press enter, and takes me back to the same interface where I can edit the file
Thanks a lot!
No worries!
This videos shows how to test the app but it wont work due to server will be terminated once window is closed..
You can run the command with `&` at the end (on any Unix based terminal) and that should prevent the terminal from shutting down the process when it exits. They can continue to run in the background.
But yes, if you want to go to production with this you'll need to quite a bit more work.
I couldn't understand if we launched our instance then AWS charged us and if not then after how much time it start charging?
As of 2023 (when I'm typing this) EC2 has a 1-year free tier for the micro instance type: aws.amazon.com/free/
But if you use serverless hosting (Lambda) you can get something like 1 million free requests per month, which is much better in my opinion: ua-cam.com/video/RGIM4JfsSk0/v-deo.html
thank you for your help but I have a question in my case when I try to do "sudo service nginx restart" or "sudo service nginx start"
Job for nginx.service failed because the control process exited with error code.
See "systemctl status nginx.service" and "journalctl -xeu nginx.service" for details.
it shows me this error how can I figure it out?
syntax error in nginx
Nice example. Wouldn't it be easier to just use docker, and deploy it on container with k8's?
It might be, if you have experience with Docker and K8s already. Personally I think something like Lambda is better for APIs like this (I have a tutorial on that too). But I think having a basic EC2 example as a tutorial is the easiest to grasp (even if it's not the best way to build this long term).
@@pixegami Damn. I appreciate you replying. I am not worthy. :-) But thank you!
Hi Sir, 1) I followed your tutorial and i run the smallest Application with ec2 and fast Api. 2) I created big application in that static, templates and model folders is there . How can i include in configuration of nginx.
Nice work! I don't actually use NGINX a lot myself, so I think you'll have to look on your own how to integrate that with your FastAPI server. For FastAPI my preferred way to deploy it is actually to use Lambda functions like in this video: ua-cam.com/video/RGIM4JfsSk0/v-deo.html
I got it no need to mention anything. folder structure is important.@@pixegami
How did you put the cursor on --INSERT-- in the nginx file to write :wq and close it?
you'll first need to return to command mode. You can tell you're in insert mode if you see -INSERT- at the bottom of the editor. To exit insert mode and go back to command mode, press the Esc (Escape) key on your keyboard.
Once you are in command mode, you can save and quit by typing :wq. This command is a combination of :w (which saves the file) and :q (which quits the editor).
Press Enter after typing :wq to execute the command.
NICE
Can i expose it bia https without a domain?
Hey, great tutorial. Im currently working on my first API for an android app and was wondering how the api connects to a database once deployed to cloud? Does the database also need to be uploaded? Im sorry if the question sounds dumb but im still learning.
Great question - That's the next step! If you host your FastAPI on EC2, you can just put the database on EC2 as well. But here I just released a video on how to host a serverless FastAPI (and use a serverless database with it): ua-cam.com/video/iLt00bqp6is/v-deo.html
If you're using a NoSQL database like MongoDB it's easier, bacause it's already deployed, so you only need to use the connection URL string in the API and give the needed permissions
@@isaquehg bad suggestion imo, choice of db completely depends on the use case of the project. NoSQL might or might not fit into the usecase.
you missed the step for nginx where you show how to allow connection from firewall..... no wonder it didn't work for me first time I followed your tutorial
Hmm interesting, I must have missed that. Thanks for sharing your discovery!
hey, how can you make the fast api /docs not visible from the users ?
You should be able to turn it off like this: `FastAPI(docs_url=None)`.
See this for more info: fastapi.tiangolo.com/tutorial/metadata/#docs-urls
@@pixegami thank you !! have a great day
Thanks for this tutorial. Am new to AWS / EC2. Would like to know why would one need NGINX here ? Can't we spin up Fast API container & then access it using EC2 Public IP ??
You probably can, but using NGINX as a reverse proxy is a very typical design pattern for this type of app just because of the control and security it provides. But if you have a lightweight use case and don't need all the extra stuff that NGINX helps with, yes I think you can just use FastAPI on a fixed port.
@@pixegami I tried with & without NGINX - both work fine. Thanks 👍
I used this but give a 404 error in all situations
Hi, love this vid, could you do a deployment video for ec2 or lambda where you deploy to a windows device?
Thanks! Although I'm not sure what you mean by deploying to a "Windows" device. Do you mean a EC2/Lambda that runs a Windows OS?
Nah tbh I just wanted a way to self host my app with a static ip for freez thought ec2 would help out but yeah its all but if there's a way you could possibly display how you could port forward a fastapi app to https that would be a pretty good video, I haven't seen anything like that and I would appreciate it if you made a tutorial showcasing how you'd do that
@@ApartmentAngelsFireStarters EC2 will cost money after a year, and a static IP is hard for people to type and remember and it can change if you reboot the server. Also, setting up HTTPS means you have to manage your own certificate and expiry. You'll also have to figure out how to patch and update your host so it doesn't become vulnerable to new attacks.
Managing all that is possible, but difficult. That's why if you want to deploy a new app, I recommend going serverless instead.
Check out how to deploy FastAPI to AWS Lambda. It's free for 1 million requests per month, and comes with a free HTTPS API endpoint (better than a static IP).
If you need to persist data, check out ua-cam.com/video/iLt00bqp6is/v-deo.html
HI! very good video. I have a questions. After stopped the virtual machine and restart it, the FastAPI app does not work. Should I change any information each time I turn off the created EC2?
@pixegami What about production environment?
You explained how to run it.
How can you keep it running if you are hosting on ec2 for production use?
For production, if you are building a new app, I recommend going serverless. It's much easier to maintain and keep running (you don't need to worry about scaling, patching, load balancing, security). Check out a similar video for how to deploy FastAPI on AWS Lambda here: ua-cam.com/video/RGIM4JfsSk0/v-deo.html
And if you wanna add a database: ua-cam.com/video/iLt00bqp6is/v-deo.html
You tutorial is excellent but I found this tutorial. It can be used for production
ua-cam.com/video/t2AXy2y61oE/v-deo.html
Sharing this tutorial so that it will be helpful to others.
I spent almost 8 hours finding this solution last weekend.
Thanks for the tutorial and quick response.
@@chetanpatel2282 thanks for sharing!
You can use a process manager to keep it running. Just like it’s done in NodeJS.
Something like pm2, systemd, or even supervisord
The will keep the app running and even restart if it crashes.
which is better for api deployment ec2 or aws lambda
hi, can i ask? if i shutdown my computer, can people access my api ?
@@justin23262 if its on local host then no
If you host it somewhere then yes
@@justin23262 look if you hosted it somewhere and make outpost settings for everywhere then yes anyone can access
Personally, I'd go with Lambda. Better scaling, better availability, less maintenance.
@@pixegami yep I consider lambda also btw I am fresher working with cloud new to me and Thanks for comments it gives me a support
Why doesn't ec2 download tensorflow
Hi good afternoon , one question, I closed the terminal CMD , the api no found why?, the instance do not start?
Yeah... that could be the case. If you run the API directly in the terminal, then when you close it, it might also terminate the API. You might need to run it as a background process, or as a daemon (agent).
f***ing legend
Might look unrelated, but...
What's your firefox theme?
I don't use any special theme, I think it was just one of the in-built ones suggested my Firefox when I upgraded/downloaded it.
Hey,
Great content.
I have a question, if we stop the ssh session, will the server still be accessible from internet i.e. will the services still work?
Yes, it should be because the FastAPI server will still be running on EC2. However, you can still have the server crash and not recover. So even though this lets you set it up quickly, there's still a lot of work to do to make it resilient.
You can also check out my other video on how to host this on AWS Lambda instead, which takes away a huge amount of the server management work you need to do: ua-cam.com/video/RGIM4JfsSk0/v-deo.html
@@pixegami Hi, thanks for the great tutorial. I followed the steps and everything is working. However once I quit running uvicorn on my local terminal, the server gives a 502 error. Could it be my instance settings causing this dependency issue?
how to make it work over https???????
how do i keep fastAPI app running in production?
Good question. If you follow this AWS EC2 tutorial, you will have a server up very quickly - but it is quite fragile. It doesn't have mechanisms to handle traffic surge, outages, reboots, or host/software patching.
If you wanna continue using EC2, you need to add something like a health check and a boot script so that the server restarts when it fails. You might want to put it into auto-scaling group and maybe behind a load-balancer.
But if this sounds complicated - then the good news is that there is also a better solution. We can instead deploy FastAPI to something serverless, like AWS Lambda (or AWS Fargate) instead. That will ensure that it is live, scalable and production ready all the time. I'll do a video on that next if people are interested!
@@pixegami I’ll be looking forward to seeing the tutorial on Lambda
@Yang Wang Here's the video with FastAPI on AWS Lambda: ua-cam.com/video/RGIM4JfsSk0/v-deo.html Enjoy!👍
@@pixegami I’ve watched the video, this is superb 🤩
you should've just mentioned that from the beginning smh...@@pixegami
What if you close your terminal in your local linux machine would your fastapi close as well on Amazon AWS?
Yup, general closing the SSH connection will also terminate the process.
unix.stackexchange.com/questions/422188/sshd-into-a-linux-aws-box-how-to-exit-out-of-terminal-without-closing-script-r
To work around it, you have several options. The easiest one is just to run the script as a background task.
askubuntu.com/questions/396654/how-to-run-a-python-program-in-the-background-even-after-closing-the-terminal
But a better way is to run it as a Daemon or a service, so it starts up with the system, and also restarts if it crashes.
@@pixegami I want to run it 24/7
Hello,
I've been wondering, is it possible to use a port different port 80 ? And if you've covered this topic in another video
You can set FastAPI's server itself to a different port, but port 80 is the standard protocol for HTTP so if you want users to be able to access this via HTTP it eventually does need to be routed through 80.
Now how do you secure this. Since this is just public facing. Is there any authorization process?
That's an excellent question - actually I think I'll make a video on the topic of authentication next to cover it.
But to specifically answer your question, the API itself will need some sort of auth capability. Anyone can hit the endpoint with a request, but a request must be 'authentic' to be processed (and bad requests rejected with a 401 or 403 error).
For FastAPI (and serverless APIs specifically) the popular way to do it these days is by using JWT tokens.
This means the requester must seen a signed token that proves they are authenticated, and authorized to use the API. The token can only be signed by the auth provider (such as Firebase, Auth0, Clerk, and more), but it can be validated by the anyone who has access to the public key (which your API will).
Typically, the token is sent in the HTTP request header under "Authorization". FastAPI has support to deal with this: fastapi.tiangolo.com/features/?h=authentication#security-and-authentication
Finally, to prevent an attacker from obtaining a token and re-using it to use the API on a victim's behalf, tokens are usually short-lived (they have an expiry time after a few hours).
But to get access to a victim's token, the attacker would usually need to have access to the victim's local environment to begin with (e.g. a public computer in a LAN cafe that didn't get logged out).
Thank you very much for your answer also how do i achieve persistence with my database model. My dat that has been pushed seems to be wiped out after a while
@@princemoronfolu If you're using Lambda, that's expected because the function is 'created' clean at the start of most invocations. If you're using EC2, then files and things you create should persist, so I'd be surprised if something is wiping it out - maybe there's a script or something that resets it? I don't know.
But in either case, you *shouldn't* really be storing your DB model in your compute environment anyways. It is unsafe, difficult to back up, and will not scale. You need a separate service to host your DB, and your compute (whether EC2 or Lambda) and talk to it with API calls. That's the best way to build it.
My friend please highlight that you use python3.8 otherwise with python 3.10 you have to install 3.10
Thanks for catching that!
how about https?
Hmm, it's pretty complex if you haven't done it before. You need to get a certificate for your app (from where like "LetsEncrypt"), maybe put a NGINX rule (or maybe FastAPI has native HTTPS integration, I'm not sure).
Then you'd have to modify the EC2 instance and security groups to allow HTTPS traffic: docs.aws.amazon.com/cloudhsm/latest/userguide/ssl-offload-enable-traffic-and-verify-certificate.html
Personally, I don't like to manage any of that stuff myself so I prefer to use something like AWS Lambda for my APIs, then use AWS ApiGateway to manage the endpoint (and configure HTTPS through it instead).