How to Run a Python Docker Image on AWS Lambda

pixegami
21 Aug 202313:07

Summary

TLDRThis tutorial video guides viewers on deploying a Docker image on AWS Lambda, showcasing the synergy of containerization and serverless computing. It demonstrates creating a Python 'Hello World' application with Docker, highlighting the benefits of using Docker for complex dependencies. The video covers the setup of AWS CDK, Dockerfile creation, local testing, and AWS infrastructure deployment, culminating in a functional Lambda function accessible via an API endpoint. It also addresses potential cold-start performance trade-offs and provides a foundation for developing applications with diverse dependencies.

Takeaways

  • 📦 Docker is a containerization platform that packages code and its dependencies into a container for consistent execution across different environments.
  • 🚀 AWS Lambda is a serverless compute service that runs code in response to events and charges based on the time the code executes.
  • 💡 Combining Docker with AWS Lambda allows for building applications in any language and running them with all necessary dependencies on the cloud at a low cost.
  • 🛠️ To get started, you need to have Docker, AWS CLI, and AWS CDK installed, ideally with some experience using them.
  • 📝 The script demonstrates creating a Python 'Hello World' application as a Docker image to be deployed on AWS Lambda.
  • 🔧 AWS CDK is utilized to define and provision cloud infrastructure through code, which is more efficient than manual console operations.
  • 🔑 The script includes creating a Dockerfile that specifies the base image, copies the application and its dependencies, and sets up the execution environment.
  • 📝 The Dockerfile uses an AWS Lambda Python 3.11 base image and installs additional dependencies like numpy, which is useful for applications requiring specific packages.
  • 🖥️ Before deploying to AWS, the script suggests testing the Docker image locally to ensure it functions as expected.
  • 🔄 The script outlines deploying the Docker image to AWS Lambda using AWS CDK, which automates the creation of the necessary cloud resources.
  • 🔗 A function URL is created for easy access to the Lambda function, allowing the Docker image to be triggered via an HTTP request.
  • ⚠️ The video notes potential issues with architecture mismatches, especially when using different processor architectures like ARM64 or x86.
  • 🌡️ Docker Lambda functions may have slower cold-starts compared to native Lambda functions but offer reliability for applications with complex dependencies.

Q & A

  • What is Docker and why is it useful?

    -Docker is a way to package your code and its dependencies into a container, ensuring consistent behavior across different environments and machines. This helps avoid unexpected issues that can arise from running the code in different setups.

  • What is AWS Lambda and how does it work with Docker?

    -AWS Lambda is a serverless compute service that allows you to run code in response to events without managing servers. By running Docker images on AWS Lambda, you can execute containerized applications on demand, billed by millisecond of usage.

  • Why combine Docker and AWS Lambda?

    -Combining Docker and AWS Lambda allows you to build applications in any language and run them in a serverless environment. This combination offers flexibility, low cost, and avoids platform-specific issues by packaging dependencies within the Docker image.

  • What are the prerequisites for following the tutorial in the video?

    -To follow along effectively, you need to have Docker, AWS CLI, and AWS CDK installed, along with some experience using these tools.

  • What is AWS CDK and why is it used in the tutorial?

    -AWS CDK (Cloud Development Kit) is a tool for defining cloud infrastructure using code. It is used in the tutorial to create the necessary AWS infrastructure for deploying and running the Docker image as a Lambda function.

  • How do you initialize a CDK project for this tutorial?

    -You initialize a CDK project by creating a folder for the project, navigating to it, and running the 'cdk init' command. The tutorial uses TypeScript as the language for the CDK application.

  • What is the purpose of the Dockerfile in this tutorial?

    -The Dockerfile contains instructions for building the Docker image. It specifies the base image, installs dependencies, and sets up the Python application to be run by AWS Lambda.

  • How do you build and run the Docker image locally?

    -To build the Docker image, navigate to the folder containing the Dockerfile and run a Docker build command. To run the image locally, use a Docker run command and test it by sending an HTTP request to the specified port.

  • What steps are involved in deploying the Docker image to AWS Lambda?

    -Deploying the Docker image involves setting up AWS CLI, bootstrapping the CDK application, and running 'cdk deploy' to create the necessary AWS resources and upload the Docker image.

  • What should you check if your deployed Lambda function encounters runtime errors?

    -If you encounter runtime errors like 'runtime invalid entry point,' ensure that the Lambda architecture matches the architecture of the machine used to build the Docker image. For example, use ARM64 for Apple M1 silicon or x86 for Intel processors.

  • What are the trade-offs of using Docker functions on AWS Lambda compared to native runtimes?

    -Docker functions can have slower cold-start times but offer greater reliability for applications with complex dependencies or platform-specific binaries. They simplify development by avoiding platform-specific issues that can arise when deploying to the AWS Lambda environment.

