Speed up static resources with AWS CloudFront CDN

Nowadays CDN is easy to use. Even a startup or personal website shall consider use CDN to speed up static resources, such as photos, javascript/css files. Here are simple steps to use AWS CloudFront CDN and enable it in Tornado.

Enable AWS CDN with Custom Origin option

  1. Open CloudFront in AWS Management Console. If you don’t have AWS account, create it at https://aws.amazon.com
  2. Click ‘Create Distribution‘.
  3. Select the option ‘Download‘. Click Continue.
  4. Set ‘Origin Domain Name‘ to your web server domain.
  5. Set ‘Forward Query Strings‘ option to ‘Yes‘. This is because tornado can generate a hash code for each static resource, for best cache performance.
  6. Set other options accordingly. Click ‘Create Distribution‘. Your CDN distribution is ready to use, once its state becomes Enabled.
  7. Record the Domain Name field. We will use it soon. It normally looks like d1234.cloudfront.net

Enable it easily with python tornado

  1. First, apparently we don’t want to use CDN in dev mode. To easily switch between dev and production mode, customize your base RequestHandler like below

We override the static_url method here. Custom the is_cdn_enabled method. Let it depends on the production mode. Then it will automatically employ CDN in production deployment. Enjoy coding!

This entry was posted in Internet, python, Server. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *


This site uses Akismet to reduce spam. Learn how your comment data is processed.