Cloudflare 中文文档
Pulumi
编辑这个页面
跳转官方原文档
Set theme to dark (⇧+D)

Deploy a Hello World app using Cloudflare Workers and Pulumi

In this tutorial, you will go through step-by-step instructions to deploy a Hello World web application using Cloudflare Workers and Pulumi Infrastructure as Code (IaC) so that you can become familiar with the resource management lifecycle. In particular, you will create a Worker, a Route, and a DNS Record to access the application before cleaning up all the resources.

alt_text
Running Cloudflare Workers application deployed with Pulumi

​​ Before you begin

Ensure you have:

​​ Initialize Pulumi

​​ a. Create a directory

You’ll use a new and empty directory for this tutorial.

​​ b. Login

At the prompt, press Enter to log into your Pulumi Cloud account via the browser. Alternatively, you may provide a Pulumi Cloud access token.

​​ c. Create a program

To create a program, run:

Complete the prompts with defaults where available; otherwise, provide the requested information. You will need:

  • Your Cloudflare account ID.
  • Your Cloudflare Zone ID.
  • A registered domain. For instance, example.com
  • A valid Cloudflare API token.

​​ d. Create a stack

To create a stack, run:

After the above command completes, review the value of myFirstOutput for correctness.

​​ e. (Optional) Review the stack

From the output above, follow your View in Browser link to get familiar with the Pulumi stack.

Example:

alt_text
Pulumi Cloud stack

​​ Add a Worker

You will now add a Cloudflare Worker to the Pulumi stack, dev.

​​ a. Add Cloudflare Worker to index.ts

Replace the contents of your index.ts file with the following:

​​ b. Install dependencies

​​ c. Apply the changes

​​ d. (Optional) View the Cloudflare Dashboard

You can view your Cloudflare resource directly in the Cloudflare Dashboard to validate its existence.

  1. Log into the Cloudflare dashboard.
  2. Select your account.
  3. Go to Workers & Pages.
  4. Open the “hello-world” application. Example:
    alt_text

​​ Add a Worker route

You will now add a Worker Route to the Pulumi stack, dev so the script can have an endpoint.

​​ a. Add Worker Route to index.ts

Replace the contents of your index.ts file with the following:

​​ b. Apply changes

​​ c. (Optional) View the Cloudflare Worker route in the dashboard

In the Cloudflare Dashboard, the Worker application now contains the previously defined Worker Route.

  1. Log into the Cloudflare dashboard.
  2. Select your account.
  3. Go to Workers & Pages.
  4. Select your application.
  5. For Routes, select View to verify the Worker Route details match your definition.
    alt_text
    Cloudflare Dashboard - Worker Route

​​ Add a DNS record

You will now add a DNS record to your domain so the previously configured route can be accessed via a URL.

​​ a. Add DNS Record to index.ts

Replace the contents of your index.ts file with the following:

​​ b. Apply the changes

​​ c. (Optional) View all the resources in Pulumi Cloud

  1. In your browser, open your Pulumi Cloud
  2. Navigate to your stack, serverless-cloudflare/dev.
  3. Confirm all the defined resources are created and healthy. Example:

alt_text
Pulumi Cloud stack

​​ Test the app

You have incrementally added all the Cloudflare resources needed to run and access your Hello World application. This was done by defining the resources in TypeScript and letting Pulumi handle the rest.

You can test your application via the terminal or browser.

  • In the terminal
  • In your browser, open hello-world.YOUR_DOMAIN.com

Example:

alt_text
Hello World app browser screenshot

​​ Clean up

In this last step, you will run a couple of commands to clean up the resources and stack you used throughout the tutorial.

​​ a. Delete the Cloudflare resources

​​ b. Remove the Pulumi stack