Outlines

00:00

🐳 Introduction to Docker on AWS Lambda

This paragraph introduces the concept of running a Docker image on AWS Lambda. Docker is explained as a containerization tool that ensures code runs consistently across different environments. AWS Lambda is described as a serverless compute service that executes code in response to events, billed per millisecond of usage. The tutorial aims to demonstrate combining Docker with Lambda by creating a Python 'Hello World' application. Prerequisites include having Docker, AWS CLI, and AWS CDK installed, along with some experience with these tools. The process begins with setting up a CDK project using TypeScript and creating a Docker image folder for the Python application and its dependencies.

05:02

🛠️ Building and Testing the Docker Image Locally

The second paragraph details the steps to create a Dockerfile, which is a set of instructions for building the Docker image. It explains how to copy the 'requirements.txt' file and install the numpy package as a dependency within the Docker image. The Dockerfile is then used to build and run the image locally, testing it by sending an HTTP request to the running container. The paragraph also covers creating the necessary AWS infrastructure using the AWS CDK, defining a Lambda function with a Docker image and setting up an API endpoint for easy access. It emphasizes the importance of testing the Docker image locally before deploying it to AWS and provides guidance on configuring the AWS CLI for deployment.

10:07

🚀 Deploying Docker Image to AWS and Testing

The final paragraph outlines the deployment process of the Docker image to AWS using the AWS CDK. It describes the steps to bootstrap the CDK environment, deploy the application, and verify the deployment by accessing the function URL. The paragraph addresses potential issues that may arise, such as architecture mismatches between the local machine and AWS Lambda, and provides a solution for it. It also discusses the trade-offs between using Docker with Lambda versus native Lambda functions, particularly in terms of cold-start performance. The video concludes with an invitation to check the comments for project code and a thank you note to the viewers.

Mindmap

Keywords

💡Docker

Docker is a platform that uses containerization technology to package applications and their dependencies into a portable, consistent, and isolated environment. In the video, Docker is used to create a container for a Python application, ensuring that it runs smoothly across different machines or environments. The script mentions Docker as a way to avoid unexpected problems when deploying applications.

💡AWS Lambda

AWS Lambda is a serverless compute service provided by Amazon Web Services (AWS) that allows users to run code without provisioning or managing servers. The video explains how to run a Docker image on AWS Lambda, emphasizing its on-demand nature and the cost-effectiveness of its pay-per-millisecond billing model.

💡Container

A container in the context of the video refers to a lightweight, standalone, and executable package of software that includes everything needed to run an application as a single unit. The script discusses using Docker to create a container for the Python 'Hello World' application, highlighting the benefits of containerization for application deployment.

💡Serverless

Serverless computing is a cloud computing execution model where the cloud provider fully manages the server, and dynamically manages the allocation of machine resources. The video describes AWS Lambda as a serverless service, meaning that users do not have to manage servers, and they are billed based on usage rather than server capacity.

💡Cloud Development Kit (CDK)

AWS Cloud Development Kit (CDK) is an open-source software development framework to define cloud infrastructure using code and provision it through AWS CloudFormation. The script describes using the CDK to create a project for the application, allowing for infrastructure as code, which streamlines the process of setting up the necessary cloud resources for the Docker image to run on AWS Lambda.

💡Dockerfile

A Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image. In the video, a Dockerfile is created to provide instructions for building the Docker image of the Python application, which includes specifying the base image, copying files, and setting the default command for the image.

💡Python

