Back to GitHub Projects
    Open Source · Python

    React Website AWS CDK

    A Python CDK stack that serves a React single-page app from a private S3 bucket through CloudFront, on your own domain with an existing ACM certificate.

    AWSCDKReact
    View on GitHub
    React Website AWS CDK Architecture

    About

    react-website-aws-cdk is a small AWS CDK v2 app written in Python. It defines one stack that hosts a built React single-page app: a private S3 bucket holds the files and a CloudFront distribution serves them over HTTPS on your own domain. The bucket blocks every form of public access and is encrypted with S3-managed keys. CloudFront reads it through an origin access identity, so the distribution is the only way in. The stack does not create the certificate. You issue it in AWS Certificate Manager first, in us-east-1 because CloudFront only accepts certificates from that region, and pass its ARN in.

    How it works

    app.py loads a .env file, builds the stack name from the application and environment values, and creates ReactWebsiteStack. The stack creates the bucket, an origin access identity with read access to it, and a CloudFront distribution in front of it. The distribution uses the imported certificate, answers on the domain from the .env file, and uses price class All, so every CloudFront edge location serves it.

    The deploy returns two CloudFormation outputs. The bucket name is where the React build goes. The CloudFront domain name is the target for your DNS record. Uploading the build is a separate step, for example aws s3 sync on the build folder followed by a CloudFront invalidation.

    Limits worth knowing

    The stack does not create DNS records. Point the domain at the distribution yourself, with a Route 53 alias record or a CNAME at another DNS provider. It also does not upload the site, so a CI job has to sync the build and invalidate the cache after each release.

    It uses an origin access identity, which AWS now treats as legacy. New distributions should use origin access control, which also works with SSE-KMS encrypted buckets. The repository's unit test is still the CDK sample placeholder and asserts nothing, and the code pins aws-cdk-lib 2.57.0, so expect to update dependencies before using it today.

    Key Features

    Private S3 bucket with all public access blocked and S3-managed encryption
    CloudFront reads the bucket through an origin access identity that has read access only
    Existing ACM certificate imported by ARN, not created by the stack
    TLS 1.2 minimum (TLSv1.2_2018 policy) and an HTTP to HTTPS redirect
    403 and 404 from the origin rewritten to /index.html with a 200, so client-side routes load
    index.html set as the default root object
    Stack name, domain and certificate read from a .env file with python-dotenv
    Outputs the bucket name and the CloudFront domain name for the upload step and DNS

    Engineering Challenges

    Keeping the bucket private while CloudFront still serves it: Block Public Access stays on and only the origin access identity can read.
    Making deep links work in a single-page app. Without the 403 and 404 rewrites, refreshing /about returns an S3 access-denied page instead of the app.
    Reusing a certificate that already exists, since CloudFront only accepts ACM certificates issued in us-east-1.
    Naming stacks per application and environment from one codebase, so dev and prod can deploy side by side.

    Results & Impact

    One cdk deploy creates the bucket, the distribution and both outputs.
    Nothing in the bucket is reachable except through CloudFront over HTTPS.
    Client-side routes survive a page refresh.
    The same code deploys more than one environment by changing the .env values.