Hosting a Pelican Static Website on AWS S3


Introduction

After 3 years of downtime, I decided to make my blog alive again. The problem was I forgot everything about pelican, and I didn't want to host my blog on dedicated server anymore - now I host my main website on AWS, and therefore it makes much more sense to host my blog on AWS as well. Therefore I asked for the help ChatGPT, and it provided me quite nice and correct instruction, how can I improve my pelican configuration to be able to host on S3.

In this blog post, we'll walk you through the process of hosting your Pelican-generated static website on Amazon S3. This guide assumes that you already have a working Pelican site and an AWS account. If you don't, please follow the Pelican documentation to set up your site and sign up for an AWS account.

Steps to Host Your Pelican Site on AWS S3

1. Install the s3cmd Tool

To sync your output folder with your S3 bucket, you'll need the s3cmd tool. You can install it with the following command:

pip install s3cmd

For Windows users, you can download the Windows package from the official website and follow the installation instructions.

2. Configure s3cmd with Your AWS Credentials

Run the following command and follow the prompts to enter your AWS Access Key and Secret Key:

s3cmd --configure

3. Create an S3 Bucket

Create a new S3 bucket in the AWS Management Console. Choose a unique bucket name, and set the region according to your preferences. Take note of the bucket name, as you'll need it in later steps.

4. Set the S3 Bucket Policy

To make your S3 bucket publicly accessible, you need to set its policy. Navigate to your bucket in the AWS Management Console, click on the "Permissions" tab, and then click on "Bucket Policy". Paste the following JSON policy, replacing your_bucket_name with the actual name of your S3 bucket:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "PublicReadGetObject",
            "Effect": "Allow",
            "Principal": "*",
            "Action": "s3:GetObject",
            "Resource": "arn:aws:s3:::your_bucket_name/*"
        }
    ]
}

5. Update Your pelicanconf.py

In your pelicanconf.py, update the SITEURL setting to point to your S3 bucket. If you're using a custom domain, add the S3_CNAME setting as well:

S3_BUCKET = 'your_bucket_name'
S3_CNAME = 'your_custom_domain.com'  # Optional, only if you have a custom domain

if S3_CNAME:
    SITEURL = 'https://' + S3_CNAME
else:
    SITEURL = 'https://{}.s3.amazonaws.com'.format(S3_BUCKET)

6. Sync Your Output Folder with Your S3 Bucket

After building your Pelican site, use the following command to sync the output folder with your S3 bucket:

s3cmd sync --acl-public --delete-removed --no-mime-magic output/ s3://your_bucket