Python is a high-level, interpreted programming language known for its readability and efficiency. The video script uses Python to create a 'Hello World' application that serves as an example of how to run a Docker image on AWS Lambda. The script also mentions the use of Python for creating the Lambda function handler.

💡Dependencies

Dependencies in software development refer to other software packages or libraries that a program requires to function properly. The video discusses the use of a 'requirements.txt' file to list all the packages or dependencies needed for the project, such as the 'numpy' package, which is used to demonstrate the handling of external dependencies within a Docker container.

💡API Endpoint

An API endpoint is a specific location in a networked system that is used to send and receive data to and from other programs. The video explains how to create an API endpoint using AWS Lambda, which allows the Docker image to be triggered on-demand when the endpoint is called.

💡Lambda Function Handler

In AWS Lambda, a function handler is the method in your code that processes events. The video script describes creating a Python file named 'main.py' that serves as the Lambda function handler, executing the 'hello world' message and generating a random matrix using the 'numpy' package when the Lambda function is invoked.

💡Cold-start

Cold-start refers to the initial startup latency that occurs when a cloud function is invoked for the first time or after not being used for a period. The video mentions that Docker Lambda functions may have a slower cold-start compared to native Python Lambda functions due to the need to initialize the Docker runtime environment.

Highlights

Docker images can be run on AWS Lambda, offering a serverless cloud computing solution.

AWS Lambda is billed by millisecond of usage, making it an extremely cost-effective option.

Docker and Lambda combined allow for building applications in any language with any dependencies.

The tutorial demonstrates creating a Python 'Hello World' application as a Docker image for AWS Lambda.

Prerequisites for the project include Docker, AWS CLI, and AWS CDK installation and familiarity.

AWS CDK is used to define cloud infrastructure as code, simplifying the setup process.

A Docker image folder with a Python app and a Dockerfile is necessary for the project.

The Python file 'main.py' serves as the Lambda function handler executing the 'hello world' function.

Docker is particularly useful for Python applications with many dependencies or platform-specific binaries.

The 'numpy' package is demonstrated as an example of an external dependency in the Python application.

A Dockerfile is created with instructions to build the Docker image from an AWS Lambda Python base image.

The Dockerfile includes steps to copy the 'requirements.txt' and 'main.py' to the Lambda task root.

Local testing of the Docker image is recommended before uploading it to AWS.

AWS infrastructure for the Lambda function and API endpoint is created using AWS CDK.

The Lambda function is configured with a Docker image, memory size, timeout, and architecture.

A function URL is added for easy access to the Lambda function via an API endpoint.

AWS CLI must be configured with the correct permissions to deploy the Docker image to AWS.

CDK deployment involves bootstrapping and deploying the application to create AWS resources.

The function URL output allows testing of the deployed Lambda function directly from the browser.

Potential issues with architecture mismatch between the local machine and Lambda need to be checked.

Docker Lambda functions may have slower cold-starts but offer reliability with runtime dependencies.

The tutorial concludes with the successful deployment of a Docker Python function on AWS Lambda.

Transcripts

play00:00

Hey everyone. In this video, I'm going to show

play00:02

you how to run a Docker image on AWS Lambda.

play00:05

Docker is a way to put all of your code into

play00:07

a container. That way you won't encounter

play00:10

unexpected problems when you run it on a different

play00:13

machine or a different environment.

play00:15

AWS Lambda is a cloud compute service. We can

play00:18

use this to run our Docker image on demand,

play00:20

just like a function. And we can make it

play00:23

run whenever a user calls our API endpoint.

play00:26

It's completely serverless, so we get billed by millisecond

play00:30

of usage. But it is extremely cheap.

play00:32

Docker and Lambda are amazing together because

play00:35

it means that you can build any type of application

play00:38

you want in any language you want, and then

play00:40

run it on the cloud for a really low cost.

play00:43

Today, we're going to learn how to use this by creating

play00:46

a Python "Hello World" application that we're

play00:49

going to run as a Docker image on AWS Lambda.

play00:51

To follow along effectively with this project,

play00:54

you'll need to have Docker, AWS CLI and

play00:56

AWS CDK installed. And it will be good if you

play00:59

have some experience with them as well.

play01:01

