Skip to content

Instantly share code, notes, and snippets.

@tomdyson
Last active May 29, 2019 14:49
Show Gist options
  • Save tomdyson/79e3827a0f3b52221ba1d14ec3a1987e to your computer and use it in GitHub Desktop.
Save tomdyson/79e3827a0f3b52221ba1d14ec3a1987e to your computer and use it in GitHub Desktop.
Wagtail on Lambda with RDS

Wagtail on AWS Lambda, with Zappa and RDS

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 zappa_rds

Set up Zappa. Accept the defaults, but set the project settings to zappa_rds.settings.dev. Make a note of your bucket name.

cd zappa_rds
zappa init

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

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

Now create a PostgreSQL instance in the RDS console, and call it wagtail-zappa-rds. Make sure it is in the same region that your Lambda will be deployed to (check "aws_region" in your zappa_settings.json). Make it publically accessible for now, but use a strong password. Create a database named wagtail_zappa_db. When it's ready, update zappa_rds/settings/dev.py with

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'wagtail_zappa_db',
        'USER': 'your-username',
        'PASSWORD': 'your-password',
        'HOST': 'your-endpoint',
        'PORT': '5432',
    }
}

Install pyscopg2: pip install psycopg2

Deploy the site with zappa deploy dev. Zappa may create a new security group for your RDS instance. Edit this to allow all inbound traffic. Then migrate, and create an admin user:

zappa manage dev migrate
zappa manage dev create_admin_user

Set up static files:

pip install django-storages boto

Add and configure django-storages, in settings/zappa_rds/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