WhiteNoise + Lambda + Zappa
efimerdlerkravitz opened this issue · comments
Hello Guys,
We are using Zappa + Django in AWS Lambda. I really like the idea of using whitenoise, at least for serving our Django admin pages, it sounds to me like a simple solution that does not require a lot of configuration (like similar S3 solutions).
I followed the tutorial and tried the following with no avail:
settings.py
-----------
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
$ zappa manage production collectstatic
...
error stack
...
Read-only file system: '/var/task/staticfiles'
Well makes sense, from what I know the only writeable directory is /tmp so I tried
STATIC_URL = '/static/'
STATIC_ROOT = /tmp/staticfiles'
...
$ zappa manage production collectstatic
61 static files copied to '/tmp/staticfiles'.
[END] RequestId: 4b9c1c60-ddc4-11e7-a97d-e193c05b0571
[REPORT] RequestId: 4b9c1c60-ddc4-11e7-a97d-e193c05b0571
Duration: 77.20 ms
Billed Duration: 100 ms
Memory Size: 512 MB
Max Memory Used: 44 MB
When go to my-site.com/admin
I'm getting 403 when trying to retrieve the resources which again makes sense.
So what is the correct way to implement it ?
I haven't used this myself, but I think that collectstatic
needs to get run before Zappa packages up your application so that the static files get included with the rest of the project files.
It should be possible to configure Zappa to do this automatically using the prebuild_script
setting, but it might also work to just run collectstatic
locally before running zappa deploy
.
If you do get a working configuration it would be great if you could update this ticket with the details so I could add it to the documentation.
Thank you @evansd . My findings until now:
- I did collectstatic locally and then ran
zappa update
. - I've notices that when I use
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
I'm getting 403, to the following url https://4in6uw7c2b.execute-api.us-east-1.amazonaws.com/static/admin/css/base.css
- But when adding
production
(on whichzappa update
runs) I'm getting the file successfully, e.g.https://4in6uw7c2b.execute-api.us-east-1.amazonaws.com/production/static/admin/css/base.css
- I changed the static url to
/production/static/
and redeployed, now I'm getting 404 on the above resource.
STATIC_URL = '/production/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
Am I missing something in configuration ?
Is your Django app running on the root of the domain, or under the /production
path?
When running at the domain root everything works out of the box, but if you're running under a particular path you need to configure WHITENOISE_STATIC_PREFIX explicity e.g:
STATIC_URL = '/production/static/'
WHITENOISE_STATIC_PREFIX = '/static/'
Works ! Where do you want me to add the instructions ?
Great. Just add them to this ticket for now so at least they'll be easy to find later. Thanks.
Using WhiteNoise, Zappa and Django
- Assuming you are using a
production
profile inzappa_settings.json
- Under
settings.py
add
STATIC_URL = '/production/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
WHITENOISE_STATIC_PREFIX = '/static/'
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
- Run locally
./manage.py collectstatic
- Run
zappa update
Did you try your config using the STATIC_HOST with cloudfront as CDN?