In this tutorial, you will follow step-by-step instructions to bring an existing site to Cloudflare using Pulumi infrastructure as code (IaC) to familiarize yourself 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 site tutorial.
Before you begin
Ensure you have:
A Cloudflare account and API Token with permission to edit the resources in this tutorial. If you need to, sign up for a Cloudflare account ↗ before continuing. Your token must have:
Zone-Zone-Edit permission
Zone-DNS-Edit permission
include-All zones from an account-<your account> zone resource
A domain name. You may use example.com to complete the tutorial.
1. Initialize your project
A Pulumi project is a collection of files in a dedicated folder that describes the infrastructure you want to create. The Pulumi project folder is identified by the required Pulumi.yaml file. You will use the Pulumi CLI to create and configure a new project.
a. Create a directory
Use a new and empty directory for this tutorial.
b. Login to Pulumi Cloud
Pulumi Cloud ↗ is a hosted service that provides a secure and scalable platform for managing your infrastructure as code. You will use it to store your Pulumi backend configurations.
At the prompt, press Enter to log into your Pulumi Cloud account via the browser. Alternatively, you may provide a Pulumi Cloud access token ↗.
A Pulumi stack ↗ is an instance of a Pulumi program. Stacks are independently configurable and may represent different environments (development, staging, production) or feature branches. For this tutorial, you'll use the dev stack.
To instantiate your dev stack, run:
You have not defined any resources at this point, so you'll have an empty stack.
e. Save your settings
In this step, you will store your settings in a Pulumi ESC Environment ↗, a YAML file containing configurations and secrets. These can be accessed in several ways, including a Pulumi program. All ESC Environments securely reside in your Pulumi Cloud account and can be fully managed via the Pulumi CLI. For this tutorial, you will store the following values:
Below are Apache Maven instructions. For other Java project managers such as Gradle, see the official Maven repository ↗
Open your pom.xml file.
Add the Pulumi Cloudflare dependency inside the <dependencies> section.
Run:
There are no dependencies to download for YAML. Skip ahead.
2. Define Cloudflare resources in code
With the Cloudflare package installed, you can now define any supported Cloudflare resource ↗ in your Pulumi program. You'll define a Zone, and a DNS Record next.
a. Add a Zone
A domain, or site, is known as a Zone in Cloudflare. In Pulumi, the Zone resource ↗ represents a Cloudflare Zone.
Replace the contents of your entrypoint file with the following:
Notice that the code also outputs several properties from the Zone resource, such as the zoneId, nameservers, and status, so that they can easily be accessed in subsequent steps.
b. Add a DNS Record
You will now add a DNS Record resource ↗ to test previously configured Zone.
Add the following code snippet to your entrypoint file after the Zone resource definition:
Now that you have defined your resources, you can deploy the changes using the Pulumi CLI so that they are reflected in your Cloudflare account.
To deploy the changes, run:
4. Configure your DNS provider
a. Obtain your nameservers
Once you have added a domain to Cloudflare, that domain will receive two assigned authoritative nameservers.
To retrieve the assigned nameservers, run:
b. Update your registrar
Update the nameservers at your registrar to activate Cloudflare services for your domain. The instructions are registrar-specific. You may be able to find guidance under this consolidated list of common registrars.
c. Check your domain status
Once successfully registered, your domain status will change to active.
5. Test your site
You will run two nslookup commands against the Cloudflare-assigned nameservers.
To test your site, run:
For .NET, use Nameservers as the Output.
Confirm your response returns the IP address(es) for your site.
6. 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. You declare the resources in your programming language of choice and let Pulumi handle the rest.