Last active
March 6, 2025 05:43
-
-
Save sheep-snow/b3338006a9849ea80217c68a7bd6fa88 to your computer and use it in GitHub Desktop.
using-uv-with-python-lambda-image-on-ecr-public-repo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # AWS Lambda 関数の公式コンテナイメージ https://gallery.ecr.aws/lambda/python で | |
| # uv https://docs.astral.sh/uv/ を使って python パッケージ管理と実行を可能にする最小限の例 | |
| FROM public.ecr.aws/lambda/python:3.12 | |
| # uv を公式イメージからCOPYする方法でインストールする | |
| # https://docs.astral.sh/uv/guides/integration/docker/#installing-uv | |
| COPY --from=ghcr.io/astral-sh/uv:latest /uv /bin/uv | |
| ENV PATH="/app/.venv/bin:$PATH" | |
| # AWS Lambda は書込可能なディレクトリが `/tmp` 配下だけなので uv の cache も同ディレクトリ配下を使うように変更する | |
| # https://docs.astral.sh/uv/configuration/environment/ | |
| ENV UV_CACHE_DIR=/tmp/.uv_cache | |
| # uv の lockfile からプロジェクトの python パッケージ をインストールする | |
| COPY pyproject.toml uv.lock ${LAMBDA_TASK_ROOT}/ | |
| RUN uv sync | |
| # Lambda関数のコード `./src/` をイメージに含める | |
| COPY src/ ${LAMBDA_TASK_ROOT}/ | |
| # AWS Lambda イメージのデフォルトのエントリーポイント `lambda-entrypoint.sh` を | |
| # uv の仮想環境で実行させることで sync したパッケージが利用可能なコンテキストでLambdaを実行させる | |
| ENTRYPOINT ["uv", "run", "/lambda-entrypoint.sh"] | |
| # `lambda-entrypoint.sh` に `./src/lambda_handler.py` の `handler(event, context)` を | |
| # Lambda関数のエントリーポイントとして指定する例。プロジェクトに合わせてお好みで | |
| # CMD ["lambda_handler.handler"] |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
build example.