I'm going to start by creating a CDK project for this

play01:05

app. AWS CDK—or "Cloud Development Kit"—is a tool

play01:09

we can use to create our cloud infrastructure.

play01:13

That way, we can define our cloud infrastructure using

play01:17

code instead of having to click things in the console

play01:20

or having to do everything in the command line.

play01:23

Let's make a folder for our CDK project.

play01:27

When you're in the folder, you can run the "cdk

play01:31

init" command to create the CDK application.

play01:34

I'm going to use the language TypeScript for the CDK application.

play01:38

This should finish after about a minute and

play01:41

we can open this directory up in our code editor.

play01:44

Next, we have to create our Docker image folder.

play01:47

That means we'll have our Python app in a

play01:50

folder and a Dockerfile to turn that app into

play01:53

an image with all of its runtime dependencies.

play01:56

Back in our project, I'm going to create a folder

play01:59

called "image". Inside that image folder, I'm

play02:02

going to create another folder called "src".

play02:05

In the "image/src" folder, I'm going to create my Python

play02:09

application file, which I'm going to call "main.py".

play02:13

This Python file is going to be my Lambda function

play02:15

handler. This is going to be the code that

play02:18

executes when my Lambda function is called.

play02:21

And for this, it's just going to be a really basic

play02:24

"hello world" function. It simply returns

play02:26

a status code of 200 (which means it was successful)

play02:29

and a "hello world" message in the body.

play02:32

But if you only wanted to run a simple Python script by

play02:36

itself, then AWS actually has a native Python runtime.

play02:40

So if "hello world" was all you wanted to do, you could

play02:42

just use that. You don't need to use Docker.

play02:45

Where Docker starts to become really useful

play02:47

is if you actually need a lot of packages or

play02:50

dependencies with your Python application.

play02:53

That's because many popular Python packages actually use a lot

play02:56

of platform specific binaries or code behind the scenes.

play03:00

And it can be really difficult to get these binaries

play03:03

installed or working correctly for the

play03:06

Amazon Linux runtime that AWS Lambda uses.

play03:09

That's why a lot of people who develop Python Lambda functions

play03:12

find that they have an error in their AWS Lambda

play03:15

when the same Python code works fine on their machine.

play03:19

With Docker though, this is really easy to fix.

play03:22

Everything inside the image will be run as is.

play03:25

So to demonstrate that, let's also install the

play03:27

"numpy" package and use that in our code.

play03:30

Go to our image folder and in here we're going to create a

play03:33

new file and we're going to call it "requirements.txt".

play03:37

And in here we can use this to list all of the packages

play03:39

or dependencies we need for our project.

play03:42

So here I'm going to add numpy.

play03:45

And now I've gone back to my Python function code

play03:47

and I've included numpy as a dependency.

play03:50

And I've also used it to generate a random matrix

play03:52

so that I can use it as part of my application.

play03:55

And I'll print it out here as well.

play03:56

So now I have a Python hello world application that also uses an external package.

play04:02

And I'm ready to build it into a Docker image.

play04:06

To turn this app into a Docker image, we'll need a Dockerfile.

play04:10

Which is basically just a set of instructions on how to build the image.

play04:14

So go to your image folder and let's create a new file here.

play04:18

We have to call it "Dockerfile".

play04:21

And the name here does matter. It has to be

play04:24

named "Dockerfile" with the capital "D".

play04:27

The first line of our Dockerfile is going to tell us where to base this image on.

play04:33

So we're not building the image from scratch.

play04:36

We're actually going to use this base image,

play04:39

which is an AWS Lambda Python 3.11 image.

play04:43

And then add our stuff into this image to build our own image.

play04:48

And if you want to use a different base image,

play04:50

then the AWS documentation has a list of

play04:52

all the different base images they publish.

play04:55

So you can click in the link below (in the video description)

play04:57

to go to this page and look at all the

play04:59

different Python versions you can choose from.

play05:02

Or if you don't want to use Python, then AWS

play05:04

also provides a bunch of base images in other

play05:07

languages too, such as TypeScript and Java.

play05:10

The next line we need to add to the Dockerfile is

play05:12

to copy this requirements text, which is here.

play05:15

