Deploy Django to AWS part 2 - Hosting Files on S3

Continued from part 1: deploy-django-to-aws.

I’m learning from this tutorial.

Set up s3 bucket

Go to https://console.aws.amazon.com/s3/home and click “Create Bucket”

Once your bucket is created click on “Permissions”:

Then hit “Edit bucket policy”. This is our bucket policy:

1
2
3
4
5
6
7
8
9
10
11
12
{
"Statement": [
{
"Principal": {
"AWS": "*"
},
"Effect": "Allow",
"Action": "s3:*",
"Resource": ["arn:aws:s3:::bucket-name/*", "arn:aws:s3:::bucket-name"]
}
]
}

I got the policy from this Stack Overflow post.

Where cptmusicblog is the name of my bucket. Add a file and double click on it. You should be able to see it publicly. Here is a url for an image I uploaded:

https://s3-us-west-2.amazonaws.com/cptmusicblog/splash.png

Set up django to get static files from s3

Install some packages and add them to requirements.txt:

1
2
$ pip install django-storages boto
$ pip freeze > requirements.txt

Add “storages” to INSTALLED_APPS in iotd/iotd/settings.py:

1
2
3
4
5
6
7
8
9
10
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'images',
'storages',
)

Then add more to settings.py:

1
2
3
4
5
6
7
8
AWS_HEADERS = {
'Expires': 'Thu, 31 Dec 2099 20:00:00 GMT',
'Cache-Control': 'max-age=94608000',
}
AWS_STORAGE_BUCKET_NAME = 'BUCKET_NAME'
AWS_ACCESS_KEY_ID = 'xxxxxxxxxxxxxxxxxxxx'
AWS_SECRET_ACCESS_KEY = 'yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy'

The AWS_HEADERS means that AWS tells browsers they can cache the files until 2099

which happens to be in

Get your access credentials by clicking your username dropdown > “Security credentials” > “Users”.

If you haven’t created a user you have to do that. Then click Manage Access Keys. Then Create Access Key. We can only view our private key once. Download the keys as a CSV file. Keep these secret so no one exploits them by mining bitcoin on your account with your credentials (do not push them to github).

Also add these lines to settings.py:

1
2
3
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'

CORS configuration

Go to S3 bucket preferences and under “edit CORS configuration” paste this in:

1
2
3
4
5
6
7
8
<CORSConfiguration>
<CORSRule>
<AllowedOrigin>*</AllowedOrigin>
<AllowedMethod>GET</AllowedMethod>
<MaxAgeSeconds>3000</MaxAgeSeconds>
<AllowedHeader>Authorization</AllowedHeader>
</CORSRule>
</CORSConfiguration>

Try it

1
2
$ python iotd/manage.py collectstatic
$ eb deploy cptmusicblog-dev