Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python lambda is 2.6mb when there is 0 dependencies #5566

Open
michaelbrewer opened this issue Oct 13, 2020 · 20 comments
Open

Python lambda is 2.6mb when there is 0 dependencies #5566

michaelbrewer opened this issue Oct 13, 2020 · 20 comments
Labels
feature-request Request a new feature functions Issues tied to the functions category p3

Comments

@michaelbrewer
Copy link
Contributor

michaelbrewer commented Oct 13, 2020

Describe the bug
When create a function with a very simple Python lambda, the generated lambda is 2MB

Amplify CLI Version

4.30.0

To Reproduce

Take pointless lambda like this and build it.

def handler(event, context):
     print("event:", event)

Expected behavior
The deployed lambda should only be a couple bytes not 2.6mb

Screenshots

image

Desktop (please complete the following information):

  • OS: [e.g. Mac/Windows/Ubuntu]
    Mac
  • Node Version. You can use node -v to check the node version on your system
    v14.13.1

Additional context
Add any other context about the problem here.

@michaelbrewer michaelbrewer changed the title Python lambda size when 0 dependencies Python lambda is 2.6mb when there is 0 dependencies Oct 13, 2020
@yuth yuth added functions Issues tied to the functions category question General question labels Oct 13, 2020
@yuth
Copy link
Contributor

yuth commented Oct 13, 2020

Amplify CLI uses pipenv to bundle the resources to the cloud. Pipenv brings in virtual env, which adds size to the bundle.

@michaelbrewer
Copy link
Contributor Author

@yuth
What is the benefit of this?
Why not just use AWS Sam to handle the bundling of the lambda? even CDK does not inflate the lambda by 2.6mb with zero benefits.

And FYI, Amplify does not even support Python 3.8

@edwardfoyle
Copy link
Contributor

@michaelbrewer You're right that pipenv is overkill for small functions that only have a couple dependencies, but it is good for managing functions with lots of dependencies. It is also a well adopted package management tool in the python ecosystem: https://packaging.python.org/tutorials/managing-dependencies/. We don't use AWS SAM because many of our customers don't use / have SAM installed.

We do support Python 3.8, so if you are having trouble there please open an issue with details of the problem.

@michaelbrewer
Copy link
Contributor Author

@edwardfoyle -

amplify console does not support Python 3.8
aws-amplify/amplify-hosting#595

Most lambdas don't need many dependencies (and some have none at all outside of AWS SDK)

So in most cases just have a requirements.txt should be all that you would need.

Larger projects can use poetry.

@michaelbrewer
Copy link
Contributor Author

@edwardfoyle
i don't quite understand why the actual deployed lambda needs to be 2.6mb, these are not runtime dependencies but build time dependencies?

Here is the example lambda for python function for a cognito auth challenge:

def handler(event: dict, _):
    print("event", event)

    if len(event["request"]["session"]) == 0:
        event["request"]["challengeMetadata"] = "COOKIE_CHALLENGE"
        event["request"]["publicChallengeParameters"] = {}
        event["request"]["publicChallengeParameters"]["cookieName"] = "source"
        event["request"]["privateChallengeParameters"] = {}

    return event

I would expect the deployed lambda to be tiny

@michaelbrewer
Copy link
Contributor Author

Note how AWS-CDK does this without bloating the lambda with unused libraries : https://github.com/aws/aws-cdk/tree/master/packages/%40aws-cdk/aws-lambda-python

As a develop you just need to have Docker installed.

@michaelbrewer michaelbrewer changed the title Python lambda is 2.6mb when there is 0 dependencies Python lambda is 2.6mb when there is 0 dependencies [BUG] Oct 20, 2020
@michaelbrewer
Copy link
Contributor Author

@edwardfoyle @yuth - will anyone look into fixing this?