So when we run this Dockerfile, the root directory that it

play05:19

works with is basically wherever this Dockerfile is based.

play05:23

So because the Dockerfile is in the same folder as requirements

play05:26

text, we can just call it requirements text

play05:29

without having to have an absolute path to the file.

play05:32

And we want to copy this into the Lambda "task root".

play05:35

So this is going to be an environment variable that

play05:37

is available to the Dockerfile when it's building.

play05:40

And this is basically where Lambda will consider as the root path for its function.

play05:48

Next, we're going to want to run "pip install" on the

play05:51

requirements text file, which is how we get our

play05:54

numpy dependency installed into the Docker image.

play05:57

My next command is going to copy all the files in this source folder.

play06:02

So that's going to be my main.py application file.

play06:05

And it's also going to put it in the Lambda task root.

play06:09

And the last line is going to set the default command

play06:12

of the Dockerfile to this handler function.

play06:15

This is going to be "main", which is the name of this file,

play06:17

and then "handler", which is the name of the function.

play06:20

So this is what the Lambda function runs when it is used.

play06:23

Now your Dockerfile is ready and you can build an image from it.

play06:27

But before we upload it to AWS, you might want to test it locally.

play06:31

So first go back to your terminal and then navigate

play06:33

to the folder where your Dockerfile is.

play06:36

So that's the "image" folder in this project.

play06:39

And then build your Dockerfile.

play06:41

And you can do that using a command like this.

play06:44

And once you've built your Dockerfile, you have to run it.

play06:47

So you can do that using a command like this.

play06:50

And here I'm going to run it on port 9000.

play06:53

And once the image is running, you can go to another terminal

play06:57

and then send an HTTP request to it like this.

play07:00

And this will actually run the function inside your Dockerfile.

play07:03

So if I run that, you can see that I get my status code 200

play07:07

and my "hello world" message and even my numpy matrix.

play07:11

Now let's go ahead and create the AWS infrastructure

play07:13

that will have the Lambda function and the

play07:16

API endpoint that we can use to run this image.

play07:19

Go to the "lib" folder in your project and you'll find this AWS stack file here.

play07:25

And it should look something like this.

play07:27

So we're going to delete all this commented code because we don't need that.

play07:30

And at the top we're going to import AWS Lambda.

play07:33

And in here, let's create our Docker image function.

play07:39

I'm going to call this Docker function and it's

play07:41

going to be a Lambda "DockerImageFunction".

play07:44

And here are the parameters.

play07:45

For the code, we're going to use Docker image code from asset.

play07:49

And this is going to be a directory where we have the Dockerfile.

play07:52

When we compile this, CDK will go into this folder and

play07:55

it will use this Dockerfile to create an image.

play07:58

And it's going to upload that image and associate it

play08:00

with this Lambda function so that we can use it.

play08:03

For the memory size, I'm going to put 1024MB and then

play08:06

I'm going to give it a timeout of 10 seconds.

play08:10

By default, I think Lambda has quite a low timeout.

play08:13

Maybe it's like 2 seconds and that might not be enough

play08:15

time for a Docker image to start up and run.

play08:17

And for the architecture, I'm going to put architecture "arm64".

play08:21

Now, most of you probably don't need this.

play08:23

So this is probably going to be fine to have it like this.

play08:25

But because I'm running this on a Mac M1, I need the

play08:28

architecture of the Lambda function to match the

play08:31

architecture that I'm building this image with.

play08:34

So when I run it on this machine, it's going to be built with "arm64".

play08:38

If I don't do this, it's going to fail.

play08:40

But you can choose arm64 or the Intel one if you are using a Windows

play08:43

or if you know that you are using an Intel processor.

play08:47

Or just leave it out completely if you're unsure.

play08:49

Now, to make the Lambda function easily accessible with

play08:53

an API endpoint, I'm also going to add a function URL.

play08:57

So to do that, you can use this add function URL

play08:59

method on your Lambda function to create a URL

play09:02

endpoint that you can just use right away.

play09:05

But I'm going to set the arguments here so that auth type is

play09:07

"NONE" so that we don't need to be authenticated to use it.

play09:10

Anyone with a URL will be able to call it.

