Skip to content

Instantly share code, notes, and snippets.

@tomdyson
Last active June 20, 2023 02:22
Show Gist options
  • Star 14 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tomdyson/16b96cbe597c3974a4c309b0b91d8847 to your computer and use it in GitHub Desktop.
Save tomdyson/16b96cbe597c3974a4c309b0b91d8847 to your computer and use it in GitHub Desktop.
Wagtail on AWS Lambda, with Zappa

Wagtail on AWS Lambda, with Zappa

Install Wagtail and Zappa, and create an empty site:

pip install wagtail zappa zappa-django-utils
pip install pip==9.0.3 # see https://github.com/Miserlou/Zappa/issues/1471
wagtail start foo

Set up Zappa. Set the project settings to foo.settings.dev. Make a note of your bucket name.

cd foo
zappa init

Allow all hosts for now, and include zappa_django_utils in your INSTALLED_APPS:

echo -en "\n\nALLOWED_HOSTS = ['*']" >> foo/settings/dev.py
echo -en "\n\nINSTALLED_APPS += ('zappa_django_utils',)" >> foo/settings/base.py

Add Zappa's SQLite backend to foo/settings/dev.py:

DATABASES = {
    'default': {
        'ENGINE': 'zappa_django_utils.db.backends.s3sqlite',
        'NAME': 'sqlite.db',
        'BUCKET': 'your-bucket-name'
    }
}

Deploy the site, migrate the database and create an admin user:

zappa deploy dev
zappa manage dev migrate
zappa manage dev create_admin_user

Finally set up static files:

pip install django-storages boto

Add and configure django-storages, in settings/foo/dev.py:

INSTALLED_APPS += ('storages',)
# use the bucket that was created in `zappa init`
AWS_STORAGE_BUCKET_NAME = 'your-bucket-name'
AWS_S3_CUSTOM_DOMAIN = '%s.s3.amazonaws.com' % AWS_STORAGE_BUCKET_NAME
STATIC_URL = "https://%s/" % AWS_S3_CUSTOM_DOMAIN
STATICFILES_STORAGE = 'storages.backends.s3boto.S3BotoStorage'
# Don't do this! It's convenient but insecure to use the same 
# bucket for static files and media.
DEFAULT_FILE_STORAGE = 'storages.backends.s3boto.S3BotoStorage'

Update and collect static.

zappa update dev
zappa manage dev "collectstatic --noinput"

Fonts probably won't work until you set a CORS policy on your bucket. Go to your S3 bucket properties, and under "Permissions", click on "Add CORS Configuration", and replace the contents with:

<?xml version="1.0" encoding="UTF-8"?>
<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
  <CORSRule>
    <AllowedOrigin>*</AllowedOrigin>
    <AllowedMethod>GET</AllowedMethod>
    <AllowedMethod>HEAD</AllowedMethod>
  </CORSRule>
</CORSConfiguration>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment