Skip to main content

Serverless Cloudflare Worker backed by a D1 database

A serverless Cloudflare Worker backed by a D1 SQL database

This example lives in the pulumi/examples repository. Check out just this directory to use it:

Get started with this example
git clone --filter=blob:none --sparse https://github.com/pulumi/examples pulumi-examples
git -C pulumi-examples sparse-checkout set cloudflare-ts-serverless-d1
cd pulumi-examples/cloudflare-ts-serverless-d1

A Cloudflare Worker that records each visit in a D1 serverless SQL database and returns the running total. The Worker is uploaded as a version and deployed with cloudflare.WorkersDeployment, and is served on its *.workers.dev subdomain.

Prerequisites#

  1. Install Pulumi

  2. Install Node.js

  3. Create a Cloudflare API token with Workers and D1 edit permissions, and export it:

    Terminal window
    export CLOUDFLARE_API_TOKEN=<your-token>

Deploying the example#

  1. Create a new stack:

    Terminal window
    pulumi stack init dev
  2. Set your Cloudflare account ID:

    Terminal window
    pulumi config set accountId <your-account-id>
  3. Install dependencies:

    Terminal window
    npm install
  4. Run pulumi up to deploy:

    Terminal window
    pulumi up
  5. Visit the Worker’s URL, refreshing a few times to watch the counter increment:

    Terminal window
    curl "$(pulumi stack output url)"
    # Hello, world! This page has been visited 1 times.

Cleaning up#

Terminal window
pulumi destroy
pulumi stack rm dev

Related

The infrastructure as code platform for any cloud.