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

Reduce Pydantic package size with no-binary flag when support is out #1078

Closed
zrothberg opened this issue Nov 3, 2021 · 9 comments
Closed
Assignees
Labels

Comments

@zrothberg
Copy link

I was reading through the docs and saw that pydantic adds over 70 mbs. Poking around the pydantic repo there are a few threads about this subject. pydantic/pydantic#2276 pydantic/pydantic#2517. I was wondering if it was possible to use the --no-binary flag in your package level requirements.txt. I had seen this comment https://github.com/awslabs/aws-lambda-powertools-python/issues/481#issuecomment-863907997 and am unsure if the same issue that prevents the smaller cython binaries would prevent this from working. I also saw that poetry doesn't allow the --no-binary flag so wasn't sure if that could be a reason that preventing this from being recommended. Or if this works but wasn't mentioned in the docs due to speed concerns.

@boring-cyborg
Copy link

boring-cyborg bot commented Nov 3, 2021

Thanks for opening your first issue here! We'll come back to you as soon as we can.

@heitorlessa
Copy link
Contributor

hey @zrothberg thanks for bringing this up! As you pointed out, there are two angles to this from what I could tell after some experimentation.

Publisher. Poetry didn't support the --no-binary flag (maybe it does now?). We'd have to experiment exporting extra dependencies as requirements from pyproject.toml, then install using --no-binary flag. I did find a workaround but found I needed more time to test it thoroughly since this project is used in production by hundreds of customers

Direct installs. SAM CLI doesn't support propagating pip flags, nor did propagate PIP flags via env var like PIP_NO_BINARY when using --use-container. I found that recommending customers to define those extra dependencies in requirements to be sub-optimal, as it can easily get out of date if we need to add more later.

Now that we have a reliable Layers infrastructure created by @am29d, maybe this could be worked around as a Lambda Layer since we have more control on how to package it (e.g. we could use a Docker Amazon Linux 2 + pip with no-binary flag).

For now, I'm actively following that Poetry issue as this provides a more reliable solution long-term. If you do find a better mechanism, by all means I'd love to get this improved indeed.

Thanks again!

@heitorlessa heitorlessa changed the title Pydantic size Reduce Pydantic package size with no-binary flag when support is out Nov 12, 2021
@heitorlessa heitorlessa transferred this issue from aws-powertools/powertools-lambda-python Nov 13, 2021
@zrothberg
Copy link
Author

@heitorlessa so I actually found a work around for this today. Appears someone on the workaround you commented on also found a similar thing. I was poking around the pip documentation and saw that it also allows you to specify the exact url to download a package from. If you use the py3-none-any.whl link from the pypi folder this will install without the binaries safely. I dropped 10 mbs off my zipped lambda and another 40mbs off my running size doing this. It's a reliable solution to the problem that doesn't cause weird issues. Only thing that may be annoying is dependabot won't be able to update dependencies automatically.

@heitorlessa
Copy link
Contributor

heitorlessa commented Feb 22, 2022

cc @am29d ^^ o//. Though I think with the work of moving the Lambda Layer pipeline to AWS Code*, we can use pip flags to correctly address this without impacting dependabot upgrades.

@boring-cyborg
Copy link

boring-cyborg bot commented Mar 17, 2022

Thanks for opening your first issue here! We'll come back to you as soon as we can.

@heitorlessa heitorlessa transferred this issue from aws-powertools/powertools-lambda Mar 17, 2022
@heitorlessa heitorlessa added pending-release Fix or implementation already in dev waiting to be released and removed python pending-release Fix or implementation already in dev waiting to be released labels Apr 12, 2022
@heitorlessa heitorlessa assigned rubenfonseca and unassigned am29d Oct 14, 2022
@heitorlessa
Copy link
Contributor

assigning to @rubenfonseca as we've managed to make a HUGE improvement for V2, getting down to Pydantic fully optimized with Cython, yet ~2MB final Lambda Layer with ALL deps installed!

@rubenfonseca
Copy link
Contributor

We are now close to release the v2 layer with all the optimizations mentioned on this issue, thank you so much for your great tips! You can check the current implementation on our CDK Layer construct: https://github.com/awslabs/cdk-aws-lambda-powertools-layer/blob/main/layer/Python/Dockerfile#L31-L47

@heitorlessa
Copy link
Contributor

Closing as we're wrap to launch V2

@github-actions
Copy link
Contributor

⚠️COMMENT VISIBILITY WARNING⚠️

This issue is now closed. Please be mindful that future comments are hard for our team to see.

If you need more assistance, please either tag a team member or open a new issue that references this one.

If you wish to keep having a conversation with other community members under this issue feel free to do so.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants