-
Go to the Get Docker page on the Docker website. Choose the Docker Desktop application that meets your requirements. Follow the directions provided to install Docker Desktop.
-
On your local machine, create one folder with three files: Dockerfile, requirements.txt with libraries, and app.py with import statements.
Note: Use the latest version of Python for Amazon Elastic Container Registry (Amazon ECR) public registry.
The following example Dockerfile uses Python 3.8:
FROM public.ecr.aws/lambda/python:3.8
# Copy function code
COPY app.py ${LAMBDA_TASK_ROOT}
# Install the function's dependencies using file requirements.txt
# from your project folder.
COPY requirements.txt .
RUN pip3 install -r requirements.txt --target "${LAMBDA_TASK_ROOT}"
# Set the CMD to your handler (could also be done as a parameter override outside of the Dockerfile)
CMD [ "app.handler" ]
The following example app.py file includes an example import statement:
import sysdef handler(event, context):
return 'Hello from AWS Lambda using Python' + sys.version + '!'
-
Build your Docker image with the docker build command and an image name.
This is a docker build command with a "hello-world" example:
docker build -t hello-world .
Note: If you get an authentication error when pulling from the Amazon ECR public repository, then see Amazon ECR Public troubleshooting.
-
Start the Docker image with the docker run command.
This is a docker run command with a "hello-world" example:
docker run -p 9000:8080 hello-world
-
Test your application with the Lambda Runtime Interface Emulator (RIE) from AWS on the GitHub website. From a new terminal window, run a curl command to post an event to the following endpoint:
curl -XPOST "http://localhost:9000/2015-03-31/functions/function/invocations" -d '{}'
This command invokes the function running in the container image and then returns a response.
-
Authenticate the Docker CLI to your Amazon ECR registry.
Change the account ID and the AWS Region in the command to your account ID and Region.
This is an example command to authenticate the Docker CLI to the Amazon ECR registry:
aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin 123456789012.dkr.ecr.us-east-1.amazonaws.com
-
Create a repository in Amazon ECR using the create-repository command.
This is an example create-repository command:
aws ecr create-repository --repository-name hello-world --image-scanning-configuration scanOnPush=true --image-tag-mutability MUTABLE
-
Tag your image to match your repository name with the docker tag command. Then, deploy the image to Amazon ECR with the docker push command.
Change the account ID and the AWS Region in the command to your account ID and Region.
This is an example docker tag command:
docker tag hello-world:latest 123456789012.dkr.ecr.us-east-1.amazonaws.com/hello-world:latest
This is an example docker push command:
docker push 123456789012.dkr.ecr.us-east-1.amazonaws.com/hello-world:latest
-
With your container image in the Amazon ECR container registry, create and run a Lambda function. For more information, see Create a Lambda function using a container image.