Issue
My tech stack is Django, Zappa, AWS lambda(+ API gateway, cloudwatch, more....).
The maximum size of a function code package for AWS lambda
is 50MB
.
But my package size is 47MB
.
How can I reduce the package size?
Solution
The first step would be to figure out what exactly is taking up so much space. Run zappa package
and inspect the resulting ZIP file. Add anything that's not necessary to the exclude
list in in your zappa_settings
. Note that this functionality is somewhat limited in what it can exclude; see this article how to add a regex_excludes
option that can remove more files via regex matching.
The biggest thing though is probably going to be static assets. Serve them from S3 instead of through your Lambda Django server. The django-s3-storage package is very useful. Install it, and configure it in your settings.py
something like this:
STATICFILES_STORAGE = 'django_s3_storage.storage.StaticS3Storage'
AWS_S3_BUCKET_NAME_STATIC = os.environ['STATIC_BUCKET']
AWS_S3_CUSTOM_DOMAIN = os.environ.get('AWS_S3_CUSTOM_DOMAIN', f'{AWS_S3_BUCKET_NAME_STATIC}.s3.amazonaws.com')
STATIC_URL = f'https://{AWS_S3_CUSTOM_DOMAIN}/'
(I'm using environment variables from the Zappa settings file to customise this per stage, you can configure this however you want.)
Then after deploying to Lambda, run another command to deploy your static files:
$ zappa update
$ ./manage.py collectstatic
Make sure the static files are excluded from the Lambda package as described above. With a combination of these techniques, you should be able to get your package size down; my deployments are currently around 25 MB in size.
Answered By - deceze
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.