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

Add a site to Cloudflare with Pulumi IaC

In this tutorial, you will go through step-by-step instructions to bring an existing site to Cloudflare using Pulumi Infrastructure as Code (IaC) so that you can become familiar with the resource management lifecycle. In particular, you will create a Zone and a DNS record to resolve your newly added site. This tutorial adopts the IaC principle to complete the steps listed in the Add a Site tutorial.

​​ Before you begin

Ensure you have:

​​ Initialize Pulumi

​​ a. Create a directory

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.
  • A registered domain. For instance, example.com
  • A valid Cloudflare API token.

​​ d. Create a stack

To create a stack, run:

​​ d. (Optional) Verify the stack creation

Review the value of myTestOutput to confirm the stack creation.

​​ Add a Zone

You will now add a Cloudflare Zone to the Pulumi stack.

​​ a. Modify the program

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

​​ b. Install dependencies

​​ c. Apply the changes

​​ c. (Optional) Review the Zone ID

Review the value of zoneId to confirm the Zone creation.

​​ Update your nameservers

Once you have added a domain to Cloudflare, that domain will receive two assigned authoritative nameservers.

​​ a. Update the program

At the end of your index.ts file, add the following:

​​ b. Apply the changes

​​ c. Obtain the nameservers

Review the value of nameservers to retrieve the assigned nameservers:

​​ d. Update your registrar

Update the nameservers at your registrar to activate Cloudflare services for your domain. Instructions are registrar-specific. You may be able to find guidance under this consolidated list of common registrars.

​​ e. Check your domain status

Once successfully registered, your domain status will change to active.

​​ Add a DNS record

You will now add a DNS record to your domain.

​​ a. Modify your program

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

​​ b. Apply the changes

​​ Verify your setup

You will run two nslookup commands against the Cloudflare-assigned nameservers.

To test your site, run:

Confirm your response returns IP address(es) for your site.

​​ Clean up

In this last step, you will remove the resources and stack used throughout the tutorial.

​​ a. Delete the resources

​​ b. Remove the stack

​​ Next steps

You have incrementally defined Cloudflare resources needed to add a site to Cloudflare. After each new resource, you apply the changes to your stack via the pulumi up command. You declare the resources in TypeScript and let Pulumi handle the rest.

Follow the Hello World tutorial next to deploy your first app with Pulumi.