I can see that AWS does care about the cost of cold starts (https://aws.amazon.com/blogs/developer/modular-aws-sdk-for-javascript-release-candidate/), and this seems to be inconsistent.

@kaustavghosh06
Copy link
Contributor

@michaelbrewer We'll look into this. Sorry for the late response.

@michaelbrewer
Copy link
Contributor Author

Maybe @heitorlessa has some input on how to support Python lambdas in a clean way. It would be nice to support SAM or do what CDK does when building Lambda functions (while keeping the size down)

@edwardfoyle edwardfoyle removed their assignment Jan 25, 2021
@michaelbrewer
Copy link
Contributor Author

Hopefully the tools improves soon. As we are planing to build all of our lambdas in Python

@heitorlessa
Copy link
Contributor

@kaustavghosh06 while I don't have the bandwidth to contribute code I'm happy to review or chat

You can keep pipenv but only extract the dependencies themselves instead of bringing the whole virtual env - it's not necessary.

pipenv here will also break when customers bring dependencies that rely on C, as it needs to build within a Linux env -- hence Docker suggestion by Michael (a flag would do).

@michaelbrewer
Copy link
Contributor Author

Thanks @heitorlessa for offering an ear for this.

I do like how sam can bootstrap a lambda with the build tools and sample code:

sam init --location https://github.com/aws-samples/cookiecutter-aws-sam-python

Maybe this can be an option @kaustavghosh06 . When can we expect to at least not bring the whole virtual env with the lambda?

@michaelbrewer
Copy link
Contributor Author

@kaustavghosh06 what is the timeline on this? Otherwise amplify function build does not seem to work, it just hangs without completing.

@michaelbrewer
Copy link
Contributor Author

michaelbrewer commented Feb 13, 2021

RE: amplify function build failing

Oh i see that this is a separate issue (which is NOT being fixed):

@michaelbrewer
Copy link
Contributor Author

@eddiekeller @kaustavghosh06 - is anyone looking into this? How easy would it be to fix this ourselves in the CLI?

@michaelbrewer
Copy link
Contributor Author

How about doing something along the lines of :

# Create an requirements.txt in src/ (which can be gitignored)
pipenv lock -r > src/requirements.txt

Then build/download the dependencies using docker image lambci/lambda:build-python3.8

# Download the vendored deps
pip install -r requirements.txt -t /vendored && cp -au . /vendored

@siegerts siegerts added the feature-request Request a new feature label Sep 3, 2021
@samjett247
Copy link

Found a hack to make amplify stop building all these nondependent-dependencies into my Python functions. This will only work for functions that do not have any dependencies that aren't already provided in lambda layers, as it just completely stops the function build process. There is an amplify.state file inside of each function. If you don't want amplify to build your function, modify the amplify.state file to make amplify "think" its a nodejs function.

amplify.state (original, will build virtualenv into the deployed package)

{
  "pluginId": "amplify-python-function-runtime-provider",
  "functionRuntime": "python",
  "useLegacyBuild": false,
  "defaultEditorFile": "src/index.py"
}

amplify.state (new - will NOT build any python packages but otherwise the Lambda function will work the same in AWS)

{
  "pluginId": "amplify-nodejs-function-runtime-provider",
  "functionRuntime": "nodejs",
  "useLegacyBuild": false,
  "defaultEditorFile": "src/index.py"
}

@tmirun
Copy link

tmirun commented Dec 31, 2021

Same Issue Here

@InnovateWithEric InnovateWithEric changed the title Python lambda is 2.6mb when there is 0 dependencies [BUG] Python lambda is 2.6mb when there is 0 dependencies Mar 7, 2022
@joekiller
Copy link
Contributor

There is a comment on an alternative way to run pipenv in the following comment which allows installation of dependencies without copying the virtualenv. Ideally the amplify project may use this method instead:

pypa/pipenv#746 (comment)

@speedhawk21
Copy link

Amplify is still including the pipenv venv bundle in function deployments? Is this a joke?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature-request Request a new feature functions Issues tied to the functions category p3
Projects
None yet
Development

No branches or pull requests