Generate a Dockerfile from an AWS lambda function
pip install lambda2docker
$ lambda2docker --lambda-name my-lambda-function --region us-east-1 --flavor alpine
Dockerfile generated in output directory /tmp/tmpnl30z9us
or using docker:
$ docker run --rm -it -v "/root/.aws:/root/.aws:ro" -v "/tmp":/tmp plazzari/lambda2docker --lambda-name my-lambda-function --region us-east-1 --flavor alpine
Dockerfile generated in output directory /tmp/tmpnl30z9us
Let's say we have deployed this Python function:
# lambda_function.py
import requests
def lambda_handler(event, context):
print("Hello from lambda")
alongside a layer which contains requests
.
To generate a Dockerfile:
$ lambda2docker --lambda-name my-lambda-function --region us-east-1 --flavor alpine
Dockerfile generated in output directory /tmp/tmpnl30z9us
Let's inspect what was generated:
$ tree /tmp/tmpnl30z9us
/tmp/tmpnl30z9us
├── Dockerfile
├── src
│ ├── lambda_function.py
│ └── requirements.txt
└── wrapper_entrypoint.py
1 directory, 4 files
$ cat /tmp/tmpnl30z9us/Dockerfile
FROM python:3.8-alpine
WORKDIR /data
ADD src src
ADD wrapper_entrypoint.py wrapper_entrypoint.py
RUN pip install -r src/requirements.txt
ENTRYPOINT ["python3"]
$ cat /tmp/tmpnl30z9us/src/requirements.txt
requests==2.30.0
This Dockerfile is buildable:
$ pushd /tmp/tmpnl30z9us
$ docker build -t my-lambda-container .
$ popd
[x] python3.7, python3.8, python3.9, python3.10
[] TODO: nodejs12.x, nodejs14.x, nodejs16.x, nodejs18.x
[] TODO: java8, java8.al2, java11, java17
[] TODO: dotnet5.0, dotnet6, dotnet7, dotnetcore3.1
[] TODO: go1.x
[] TODO: ruby2.7