🐾Lambda development - Container function🐾
❓All Lambda layers should take no more than 250 Mb in total and it can be a problem with heavy ML libraries. With Container functions get 10 GB for your libraries and other artefacts.
Steps to create Docker image for Lambda and push it to ECR:
1️⃣ Create Dockerfile and specify AWS base image to start with. You can use your own base image, but you need to install the Python runtime interface client.
FROM public.ecr.aws/lambda/python:3.9
2️⃣ Install dependencies from requirements.txt file. If you don’t want to create requirements.txt
file, you can install dependencies with pip3 command.
COPY requirements.txt .
RUN pip3 install -r requirements.txt --target "${LAMBDA_TASK_ROOT}"
3️⃣ Copy you Python code and all other dependencies to container
COPY app.py ${LAMBDA_TASK_ROOT}
COPY artifact.json ${LAMBDA_TASK_ROOT}
4️⃣ Set the CMD, so your container can find and run your handler function (your app.py code should have the same structure as your usual Lambda function code)
CMD [ "app.handler" ]
5️⃣ Login to the ECR using the following command (otherwise you won’t be able to push the image)
docker login -u AWS -p ${aws ecr get-login-password --region <YOUR_REGION>} <YOUR_ACCOUNT>.dkr.ecr.<>YOUR_REGION.amazonaws.com
6️⃣ Run command to build your docker image and push it to ECR
docker build -t docker push <AWS_ECR_REPOSITORY_URL>:latest .
docker push <AWS_ECR_REPOSITORY_URL>:latest
If you like this post, you can share APAWS newsletter with friends: