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:
git clone --filter=blob:none --sparse https://github.com/pulumi/examples pulumi-examplesgit -C pulumi-examples sparse-checkout set cloudflare-ts-serverless-d1cd pulumi-examples/cloudflare-ts-serverless-d1A 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#
-
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#
-
Create a new stack:
Terminal window pulumi stack init dev -
Set your Cloudflare account ID:
Terminal window pulumi config set accountId <your-account-id> -
Install dependencies:
Terminal window npm install -
Run
pulumi upto deploy:Terminal window pulumi up -
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#
pulumi destroypulumi stack rm devRelated
TemplateCloudflare
Cloudflare Serverless Application
TemplateAWS