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

WhiteNoise + Lambda + Zappa #164

Closed
efimerdlerkravitz opened this issue Dec 10, 2017 · 7 comments
Closed

WhiteNoise + Lambda + Zappa #164

efimerdlerkravitz opened this issue Dec 10, 2017 · 7 comments

Comments

@efimerdlerkravitz
Copy link

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 ?

@evansd
Copy link
Owner

evansd commented Dec 11, 2017

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.

@efimerdlerkravitz
Copy link
Author

efimerdlerkravitz commented Dec 11, 2017

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 which zappa 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 ?

@evansd
Copy link
Owner

evansd commented Dec 11, 2017

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/'

@efimerdlerkravitz
Copy link
Author

Works ! Where do you want me to add the instructions ?

@evansd
Copy link
Owner

evansd commented Dec 11, 2017

Great. Just add them to this ticket for now so at least they'll be easy to find later. Thanks.

@efimerdlerkravitz
Copy link
Author

Using WhiteNoise, Zappa and Django

  • Assuming you are using a production profile in zappa_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

@ptrhck
Copy link

ptrhck commented Apr 16, 2019

Did you try your config using the STATIC_HOST with cloudfront as CDN?

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

No branches or pull requests

3 participants