How to Run a Python Docker Image on AWS Lambda

Поделиться
HTML-код
  • Опубликовано: 29 ноя 2024

Комментарии • 122

  • @sweetymjct
    @sweetymjct 10 месяцев назад +8

    You are brilliant!! Following your steps, I deployed several Python apps to AWS Lambda. Your instructions were clear, code was perfect (worked fine without any changes) and explanations were precise. Very rare in free RUclips tutorials. Thank you.

    • @pixegami
      @pixegami  9 месяцев назад

      That's awesome feedback. Thanks for sharing that! Glad it was useful for you.

  • @tangled55
    @tangled55 8 месяцев назад +6

    Dude, I cannot tell you how AMAZING you are and how AMAZING this video is. I could literally hug you through the screen. Thank you so much for your video man. It was a PERFECT explanation.

    • @pixegami
      @pixegami  8 месяцев назад +1

      Thank you! I'm glad it was helpful.

  • @eb8931
    @eb8931 7 месяцев назад +1

    Fantastic video. It covered so much (CDK, Docker, Lambda) in just 13 minutes. And it actually all works really easily. My first interaction with lambda was a few years ago and decided I would never, ever touch it. But after this video, with CDK and the other progress since then, I am reconsidering moving my workflows over to lambda instead of FastAPI. Pretty much all because of watching this video.

    • @pixegami
      @pixegami  7 месяцев назад

      Thank you! I really appreciate this kind of feedback, and honestly it's hearing things like this that motivate me to make this content.
      I hope that project all goes well for you!

  • @visheshmp
    @visheshmp 6 месяцев назад +3

    How do you even know what we want? Short simple to the point explanation. Thank you so much. Please keep creating such amazing videos!

    • @pixegami
      @pixegami  6 месяцев назад

      Thank you, I appreciate your comment, and I'm glad you enjoyed it!

  • @champikadilshan5920
    @champikadilshan5920 5 месяцев назад +2

    Perfect! This video is brilliant, If anyone need to Run a Python Docker Image on AWS Lambda. Check this, You will definitely able to solve your issue

    • @pixegami
      @pixegami  5 месяцев назад

      Glad it was useful!

  • @alphasauroxviix4235
    @alphasauroxviix4235 5 месяцев назад +2

    Thank you for putting so much effort into this video, the quality is just magnificent. Your channel is super underrated, I hope you get the recognition that you deserve soon.

    • @pixegami
      @pixegami  5 месяцев назад

      Thank you! Really glad to hear you enjoyed this :)

  • @JosephNOMO-m4n
    @JosephNOMO-m4n Год назад +2

    I'm very thankful for this video. I really struggle with GUI tools like websites. This command line interface solution really helped me to ship and urgent work I was struggling to configure on aws web interface. Thank you for your work.

  • @igortrindade-dev
    @igortrindade-dev 9 месяцев назад +2

    Thank you for sharing this! It saves me a lot of time trying to create a bunch of layers to execute some heavy deps functions on AWS Lambda 🤯🙏

    • @pixegami
      @pixegami  9 месяцев назад +1

      You're welcome! I'm glad to hear it helped :)

  • @taylormonacelli
    @taylormonacelli 3 месяца назад

    Straight to the point and thorough, very nice job. Super helpful, thank you!

  • @terryyyzhang
    @terryyyzhang 11 месяцев назад +4

    Thanks. It could be more helpful if you walk through the permission setting in IAM, as I end up spending an hour configuring that during bootstrapping as a newbie.

    • @pixegami
      @pixegami  11 месяцев назад +2

      Ah I see, thanks for sharing that. IAM is one of those things that I often forget can sometimes be very challenging unless you've used it before. Noted for next time!

    • @hamishrobertson946
      @hamishrobertson946 11 месяцев назад

      fyi for me these issues popped up around the 943 mark (Deploy to AWS). Assuming you are running as a managed user you'll need to add the appropriate policies to your IAM group as 'Permission denied' starts popping up each time you run cdk deploy/bootstrap. I had to add stuff like AmazonS3FullAccess and AmazonSSMFullAccess

  • @ethananthony7419
    @ethananthony7419 Месяц назад

    Excellent, well articulated and easy to follow instructions. Thanks!

    • @pixegami
      @pixegami  27 дней назад

      Thanks! Really glad you found it easy to follow. That's always the goal! 😊

  • @k3chang
    @k3chang Месяц назад

    Thank you so much for the video. Very clear.
    I have 1 question - the path you're using for all this, is it on your local machine? or is it via aws cli on the aws box, given the ?

    • @pixegami
      @pixegami  Месяц назад

      Yeah, great question! The path I'm using is actually on my local machine. The 'ap-southeast-2' you see is just specifying the AWS region I'm deploying to using the AWS CLI and CDK. All the development and commands are run locally, then deployed to AWS.

  • @prajganesh
    @prajganesh 9 месяцев назад +1

    Great explanation. What are ways you can pass parameters to the container?

    • @pixegami
      @pixegami  9 месяцев назад

      Thank you! Well, it depends on what kind of parameters you'd like to pass. Environment variables are probably an obvious and easy way to get started. You could also potentially modify/override the "command" or the "entry point" it is executed with.

  • @davidcasanova2951
    @davidcasanova2951 Год назад +5

    Thanks a lot ! Nice tutorial.
    Do you plan to make a video tutorial for a larger project, such as building a full-stack website or something similar ?

    • @dchenk
      @dchenk Год назад

      Just use docker-compose for this.

    • @pixegami
      @pixegami  Год назад +4

      Thanks! I do plan some in the coming months, but right now I'm just trying to build a base of smaller videos (like this one) to cover all the basic tools first :)

    • @ueiwqoak
      @ueiwqoak Год назад

      @@pixegami bump on @davidcasanova2951 's request. Fantastic video

  • @immanuelpeter1579
    @immanuelpeter1579 5 месяцев назад +1

    What if we want to use env variables? Where would we place them and how can we retrieve them in the py file?

  • @ttaylor9916
    @ttaylor9916 Месяц назад +1

    LIke all the other comments ... brilliant. worked for me.

    • @pixegami
      @pixegami  Месяц назад

      Thanks! Really glad to hear it worked out for you too!

  • @Acumen928
    @Acumen928 Год назад +3

    omg exactly what i needed thanks so much!!

  • @juanfranciscoestradagonzal7826
    @juanfranciscoestradagonzal7826 8 месяцев назад +1

    Does the cost of lambdas change with the use of ECR? Or is there any additional cost generated by using ECR?

    • @pixegami
      @pixegami  8 месяцев назад

      ECR cost would be billed separately to Lambda. I think ECR also has a free tier? aws.amazon.com/ecr/pricing/

  • @beratgenc7695
    @beratgenc7695 7 месяцев назад +1

    This is perfect! Great work, thanks!!!

    • @pixegami
      @pixegami  7 месяцев назад

      Glad it was useful, thank you!

  • @jeffwoolfe
    @jeffwoolfe Год назад +2

    Great video! When creating the CDK project why do you choose typescript as your language and not python? When I choose python as the language the lib folder is created in the venv folder.

    • @pixegami
      @pixegami  Год назад

      I tried Python before and I'm not sure I like the experience as much as Typescript. CDK is natively developed in Typescript, so I feel it makes the quality of code hinting better, error messages easier to read (without all the JSII stuff they used to translate it to Python) and most importantly, it has the largest example base online and in tools like GPT.

  • @frkael8255
    @frkael8255 Год назад +2

    New skill added to my resume, thank you so much :D

    • @pixegami
      @pixegami  Год назад

      Congrats! Well done :)

  • @rexthundathil3828
    @rexthundathil3828 7 месяцев назад +1

    Is it possible to deploy docker image using terraform? If so, can you do a video on this please?

    • @pixegami
      @pixegami  7 месяцев назад

      I'm gonna guess it's possible, but I haven't used Terraform before. I'll add it to my list to check out!

  • @crabmeat88
    @crabmeat88 6 месяцев назад +1

    Is there anything extra I have to do to get environment variables working with this setup?

    • @pixegami
      @pixegami  6 месяцев назад

      You should be able to apply environment variables via the CDK construct for Lambda.
      See the `environment` parameter: docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_lambda.Function.html

  • @volodymyrtruba7016
    @volodymyrtruba7016 8 месяцев назад +1

    Fantastic video! However, how do we handle logic that takes more than 3 seconds to execute, considering Discord messages have a 3-second limit?

  • @erikcoding1238
    @erikcoding1238 Год назад +1

    what is the terminal autocomplete feature you are using it's nice how it autocompletes for you

    • @jeronimoahumada7817
      @jeronimoahumada7817 Год назад

      Looks like zsh-autosuggestions

    • @pixegami
      @pixegami  Год назад +1

      Yup! Or you can use `fish` terminal and get it out of the box. I have a video on the terminal setup here: ruclips.net/video/ugwmH_xzkCA/видео.html

  • @saml.8295
    @saml.8295 Год назад +3

    I follow your steps and faild to build on the RUN pip install ... I commented it out and can build the image. What it take to get pip command to work? I use Windows 10 and curl command (in PowerShell) does not work with the URL. What's the ../2015-03-03/... for?

    • @pixegami
      @pixegami  Год назад

      Hmm, what error do you see when you try to run pip install?

    • @mohamedabdo-xg6tw
      @mohamedabdo-xg6tw Год назад +1

      @@pixegami I built my image, it runs with no errors but when I do Curl nothing happens. so, as saml asked, what ../2015/... stands for? or where it comes from?

    • @kepitingtiara
      @kepitingtiara 9 месяцев назад

      @@mohamedabdo-xg6tw , I did have same questions, here the GPT help me
      The 2015-03-31 represents the version of the AWS Lambda API. When you send an HTTP POST request to this endpoint, it triggers the Lambda function handler specified in your Docker container.

  • @bengoudifaoussama7020
    @bengoudifaoussama7020 8 месяцев назад

    hello thank you for your video , i have a small question , in my lamda function i have some api keys ,how to do the .env config in aws with docker

  • @techaisolution
    @techaisolution 5 месяцев назад +1

    Hi, this setup spike my billing very high,
    The setup was to build lambda function to read the latest file from the s3 dir and make transformation then finally to s3 target dir,
    So this all setup with the python script has to run once the s3 notification to lambda function that an file just came to s3.
    But it went into a loop and made the s3 and lambda billing spike
    Let me knew what is the issue in my setup that i didn't noticed at first while running this python script in lambda

    • @pixegami
      @pixegami  5 месяцев назад

      Uh oh. It sounds like you have a recursive Lambda function. I guess after you update the S3 file, it probably triggers another event (which triggers the function) in a loop.
      Normally, if you want to trigger Lambda from an S3 event, I wouldn't recommend to update/write back into the same S3 - maybe just use a different bucket for the output.
      For the billing spike, you can create a ticket to AWS support to explain the mistake, and see if they'll be able to give you a refund on the cost (it usually works if it's the first time you are making an honest mistake).

  • @jaxontn
    @jaxontn 8 месяцев назад +1

    Hii can you make a new video for Docker Python API deployment?

  • @KillianTattan
    @KillianTattan 6 месяцев назад +1

    Great video, thank you! Wondering how this would work if using poetry instead of requirements.txt? I've seen a lot of different answers on SO and yotube.

  • @therydn784
    @therydn784 4 месяца назад

    This is was so helpful! Thank you so much!

    • @pixegami
      @pixegami  Месяц назад

      You're welcome! So glad it was helpful for you!

  • @muustafa
    @muustafa Год назад

    thanks for tutorial. What is your terminal that suggest commands?

    • @pixegami
      @pixegami  Год назад +1

      I use fish terminal. See this video for my Mac terminal setup: ruclips.net/video/ugwmH_xzkCA/видео.html

  • @claudiolaas456
    @claudiolaas456 Год назад +1

    Would this setup also work to deploy a streamlit app? Or is it only for request-response type apps?

    • @pixegami
      @pixegami  Год назад +1

      Ah, I don't think it works well for Streamlit. Streamlit relies on a session and an active connection with the user, but Lambda doesn't really do that.
      But stay tuned, I'm also working on a tutorial for deploying Streamlit with Docker to AWS as well.

  • @andreaizquierdobouldstridg2926
    @andreaizquierdobouldstridg2926 9 месяцев назад +1

    Trying your tutorial on windows, prompted the error: 'curl: (52) Empty reply from server'. Any help will be apreciated!

    • @pixegami
      @pixegami  9 месяцев назад

      Is this for when you are testing it locally? What about the Lambda docker output itself? In the terminal where you are running the image locally, do you see any messages?

  • @jayanths2788
    @jayanths2788 6 месяцев назад

    Error saying invalid entry point i am using arm64?? Im using m2 any fixes?

  • @RatherBeCancelledThanHandled
    @RatherBeCancelledThanHandled 10 месяцев назад +1

    Solution : on M chip Mac : change credstore from desktop to osxkeychain , use sudo nano ~/.docker/config.json
    Thanks for sharing but I’m getting the error
    ERROR: failed to solve: public.ecr.aws/lambda/python:3.11: error getting credentials - err: exit status 1, out: ``.
    Can you please advise a solution ?

    • @pixegami
      @pixegami  9 месяцев назад

      Hmm, sounds like a permissions issue. I recommend checking how you are setting up your AWS credentials for this. AWS goes through a series of steps to try and determine your credentials: docs.aws.amazon.com/sdkref/latest/guide/standardized-credentials.html#credentialProviderChain

  • @mohsinhassan88
    @mohsinhassan88 2 месяца назад

    I followed your guide to the letter, but when i try to test the docker build locally with curl i always get "Empty reply from server"
    Any ideas?

    • @mohsinhassan88
      @mohsinhassan88 2 месяца назад

      figured it out difference was im using windows and the command is different for that.
      invoke-WebRequest -Uri "localhost:9000/2015-03-31/functions/function/invocations" -Method Post -Body '{}' -ContentType "application/json"

  • @anaves
    @anaves Год назад +1

    wow! Excelent video. Thanks so much!

  • @BrannigansLaw21
    @BrannigansLaw21 8 месяцев назад

    You've earned a sub my man.

  • @eugenechua7239
    @eugenechua7239 4 месяца назад

    I don't quite get the part on LAMBA_TASK_ROOT? is it an environment variable that u need to set up when u first create the lambda function? the video doesn't seem to cover it..

    • @pixegami
      @pixegami  Месяц назад

      Hey there! Good catch on the LAMBDA_TASK_ROOT. It's actually an environment variable that AWS Lambda automatically sets for you - you don't need to create it yourself. It represents the directory where your Lambda function code is stored in the execution environment. In the Dockerfile, we use it to make sure we're copying files to the right place.
      If you want to dive deeper, check out the AWS docs on Lambda environment variables: docs.aws.amazon.com/lambda/latest/dg/configuration-envvars.html
      Let me know if you have any other questions!

  • @AvakashKumarSahoo
    @AvakashKumarSahoo Год назад +2

    does not work for me, my curl command gets stuck without any response

    • @pixegami
      @pixegami  Год назад

      Have you run the container successfully? Do you see any output from the container terminal?

  • @CloudScience-z4l
    @CloudScience-z4l 11 месяцев назад +2

    I absolutely needed this bro. Thank you so much. +1 follow

    • @pixegami
      @pixegami  11 месяцев назад

      Thank you! Glad it helped :)

  • @Sudeep-ow4pe
    @Sudeep-ow4pe Месяц назад

    Do I need to install docker desktop on my machine.

    • @pixegami
      @pixegami  Месяц назад

      Nope, I don't think you need Docker Desktop specifically. You just need the Docker CLI, which comes with Docker Engine. If you're on Linux, you can install Docker Engine directly. For Mac or Windows, Docker Desktop is an easy way to get everything set up, but it's not strictly necessary if you prefer a lighter installation.

  • @jesuschacon7028
    @jesuschacon7028 3 месяца назад

    i have a problems with CORS. In my frontend in headers response: OK but, also show CORS.

  • @praveenpoonja95
    @praveenpoonja95 10 месяцев назад

    Amazing. I'm facing an issue in Dockerfile in "RUN pip install -r requirements.txt". when I run cdk deploy, I get Failed to build asset error. In local, it is running correctly. Any idea how to resolve this?
    meanwhile I commented 'RUN pip install -r requirements.txt' and returned just dummy message without numpy. This time Cdk deploy worked and the url was giving me the json response

    • @pixegami
      @pixegami  9 месяцев назад

      Hmm, I think we'll need to see what the actual error is - the build could fail for a variety of different reasons.

  • @itsanishjain
    @itsanishjain 10 месяцев назад +1

    I wish real world senerios are this simple lol, I am currently using node and canvas and that's needed a lot of Os level dependies like gcc-c++ cairo-devel etc, how to cache them because everytime I make changes I need to build docker image again and that is taking too much time

    • @pixegami
      @pixegami  9 месяцев назад

      Well, tutorials and examples are always going to be a simpler cleaner version of real world problems which does have its own challenges :)
      But your problem does have a solution. Docker builds images in layers, so anything you want to update/change, just put them in a later layer (e.g. lower in the Dockerfile) so it doesn't have to rebuild everything up until that point.
      That way it'll just re-build your changes into the image without having to go through the dependencies installation again.
      If you want to take it a step further, check out using GitHub actions to automate your workflow and deployment (it also has caching): ruclips.net/video/9uMcN66mfwE/видео.html

  • @JasperRowan-v3n
    @JasperRowan-v3n 10 месяцев назад +1

    Thank you, gained a sub.

    • @pixegami
      @pixegami  10 месяцев назад

      Thank you :)

  • @aacesta
    @aacesta 11 месяцев назад +1

    On the pre-requisites. How to install cdk on aws 2023 linux image.
    sudo yum install nodejs npm
    sudo npm install -g aws-cdk
    sudo npm install -g npm@10.2.5
    sudo npm install -g aws-cdk

  • @Randomize-md3bt
    @Randomize-md3bt 7 месяцев назад +2

    I came here after trying to install my libraries to my python lambda function and it give up so I am trying to upload it as a docker image

    • @pixegami
      @pixegami  7 месяцев назад

      Yup, uploading a library with Python *without* Docker is surprisingly difficult, especially if you want to use libraries that have an OS-dependent library under the hood (which includes AI/ML libraries, numpy, and more). That's why the Docker way is really good to know :)

  • @__sjduebsj
    @__sjduebsj 5 месяцев назад

    Great video! Thanks :)

  • @EricFlores-z7m
    @EricFlores-z7m 9 месяцев назад +1

    Great. thank you

    • @pixegami
      @pixegami  9 месяцев назад

      No worries!

  • @aacesta
    @aacesta 11 месяцев назад +1

    On the pre-requisites. How to instal Docker on aws 2023 linux image.
    sudo yum update -y
    sudo yum install -y docker
    sudo systemctl start docker
    sudo systemctl enable docker
    sudo groupadd docker
    sudo usermod -aG docker $USER
    docker version

    • @pixegami
      @pixegami  11 месяцев назад

      Thanks for sharing this!

    • @aacesta
      @aacesta 11 месяцев назад

      After the above you may also have to restart your machine before commands such as docker ps or similar can succeed in connecting to the daemon.

  • @Acumen928
    @Acumen928 Год назад

    im trying to create a docker image for a python lambda from an amplify app, how would this work for that setup?

    • @pixegami
      @pixegami  Год назад

      I haven't used Amplify, but from what I understand it creates and manages the Lambda runtime for you. I'm not sure if you can extend it to support images. Let me know if you find out otherwise.
      That's why for more fine-grained control Iike to use AWS CDK for the infrastructure.

  • @ttaylor9916
    @ttaylor9916 Месяц назад

    Thanks!

    • @pixegami
      @pixegami  Месяц назад

      No worries! Glad you found it helpful!

  • @spare4337
    @spare4337 6 месяцев назад +1

    THANKS

    • @pixegami
      @pixegami  6 месяцев назад

      You're welcome!

  • @TC-gz1ko
    @TC-gz1ko 8 месяцев назад

    Dumb question, what terminal are you using?

  • @VishwamTalnikar
    @VishwamTalnikar Месяц назад

    how to add vpc related code?

    • @pixegami
      @pixegami  Месяц назад

      Yeah, adding VPC configuration wasn't covered in this tutorial. But you can definitely do it! In the CDK, you'd use the 'vpc' property when defining your Lambda function. Something like:
      ```python
      lambda_function = lambda_.Function(self, "MyFunction",
      ...
      vpc=ec2.Vpc.from_lookup(self, "VPC", vpc_id="your-vpc-id"),
      ...
      )
      ```
      Check out the AWS CDK docs for more details: docs.aws.amazon.com/cdk/api/v2/docs/aws-construct-library.html#aws-lambda

    • @VishwamTalnikar
      @VishwamTalnikar Месяц назад

      @@pixegami 👍

  • @jeromedormoy6955
    @jeromedormoy6955 Год назад +1

    king

  • @SiddharthSharma360
    @SiddharthSharma360 11 месяцев назад +2

    It's not python it's typescript

    • @pixegami
      @pixegami  11 месяцев назад +2

      The Docker image is a Python app. If you mean the CDK - yes that's Typescript. You can use Python for it too, but I generally go with Typescript since it's the 'native' language for CDK.

  • @DeepakKumar-nl5jr
    @DeepakKumar-nl5jr Год назад +1

    Nice video. Thanks @pixegami

  • @awshima
    @awshima 2 месяца назад

    I`m always getting a 404 back from the http call.
    The files are in the /var/task folder:
    sh-4.2# pwd
    /var/task
    sh-4.2# ls
    main.py requirements.txt
    sh-4.2# cat main.py
    import numpy as np
    def handler(event, context):
    arr = np.random.randint(0, 10, (3, 3))
    return {
    'statusCode': 200,
    'body': {"message": "Hello from Lambda!", "array": arr.tolist()}
    }
    Using python inside the container, the function is working fine:
    sh-4.2# python -c "from main import handler; print(handler({}, {}))"
    {'statusCode': 200, 'body': {'message': 'Hello from Lambda!', 'array': [[3, 2, 8], [3, 7, 7], [1, 8, 3]]}}
    Logs for the container was showing this:
    2024-09-01 18:56:19 01 Sep 2024 21:56:19,033 [INFO] (rapid) exec '/var/runtime/bootstrap' (cwd=/var/task, handler=)
    Addet this to the Dockerfile since "handler" in the log was empty:
    ENTRYPOINT ["/usr/local/bin/aws-lambda-rie", "python3", "-m", "awslambdaric"]
    Now Logs show this:
    2024-09-01 18:58:25 01 Sep 2024 21:58:25,802 [INFO] (rapid) exec 'python3' (cwd=/var/task, handler=main.handler)
    But the result is the same:
    curl -X POST "localhost:9000/2015-03-31/functions/invocations" -d '{}'
    404 page not found
    It looks like the AWS RIE is not calling the main.py function.
    What can be wrong?

    • @awshima
      @awshima 2 месяца назад

      Man... "localhost:9000/2015-03-31/functions/function/invocations" (missing a "function" path).
      Solved.