play09:13

And I'm also going to set the CORS header to allow all

play09:16

HTTP methods and allow all headers and origins.

play09:19

And finally, to actually see the value of this function URL,

play09:22

we're also going to create an output variable for it.

play09:25

So I'm going to do like that so that when we build our CDK application,

play09:29

we actually get this function URL in a string.

play09:32

So this should be all the infrastructure code we need.

play09:35

And because we also have the Python application

play09:38

file ready and the Docker image file

play09:40

ready, we should be ready to deploy this.

play09:43

Now you're ready to deploy your Docker image to AWS.

play09:47

But before you do that, please make sure that

play09:49

you have your AWS CLI ready and configured

play09:52

because you will need the permissions

play09:54

from that to interact with your AWS account.

play09:57

As a sanity check, you can type `aws sts get-caller-identity`.

play10:02

And if your AWS CLI is configured properly, you should get a response like this.

play10:07

Now let's go back to our project root directory and set up CDK.

play10:12

So in your project root folder, type "cdk bootstrap".

play10:16

And here you can even choose the region you

play10:18

want to bootstrap in or leave it blank if you

play10:20

just want to use your default AWS region.

play10:23

Bootstrapping will create a bunch of resources

play10:26

in your AWS account that CDK will need.

play10:30

But you only need to run it once per account per region.

play10:33

And because I've already run it for this account in this region, nothing happens.

play10:38

And that's fine as well.

play10:39

With bootstrapping completed, we can now run

play10:42

"cdk deploy" to deploy our application.

play10:45

And the first time you run this, you might have to press yes

play10:48

to confirm that you do want to create these resources.

play10:51

And you can see here it's actually building and

play10:54

publishing our image to this private repo.

play10:58

After a couple of minutes, it should finish deploying.

play11:01

And you can find the URL for the function right here

play11:04

in this function value output that we've created.

play11:07

And we can try out the function by just clicking on this URL.

play11:12

And here you can see that our function returns "hello world from Lambda".

play11:16

And it's even returned our numpy matrix for us.

play11:19

If you go to your AWS console, you'll see this function created here.

play11:23

And if you click into it, you'll see that there's a function URL.

play11:27

And it's also using this Docker image that we created.

play11:31

Now if you get up to this point and you can't

play11:33

run the function or it has an error like

play11:35

runtime invalid entry point, then double check

play11:38

what architecture you're using because

play11:40

you could be using the wrong Lambda architecture

play11:43

for the machine that you're built with.

play11:46

So in this one, I used an ARM64 architecture

play11:49

because I'm using an M1 Apple silicon Mac.

play11:52

But if you're using an Intel or a Windows computer,

play11:55

you might need to use the x86 architecture instead.

play11:58

Otherwise, I hope this worked for you and that you

play12:01

have a working Lambda Docker Python function.

play12:05

And you can use this as a basis to start developing your

play12:08

app and use whatever packages and dependencies you want.

play12:11

Now Docker Lambda functions are a little bit slower than

play12:14

native Python Lambda functions on the cold-start.

play12:17

The cold-start is when you first use the function after

play12:19

you haven't used it for 15 minutes because Lambda

play12:22

will have to kind of create the whole runtime again.

play12:25

But it is more reliable if you do need to have

play12:27

runtime dependencies because you don't have to

play12:30

worry about having platform specific bugs or

play12:33

errors appear when you move from your development

play12:36

environment to the Lambda cloud environment.

play12:39

This is especially true if you use packages that

play12:41

are heavy on things like math, cryptography

play12:44

or basically anything that tends to use platform

play12:47

specific binaries to do the heavy lifting.

play12:50

So there is a bit of a trade off, but if you want

play12:52

an easier development experience and you

play12:55

don't mind the slower cold-start, then I do think

play12:57

that Docker functions are the way to go.

play13:00

Anyways, I hope you enjoyed this video. You

play13:02

can check in the comments for the code for

play13:04

this project. And thank you for watching.

Rate This

5.0 / 5 (0 votes)

Related Tags
DockerAWS LambdaServerlessPythonDeploymentCloud ComputingLambda FunctionsDocker ContainersAPI EndpointsCDK