Skip to main content

Cloudflare Serverless Application

The Cloudflare Serverless Application template scaffolds a Pulumi project that deploys a serverless web application to Cloudflare. It provisions a Cloudflare Worker for the application backend, exposed on its own workers.dev subdomain, and a Workers KV namespace that the Worker reads and writes to persist state between requests. Pulumi uploads the application code as a versioned Worker version — bound to the KV namespace — and rolls it out to all of the Worker’s traffic with a Workers deployment. The template ships with a placeholder visit-counter application so the project deploys end to end out of the box.

Architecture diagram of the Cloudflare Serverless Application template Cloudflare Cloudflare Worker Workers KVNamespace

Using this template#

To use this template to deploy your own serverless application, make sure you’ve installed Pulumi and configured your Cloudflare credentials, then create a new project using the template in the language of your choice:

Terminal window
mkdir my-serverless-app && cd my-serverless-app
pulumi new serverless-cloudflare-typescript

Follow the prompts to complete the new-project wizard. When it’s done, you’ll have a complete Pulumi project that’s ready to deploy and configured with the most common settings. Feel free to inspect the code in index.ts for a closer look.

Deploying the project#

The template needs one setting to deploy: the Cloudflare account ID to deploy into. Set it, then deploy with pulumi up:

Terminal window
pulumi config set accountId <your-account-id>
pulumi up

When the deployment completes, Pulumi exports the following stack output value:

url

The fully-qualified HTTPS URL of the deployed application on its workers.dev subdomain.

Output values like this one are useful in many ways, most commonly as inputs for other stacks or related cloud resources. The computed URL, for example, can be used from the command line to open the newly deployed application in your favorite web browser:

Terminal window
open $(pulumi stack output url)

Cleaning up#

You can cleanly destroy the stack and all of its infrastructure with pulumi destroy:

Terminal window
pulumi destroy

Learn more#

Related

The infrastructure as code platform for any cloud.