Deploy AWS S3 Website using CDK in Python

CDK stands for Cloud Development Kit. It defines cloud infrastructure in code and provisions resources through AWS CloudFormation.

This post will demonstrate how to setup and execute a simple CDK application to create a AWS S3 Static Website.

Prerequisites

  • Node.js >= 10.3.0
  • Python 3
  • configured AWS CLI with read/write access to S3 service

Install AWS CDK

First install CDK:

npm install -g aws-cdk
pip install --upgrade aws-cdk.core

Setup CDK Application

Execute the following commands to initialize the CDK application:

mkdir cdk-s3-website
cd cdk-s3-website
cdk init --language python
source .env/bin/activate
pip install -r requirements.txt

After successfully setup the CDK application, let’s take a look at some files of interest:

app.py

This is the code execution of the application:

from aws_cdk import core
from cdk_s3_website.cdk_s3_website_stack import CdkS3WebsiteStack

app = core.App()
CdkS3WebsiteStack(app, "cdk-s3-website")
app.synth()

It invokes the custom generated class CdkS3WebsiteStack to create the needed resources.

cdk_s3_website/cdk_s3_website_stack.py

from aws_cdk import core


class CdkS3WebsiteStack(core.Stack):

    def __init__(self, scope: core.Construct, id: str, **kwargs) -> None:
        super().__init__(scope, id, **kwargs)

        # The code that defines your stack goes here

This is where the infrastructure as code will be implemented. Currently, it is just an empty class.

requirements.txt

This is where we put additional packages needed here.

Build a S3 static website infrastructure

So let’s start filling in the blanks to create the website infrastructure. Since we are using S3 service, we need the aws-s3 package. Append the following line to requirements.txt:

aws-cdk.aws_s3

Then run the following commands again:

pip install -r requirements.txt
pip install --upgrade aws-cdk.core

Modify cdk_s3_website_stack.py as follow:

from aws_cdk import (core, aws_s3 as _s3)

class CdkS3WebsiteStack(core.Stack):

    def __init__(self, scope: core.Construct, id: str, **kwargs) -> None:
        super().__init__(scope, id, **kwargs)

        # The code that defines your stack goes here
        bucket = _s3.Bucket(self, id + "_s3-bucket",
            bucket_name= ('cdk-s3-static-website'),
            website_index_document= 'index.html',
            website_error_document= 'error.html',
            public_read_access= True,
            removal_policy= core.RemovalPolicy.DESTROY)

This will create a bucket called cdk-s3-static-website using index.html as the index document. Setting public_read_access to True will automatically setup the required bucket policy for public access.

Note that the removal policy is set to DESTROY. That means we want the S3 bucket to be destroyed when we delete the CDK infrastructure. However, please note that the bucket will NOT be destroyed if it is not empty.

Now try to run command cdk synth. If everything is setup correctly, this command will display the corresponding CloudFormation template needed to create and setup the S3 bucket.

Time to run the following command to deploy the infrastructure:

cdk deploy

Review the resource to be created and enter ‘y’ to confirm the deployment.

deployment

Success. The S3 bucket should be created. We can verify it by checking at the AWS management console.

verify bucket

You can see the bucket is publicly accessible and the bucket policy is setup correctly. What’s left to do is to upload index.html and you have a working website.

To clean up the infrastructure, run the command cdk destroy

That’s it. You now have a working CDK application.

Leave a Reply

Close Menu