I have a trivial python lambda function defined in index.py
:
def handler(event, context):
return {"msg": "hello world. this is hello handler"}
Deployed via CDK (typescript):
const stack = new Stack(app, "PythonHelloStack", {env})
new PythonFunction(stack, `HelloFunction`, {
runtime: Runtime.PYTHON_3_9,
entry: path.join(__dirname, `../../../lambdas/hello`),
})
This works and is 4.8 kB. Great. If I add a single dependency to psycopg2-binary
, without changing the Python code, the AWS Lambda code size jumps from 4.8 kB to 3.2 MB. Is that inevitable or is there a fix? Can I do anything to reduce the code size? Should I? Is creating a layer necessary or helpful for this? Is there a simpler fix? Thank you :)
My project with the psycopg2-binary
dependency has the following pyproject.toml
:
[tool.poetry]
name = "hello"
version = "0.1.0"
description = ""
authors = []
[tool.poetry.dependencies]
python = "~3.9"
psycopg2-binary = "~2.9"
[tool.poetry.dev-dependencies]
[build-system]
requires = ["poetry-core>=1.1.0"]
build-backend = "poetry.core.masonry.api"