Deploy FastAPI on AWS Lambda | In 9 MINUTES

แชร์
ฝัง
  • เผยแพร่เมื่อ 5 ม.ค. 2025

ความคิดเห็น • 149

  • @tntmurphyfever
    @tntmurphyfever 6 หลายเดือนก่อน +9

    Really great tutorial. Thank you so much for sharing. FYI - to people who may be reading the comments - if you setup your FastApi with .env locally and are running with dotenv, you will need to change all those os.getenv() to ENV_VAR = os.environ['NAME'] in lambda.

    • @codingwithroby
      @codingwithroby  6 หลายเดือนก่อน

      Thank you! Also I appreciate you dropping this to help other students 🙂

    • @abdulmuminyqn
      @abdulmuminyqn หลายเดือนก่อน

      Love how a random comment just fix the problem that has been frustrating me for days

  • @Loco-Poco
    @Loco-Poco 9 หลายเดือนก่อน +11

    I am getting "errorMessage": "Unable to import module 'main': No module named 'pydantic_core._pydantic_core'",

    • @vini1520
      @vini1520 9 หลายเดือนก่อน

      pip3 install -r requirements.txt --platform manylinux2014_x86_64 --target=dependencies --implementation cp --python-version 3.10 --only-binary=:all: --upgrade openai

    • @vini1520
      @vini1520 9 หลายเดือนก่อน

      try to use this command 🙂

    • @deveshmishra6119
      @deveshmishra6119 8 หลายเดือนก่อน

      Hey I'm getting the same problem how you resolved it?

    • @simranverma7480
      @simranverma7480 8 หลายเดือนก่อน

      @@deveshmishra6119 how can i resolve

    • @marcoviera7120
      @marcoviera7120 8 หลายเดือนก่อน +1

      Include architecture in your pip install commands. e.g: pip install \
      --platform manylinux2014_x86_64 \
      --target=my-lambda-function \
      --implementation cp \
      --python-version 3.9 \
      --only-binary=:all: --upgrade \
      pandas

  • @santoshbthvr
    @santoshbthvr 5 หลายเดือนก่อน +2

    Thanks, it is a great tutorial. In a short video, you explained the process nicely.

  • @EdwardSapp
    @EdwardSapp 11 หลายเดือนก่อน +7

    What if you have multiple routers added to the main app and each router has several endpoints. Does this still work?

    • @akcelero
      @akcelero 9 หลายเดือนก่อน

      as far as I was able to check - yes works without any problem

    • @elpinchedante
      @elpinchedante 23 วันที่ผ่านมา

      @@akcelero but how? should i package the entire project instead of just the main.py? sorry maybe a dumb question

    • @akcelero
      @akcelero 23 วันที่ผ่านมา

      @@elpinchedante as far as I remember, I was using a container (with the entire project) for lambda, so I can only guess...
      by analogy, it should work if you add the other project's files, but I can't guarantee it. :/

  • @edwardwong654
    @edwardwong654 8 หลายเดือนก่อน +7

    The Lambda function worked, but when I uploaded the zip file, I got an "internal server error" which I traced to "errorMessage": "Unable to import module 'main': No module named 'pydantic_core._pydantic_core' ". Any ideas?

    • @sbussisodube1084
      @sbussisodube1084 7 หลายเดือนก่อน

      had the same issue. I retried the video and this time I removed all dependencies in the requirements.txt file besides just fasapi and mangum after pip freeze > requirements.txt and it worked. video mark 1:04

    • @sbussisodube1084
      @sbussisodube1084 7 หลายเดือนก่อน

      package issue

    • @mysteriousXsecret
      @mysteriousXsecret 3 หลายเดือนก่อน +1

      Any solution?

  • @mooreinsight-g2b
    @mooreinsight-g2b 3 วันที่ผ่านมา

    How does this translate to using a Windows workstation for build? The part building the zip and deploying did not seem to work. Not all the commands like the zip work on Windows. Should these commands working building on windows and deploying to lambda? How does the import know where to get the modules? The file structure in lambda shows a directory for the dependencies but the import makes no reference to path to modules. I'm getting Runtim.ImportModuleError.

  • @naveenv3097
    @naveenv3097 25 วันที่ผ่านมา

    Do we need AWS API gateway service? , from what you showed it seems unnecessary as we have a working url with lambda

  • @6Diego1Diego9
    @6Diego1Diego9 7 หลายเดือนก่อน +2

    This actually worked, Gemini was giving me wrong information.

  • @zoroXgamings
    @zoroXgamings 3 หลายเดือนก่อน

    hi Eric, the way you shoed to deploy the code using zip looks easy , but in production and working in companies is there nay professional way like using docker or other so we can directly run some command in lambda to install the requirements

  • @Welcomevideo-je3rm
    @Welcomevideo-je3rm 5 หลายเดือนก่อน +1

    Fantastic tutorial! I really appreciate your work.

  • @sachinsharma4539
    @sachinsharma4539 ปีที่แล้ว +4

    i followed your video and deploy code zip to lambda when i test i am getting error: "errorMessage": "Unable to import module 'main': No module named 'pydantic_core'"

    • @KnightDark1233
      @KnightDark1233 11 หลายเดือนก่อน +2

      I'm having the same issue

    • @zth-ue5oy
      @zth-ue5oy 11 หลายเดือนก่อน +2

      did you solve this problem? I have the same issue

    • @zth-ue5oy
      @zth-ue5oy 11 หลายเดือนก่อน

      did you solve this problem? I have the same issue@@KnightDark1233

    • @instructabuild
      @instructabuild 11 หลายเดือนก่อน

      I found a workaround to this issue, see my post from Feb 9, 2024 in comments. Hope it helps someone else too...

  • @jdrcreations1947
    @jdrcreations1947 6 หลายเดือนก่อน +1

    I get internal server error despite changing my handler details. What could be the reason

  • @dougkfarrell
    @dougkfarrell หลายเดือนก่อน

    These tutorials are great! I've been building FastAPI apps for a while, and your tutorials have helped strengthen my abilities even more.
    I have a question about deploying FastAPI to an AWS Lambda. I did this myself by creating a Docker container for my entire FastAPI application and deploying that Docker as an AWS Lambda function. This works and works well. But I'm curious if this (or what you showed in your tutorial) takes advantage of FastAPI and its asynchronous ability to handle many requests simultaneously. Would the same AWS Lambda instance be called for multiple requests, taking advantage of FastAPI, or would a new lambda instance be created/executed for each REST request?
    Thanks in advance for your help!
    Doug

  • @thinusenslin
    @thinusenslin หลายเดือนก่อน +1

    Thanks for the tutorial! (Ive also bought your udemy tutorial ;-)
    I have some issues with the deploument when running (cd dependencies; zip ../aws_lambda_artifact.zip -r .)
    I get the following error: At line:1 char:17
    + (cd dependencies; zip ../aws_lambda_artifact.zip -r .)
    + ~
    Missing closing ')' in expression.
    At line:1 char:54
    + (cd dependencies; zip ../aws_lambda_artifact.zip -r .)
    + ~
    Unexpected token ')' in expression or statement.
    + CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : MissingEndParenthesisInExpression
    Any advice?

    • @jeansobreira679
      @jeansobreira679 26 วันที่ผ่านมา

      I've got the same error. Have you worked out how to solve it?

    • @thinusenslin
      @thinusenslin 25 วันที่ผ่านมา

      @ No I have not. I’m looking into Docker to see if that is an easier route. Using Docker with Azure is easy and it works, but it will cost $40 per month…

    • @thinusenslin
      @thinusenslin 25 วันที่ผ่านมา

      Im also trying vscode…maybe that will work 🤷🏻‍♂️

  • @paulokuong
    @paulokuong 3 หลายเดือนก่อน

    Does this work with StreamingResponse?

  • @hmp3854
    @hmp3854 ปีที่แล้ว

    Thanks!

  • @slov1ker583
    @slov1ker583 4 หลายเดือนก่อน

    Do I need to enable CORS for the fastAPI or when I deploy it on aws it will allow me to send external requests?

    • @codingwithroby
      @codingwithroby  4 หลายเดือนก่อน

      Most likely depending on what you are trying to achieve.

  • @aabiddevra9198
    @aabiddevra9198 16 วันที่ผ่านมา

    What is point of using fastapi without Uvicorn, Uvicorn is the one which make fastapi very fast with the ASGI

    • @codingwithroby
      @codingwithroby  15 วันที่ผ่านมา

      Uvicorn def helps with speed. Starlette also helps tremendously.

  • @andresguzman8816
    @andresguzman8816 9 หลายเดือนก่อน +1

    nice video, man, really helpful

    • @codingwithroby
      @codingwithroby  9 หลายเดือนก่อน

      Awesome! You are welcome

  • @JarvisMillan
    @JarvisMillan ปีที่แล้ว +2

    how about larger files? i think you cant upload larger packaged .zip files.

    • @codingwithroby
      @codingwithroby  ปีที่แล้ว +2

      For larger files you will probably not want to do a lambda, but instead use EC2 which I will eventually make a video on

    • @tamaspleszkan6100
      @tamaspleszkan6100 ปีที่แล้ว +2

      Use lambda layers for dependencies. That way you also get the option to edit the code in the editor on the lambda page. If your lambda gets to the point where without dependencies it exceeds the lambda limit, you are packing too much stuff into it.

    • @moverecursus1337
      @moverecursus1337 8 หลายเดือนก่อน

      Mount a EFS on a EC2, pip install --target with target to the EFS, delete ec2 instance, create a lambda with a mounting to the EFS and make PYTHONPATH as the local on efs you installed your dependencies.
      I am doing this for a segmentation model inference application

    • @amliuyong
      @amliuyong 3 หลายเดือนก่อน

      you can use docker image as the run time code for AWS lambda

  • @FatimaHABIB-jm4ji
    @FatimaHABIB-jm4ji ปีที่แล้ว +1

    Thanks Eric for this great tutorial ;
    I have a question though; I could not install zip " × python setup.py egg_info did not run successfully.
    │ exit code: 1
    ╰─> [9 lines of output]" I tried every possible solution proposed online;
    anyone have faced this error while installing this package ?
    Thanks

  • @HashtagTiluda
    @HashtagTiluda ปีที่แล้ว +1

    This is amazing! Was wondering, what if my FastAPI app has multiple folders and files? How should I create the artifact in that case?

    • @codingwithroby
      @codingwithroby  ปีที่แล้ว +1

      You should still be able to do the exact same thing, but you will want to add the other files along with main.

  • @rohanalmighty
    @rohanalmighty 6 หลายเดือนก่อน

    Great Video
    I'd recommend using the FastAPI version that is shown in this video, else things might not work, didn't work in my case.
    How do I set restrictions so that nobody can hit my api too many times and I get a bill sent by AWS.

    • @codingwithroby
      @codingwithroby  6 หลายเดือนก่อน

      If you want to add it in the code you can implement Rate Limiting. One of the most popular libraries for this is: pypi.org/project/ratelimiter/

  • @scottzeta3067
    @scottzeta3067 3 หลายเดือนก่อน

    Once I tried to include another router in the main, Lambda keeps giving me internal server error

    • @codingwithroby
      @codingwithroby  3 หลายเดือนก่อน

      Interesting - what error are you getting?

    • @scottzeta3067
      @scottzeta3067 3 หลายเดือนก่อน

      @@codingwithroby Timout Internal server error. I tried several tutorial I found, but only worked on "Hello World" level API. Once I started to apply some MVC structures, API runs well locally but failed when on Lambda. Since I am not an Fast API expert and lack of exp for cloud. I can not figure out where is the problem and how to solve it.

    • @scottzeta3067
      @scottzeta3067 3 หลายเดือนก่อน

      @@codingwithroby I found if I compress everything into one file, no import from any source code file, it works. Not sure if this is Lambda's expectation.

  • @SylviaTran-u1c
    @SylviaTran-u1c 10 หลายเดือนก่อน

    did you mean to delete `uvicorn` from the generated `requirements.txt` file? that came w/ the install of fastapi?

    • @SylviaTran-u1c
      @SylviaTran-u1c 10 หลายเดือนก่อน

      around 1'30" into the video

    • @vini1520
      @vini1520 9 หลายเดือนก่อน

      @@SylviaTran-u1c Below are my requirements
      fastapi==0.110.0
      mangum==0.17.0

    • @isi6402
      @isi6402 17 วันที่ผ่านมา

      Yes

  • @amarnathreddysurapureddy9111
    @amarnathreddysurapureddy9111 ปีที่แล้ว +1

    if i import some files from another file is it zip every thing

  • @excuseop9339
    @excuseop9339 ปีที่แล้ว

    what if we have another folder . Do i need add folder using this "zip aws_lambda_artifact.zip -u folder_name"

  • @CesarAugustoPerezTafur
    @CesarAugustoPerezTafur ปีที่แล้ว +1

    Hi, How are you? first thank you for the tutorial, second, i have troblue when test de app in aws, it still show Internal server Error message, although i edited Handler in console. i wrote main.handler. the only ting that i made differenet was replace this command lines 2. (cd dependencies; zip ../aws_lambda_artifact.zip -r .)
    3. zip aws_lambda_artifact.zip -u main.py for
    Compress-Archive dependencies/*.* lambda_function2.zip and Compress-Archive main.py -update aws_lambda_artifact.zip
    Could you help me?

    • @SoulCTO
      @SoulCTO ปีที่แล้ว +2

      You need to debug to see what is happening. Maybe it is some incompatibility between lib version supported by aws lambda. What you can try to do is in your requirements.txt, force the version of fast api to 0.99.0 (fastapi==0.99.0) and remove the version of pydantic and pydantic core (so it will get the correct one. When you run locally with uvicorn you will find the right version). This happened to me in another project and might fix the issue.

    • @codingwithroby
      @codingwithroby  ปีที่แล้ว +4

      For the time being, I found the best FastAPI version to be 0.99.0

    • @CesarAugustoPerezTafur
      @CesarAugustoPerezTafur ปีที่แล้ว

      Thanks all for the answer. I solve the problem changing the py lance versión, i mafe a video in spanish with the solution th-cam.com/video/FUsJmLO2G4g/w-d-xo.htmlsi=z-mQPlbBjFde_3g0

    • @CesarAugustoPerezTafur
      @CesarAugustoPerezTafur ปีที่แล้ว

      @@codingwithroby hi,
      I deployed my api with mysql in lambda but not all of them work, only the delete one and the query by user_id work. vbx54teybuuh3rvjyss67x3j740mnyft.lambda-url.us-east-2.on.aws/docs. could you help me

    • @honchan83
      @honchan83 ปีที่แล้ว +1

      @@SoulCTO Thank you so much for leaving this comment, I have spent days to figure out what goes wrong. AWS won't give us that much hints to fix the problem. I did not realise it is the fastapi version that making things to fail.

  • @antonioclevenielsonvieirad2173
    @antonioclevenielsonvieirad2173 9 หลายเดือนก่อน

    hello my friend
    what theme are you using in vscode?

    • @codingwithroby
      @codingwithroby  9 หลายเดือนก่อน

      One Dark Pro - it is an theme that you need download!

  • @thanhquachable
    @thanhquachable ปีที่แล้ว +1

    what if we update our code ? we create and upload a new zip file?

    • @codingwithroby
      @codingwithroby  ปีที่แล้ว

      Yes, in this video I didn't go over how to setup lambda deployments automatically. If you want to do this I'd look at GitHub actions to communicate to your lambda

  • @AronBergara
    @AronBergara ปีที่แล้ว +2

    AMAZING tutorial! 👏

    • @codingwithroby
      @codingwithroby  ปีที่แล้ว

      Glad you enjoyed it! Woot woot!

    • @AronBergara
      @AronBergara ปีที่แล้ว

      I would like to host the frontend files on an S3 bucket, but I'm not finding any tips on how to make this work. Would you have any material on that?@@codingwithroby

    • @AronBergara
      @AronBergara ปีที่แล้ว

      Well, just as I wrote the last comment I thought that I can just specify the S3 URL as path for the templates and it will load as if it were on the same local as the app (that in my case will be in lambda)

  • @KTMedia-hx5gg
    @KTMedia-hx5gg ปีที่แล้ว

    the second command doesn't work in my end. It is giving 1. Missing closing ) in expression and 2. Unexpected token ) in expression or statement

    • @codingwithroby
      @codingwithroby  ปีที่แล้ว

      Hmm, I just retried it and it is working correctly. Are you sure there is no typos?

    • @KTMedia-hx5gg
      @KTMedia-hx5gg ปีที่แล้ว +1

      @@codingwithroby Yeah, I noticed it afterwards that I was using powerhell while you were in zsh. I had to use an alternative to that command for powershell

    • @codingwithroby
      @codingwithroby  ปีที่แล้ว +2

      Do you mind sharing what the alternative command was?

    • @willhg
      @willhg 9 หลายเดือนก่อน

      @@codingwithroby this is the command for VSCode powershell terminal : "Compress-Archive -Path . -destinationpath ../aws_lambda_artifact.zip"

  • @raja27ravi
    @raja27ravi 22 วันที่ผ่านมา

    pip3 install -t dependencies -r requirements.txt
    How to solve below error
    this command is returing this error
    ERROR: Can not combine '--user' and '--target'

    • @codingwithroby
      @codingwithroby  21 วันที่ผ่านมา

      you get that error when doing a pip3 install -r requirements.txt on the repo provided?

  • @טלפיזואטי
    @טלפיזואטי ปีที่แล้ว

    great video , very high quality also

    • @codingwithroby
      @codingwithroby  ปีที่แล้ว

      Much appreciated! I am glad you were able to get value 🙂

  • @ragefurious5650
    @ragefurious5650 ปีที่แล้ว

    this is dope but is there any way i could get the windows variation of some of these console lines?

    • @codingwithroby
      @codingwithroby  ปีที่แล้ว

      Do the commands not work for Windows?

    • @6Diego1Diego9
      @6Diego1Diego9 7 หลายเดือนก่อน

      they should work in Powershell

  • @thonnatigopi
    @thonnatigopi 6 หลายเดือนก่อน +1

    Kindly make a s3 uploads and trigger using lambda and fast api bro

    • @codingwithroby
      @codingwithroby  6 หลายเดือนก่อน +1

      I will add it to my backlog 🙂

    • @thonnatigopi
      @thonnatigopi 6 หลายเดือนก่อน +1

      @@codingwithroby and also how to use custom packages like ffmpeg for audio conversation and others , to trigger lambda and use GPU , inhouse models like whisper model how to use them to trigger pls kindly make a video I'm eagerly waiting

    • @thonnatigopi
      @thonnatigopi 6 หลายเดือนก่อน

      Use everything in vscode because cloud developers can't give credentials to developers in company, so kindly make a video for python automatic project in vscode and if possible dockerize integration deploy and aws lambda and Sam

  • @willk7471
    @willk7471 ปีที่แล้ว +1

    Nice video. Is this something that will interact with a database as well?

    • @codingwithroby
      @codingwithroby  ปีที่แล้ว +1

      Of course! You can do this exact same thing if you are using SQLite but if you are using a different type of database you just need to make sure all the connections are setup beforehand. If you need to see how to connect FastAPI to databases, I have multiple videos for different database you may find helpful 🙂

  • @amarnathreddysurapureddy9111
    @amarnathreddysurapureddy9111 ปีที่แล้ว +2

    I am getting Internal Server Error After followed,

    • @pedroalbertorojasvalladare260
      @pedroalbertorojasvalladare260 11 หลายเดือนก่อน +1

      Me too, did you fix that?

    • @amarnathreddysurapureddy9111
      @amarnathreddysurapureddy9111 11 หลายเดือนก่อน +1

      @@pedroalbertorojasvalladare260 th-cam.com/video/UauMQGqaxGo/w-d-xo.html. I Used CI/CD Piples of Github.

    • @amarnathreddysurapureddy9111
      @amarnathreddysurapureddy9111 11 หลายเดือนก่อน +1

      @@pedroalbertorojasvalladare260 If you want upload more than 2.4 GB Lambda function won't work.

    • @pedroalbertorojasvalladare260
      @pedroalbertorojasvalladare260 11 หลายเดือนก่อน

      @@amarnathreddysurapureddy9111 thanks, I deployed my API on render it's more easily

    • @c4rb0n5
      @c4rb0n5 11 หลายเดือนก่อน

      Check endpoint url and cors maybe

  • @athulkrishnadiyil1735
    @athulkrishnadiyil1735 ปีที่แล้ว +1

    mangum seems like no longer maitained ....?

    • @codingwithroby
      @codingwithroby  ปีที่แล้ว

      It has not had a PR in awhile, but it is still widely used.

  • @eugeniosp3
    @eugeniosp3 ปีที่แล้ว +2

    Easy easy sub. Nice job roby

    • @codingwithroby
      @codingwithroby  ปีที่แล้ว +1

      Woot woot, thanks for the kind words 🙂

    • @eugeniosp3
      @eugeniosp3 ปีที่แล้ว

      @@codingwithroby do you have any info on how to access this from a client side call? Say I want to make a get request with a json payload how would i access this? Is it the function URL? Also what type of auth would it be?

  • @shalineesharma8704
    @shalineesharma8704 11 หลายเดือนก่อน +1

    Wow 🤩

  • @ptk6909
    @ptk6909 ปีที่แล้ว

    This deployment on aws in this video free or not ?
    and also can open /docs or swagger ?

    • @codingwithroby
      @codingwithroby  ปีที่แล้ว

      Yes (part of free tier) and yes 🙂

    • @ptk6909
      @ptk6909 ปีที่แล้ว

      @@codingwithroby I follow your video until deploy successful and open /docs swagger also successful. api can use complete But except api method post api must have parameter/data{} before excute after I input and press excute result error 504 ( This swagger from my code on local have no any problems )

  • @hemantsah8567
    @hemantsah8567 ปีที่แล้ว

    Just curious. Knowing the functionality of Lambda that it is serverless(instances are spawned on request), should I still choose fastapi with Mangum or go with Chalice framework?

    • @codingwithroby
      @codingwithroby  ปีที่แล้ว +1

      I see nothing wrong with using FastAPI with Mangum

  • @kzzz3
    @kzzz3 ปีที่แล้ว +1

    mangum seems abandoned. Last commit was made almost a year ago

    • @codingwithroby
      @codingwithroby  ปีที่แล้ว

      Yeah, but Mangum is still the best option for deploying FastAPI on lambdas. When a better handler comes out you can wrap app in it

  • @JackLogan90
    @JackLogan90 7 หลายเดือนก่อน +1

    Nice!

  • @zeki7540
    @zeki7540 ปีที่แล้ว +1

    That's great!

    • @codingwithroby
      @codingwithroby  ปีที่แล้ว

      Thanks! Glad you were able to find value :-)

  • @jorgesisco981
    @jorgesisco981 11 หลายเดือนก่อน

    Great video!, quick question: will I be able to use all the endpoints without setting them up in the api gateway?

    • @johnhorton3727
      @johnhorton3727 11 หลายเดือนก่อน +1

      You can proxy all routes/methods to the lambda using {proxy+}

  • @sulaiman.micheal
    @sulaiman.micheal ปีที่แล้ว

    Thanks. I appreciate this.

  • @conadeanzola
    @conadeanzola 5 หลายเดือนก่อน

    Why would you deploy FastAPI on top of Lambda instead of using API Gateway and Lambda functions that run only the required logic? It's like forcing a use case in a service that is not designed for that purpose.

    • @Krow-n3o
      @Krow-n3o 3 หลายเดือนก่อน

      In my company, we are migrating from fastapi HTTP microservices to lambda sqs oriented, and this can help to do the transition slowly without too much rework.

    • @conadeanzola
      @conadeanzola 3 หลายเดือนก่อน

      @@Krow-n3o but Lambda already handles the request when you invoke it and then pass the parameters to the logic, why would you need a rest framework in that case instead of just receiving the request via the native method in Lambda ?

    • @Krow-n3o
      @Krow-n3o 3 หลายเดือนก่อน

      @@conadeanzola i agree with you, you don't need to.

  • @pushkarkathayat7893
    @pushkarkathayat7893 10 หลายเดือนก่อน

    The only issue is their cold start

  • @ttaylor9916
    @ttaylor9916 2 หลายเดือนก่อน

    music....

    • @codingwithroby
      @codingwithroby  2 หลายเดือนก่อน

      It's lower now on future videos .. I was learning, I was learning

  • @SajanKumar-mx2jg
    @SajanKumar-mx2jg 7 หลายเดือนก่อน

    Magnum is not maintained so I don't we should use it.

    • @codingwithroby
      @codingwithroby  6 หลายเดือนก่อน

      As of right now though it is a wrapper that doesn"t need maintained (as of now).

  • @adamrichic8753
    @adamrichic8753 ปีที่แล้ว

    Very well, thanks a lot

  • @esgosar
    @esgosar ปีที่แล้ว +1

    Thanks!