https://pantsbuild.org/ logo
#general
Title
# general
b

brave-hair-402

03/01/2023, 10:15 PM
Hey there, I was able to use pants to create docker images that function well on AWS Lambda (python 3.10) and follow the best practices outlined in this article (https://blog.pantsbuild.org/optimizing-python-docker-deploys-using-pants/). Just thought I'd post the relevant build file in this thread in case anyone else is facing a similar problem. Big thanks to @enough-analyst-54434 for helping me get started on this!
🙏 1
Copy code
python_sources()

python_requirement(
    name="awslambdaric",
    requirements=["awslambdaric==2.0.4"],
)

pex_binary(
    name="bin-source",
    entry_point="app.py",
    layout="packed",
    include_requirements=False,
    include_tools=True,
)

pex_binary(
    name="bin-deps",
    entry_point="app.py",
    dependencies=[":awslambdaric"],
    layout="packed",
    include_sources=False,
    include_tools=True,
    dependencies=[":bin-source"]
)

docker_image(
    name="img-deps",
    repository="my_service-api_lambda",
    registries=["company_name"],
    image_tags=["deps"],
    skip_push=True,
    instructions=[
        "FROM python:3.10-slim-buster as deps",
        "COPY projects.my_service.lambdas/bin-deps.pex /bin-deps.pex",
        "RUN PEX_TOOLS=1 /usr/local/bin/python3.10 /bin-deps.pex venv --scope=deps --compile /app",
    ],
    source=None,
    dependencies=[":bin-deps"]
)

docker_image(
    name="img-srcs",
    repository="my_service-api_lambda",
    registries=["company_name"],
    image_tags=["srcs"],
    skip_push=True,
    instructions=[
        "FROM python:3.10-slim-buster as srcs",
        "COPY projects.my_service.lambdas/bin-source.pex /bin-srcs.pex",
        "RUN PEX_TOOLS=1 /usr/local/bin/python3.10 /bin-srcs.pex venv --scope=srcs --compile /app",
    ],
    source=None,
)

docker_image(
    name="my_service_api_lambda",
    repository="my_service",
    dependencies=[":img-srcs", ":img-deps"],
    registries=["@ecr_test"],
    image_tags=["api_lambda-{pants.hash}"],
    instructions=[
        "FROM python:3.10-slim-buster",
        "COPY --from=company_name/my_service_api_lambda:deps /app /app",
        "COPY --from=company_name/my_service_api_lambda:srcs /app /app",
        "WORKDIR /app",
        'ENTRYPOINT [ "/app/bin/python", "-m", "awslambdaric" ]',
        'CMD ["lambdas.app.handler"]',
    ],
    source=None,
)
5 Views