Cloudflare Docs
Pulumi
Edit this page
Report an issue with this page
Log into the Cloudflare dashboard
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.

$ mkdir addsite-cloudflare
$ cd addsite-cloudflare

​​ 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.

$ pulumi login

​​ c. Create a program

To create a program, run:

$ pulumi new https://github.com/pulumi/tutorials/tree/cloudflare-typescript-add-site-begin

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:

$ pulumi up --yes

​​ d. (Optional) Verify the stack creation

Review the value of myTestOutput to confirm the stack creation.

$ pulumi stack output myTestOutput
Hurray!

​​ 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:

index.ts
import * as pulumi from "@pulumi/pulumi";
import * as cloudflare from "@pulumi/cloudflare";
const config = new pulumi.Config();
const accountId = config.require("accountId");
const domain = config.require("domain")
// Create a Cloudflare resource (Zone)
const zone = new cloudflare.Zone("my-zone", {
zone: domain,
accountId: accountId,
plan: "free",
jumpStart: true,
});
// Export the zone ID
export const zoneId = zone.id;

​​ b. Install dependencies

$ npm install @pulumi/cloudflare

​​ c. Apply the changes

$ pulumi up --yes

​​ c. (Optional) Review the Zone ID

Review the value of zoneId to confirm the Zone creation.

$ pulumi stack output zoneId
d8fcb6d731fe1c2d75e2e8d6ad63fad5

​​ 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:

export const nameservers = zone.nameServers;
export const status = zone.status;

​​ b. Apply the changes

$ pulumi up --yes

​​ c. Obtain the nameservers

Review the value of nameservers to retrieve the assigned nameservers:

$ pulumi stack output nameservers
["emerie.ns.cloudflare.com","miguel.ns.cloudflare.com"]

​​ 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.

$ pulumi stack output status
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:

index.ts
import * as pulumi from "@pulumi/pulumi";
import * as cloudflare from "@pulumi/cloudflare";
const config = new pulumi.Config();
const accountId = config.require("accountId");
const domain = config.require("domain")
// Create a Cloudflare resource (Zone)
const zone = new cloudflare.Zone("my-zone", {
zone: domain,
accountId: accountId,
plan: "free", // Choose the desired plan, e.g., "free", "pro", "business", etc.
jumpStart: true,
});
// Export the zone ID
export const zoneId = zone.id;
// Export the Cloudflare-assigned nameservers.
export const nameservers = zone.nameServers;
// Export the status
export const status = zone.status;
// Set up a Record for your site
const record = new cloudflare.Record("my-record", {
zoneId: zoneId,
name: domain,
value: "192.0.2.1",
type: "A",
proxied: true,
});

​​ b. Apply the changes

$ pulumi up --yes

​​ Verify your setup

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

To test your site, run:

$ DOMAIN=$(pulumi config get domain)
$ NS1=$(pulumi stack output nameservers | jq '.[0]' -r)
$ NS2=$(pulumi stack output nameservers | jq '.[1]' -r)
$ nslookup $DOMAIN $NS1
$ nslookup $DOMAIN $NS2

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

$ pulumi destroy

​​ b. Remove the stack

$ pulumi stack rm dev

​​ 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.