Thêm site Add a site
Hướng dẫn chi tiết đồng bộ từ docs Cloudflare — mỗi section có backlink tới đúng vị trí trên trang gốc. Detailed guide synced from Cloudflare docs — each section links to the matching anchor on the official page.
← Danh mục ← CatalogGiải thích nhanh Quick context
Thuộc Developer Platform — thực hành triển khai code, API và dữ liệu trên edge/serverless. Tutorial «Thêm site» giúp bạn làm quen luồng triển khai thật — phù hợp đọc trước khi mở tài liệu gốc tiếng Anh. Docs gốc chia khoảng 6 bước chính; bản tóm tắt dưới đây giúp bạn nắm khung trước khi làm theo từng lệnh.
This tutorial uses Pulumi infrastructure as code (IaC) to familiarize yourself with the resource management lifecycle.
Lưu ý trước khi làm Notes before you start
- Đây là bản tóm tắt trên Orange Cloud Learning Hub — không thay thế tài liệu chính thức. This is a summary on Orange Cloud Learning Hub — it does not replace the official documentation.
- Luôn mở liên kết «Tài liệu gốc» bên dưới khi cần lệnh CLI, snippet code và ảnh minh họa đầy đủ. Open the Official docs link below for CLI commands, code snippets, and full screenshots.
- Kiểm tra token/API và state backend trước khi chạy trên môi trường production. Verify API tokens and the state backend before running against production.
- Docs Cloudflare cập nhật thường xuyên — đối chiếu ngày «Rà soát lần cuối» trên trang gốc khi triển khai production. Cloudflare docs change frequently — verify the Last reviewed date on the official page before production use.
Tài liệu gốc — rà soát lần cuối: over 1 year ago Official docs — last reviewed: over 1 year ago
Tổng quan Overview
Phần «Tổng quan» — đọc hướng dẫn bên dưới, dùng liên kết docs gốc để xem ảnh minh họa và tab cấu hình đầy đủ.
Read the "Overview" section below — open the official docs link for full screenshots and configuration tabs.
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.
You will provision resources that qualify under free tier offerings for both Pulumi Cloud and Cloudflare.
Liên kết liên quan (docs Cloudflare) Related links (Cloudflare docs)
Before you begin Before you begin
Phần «Before you begin» — đọc hướng dẫn bên dưới, dùng liên kết docs gốc để xem ảnh minh họa và tab cấu hình đầy đủ.
Read the "Before you begin" section below — open the official docs link for full screenshots and configuration tabs.
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 Pulumi Cloud account. You can sign up for an always-free individual tier ↗.
- The Pulumi CLI is installed on your machine.
- A Pulumi-supported programming language ↗ is configured. (TypeScript, JavaScript, Python, Go, .NET, Java, or use YAML)
- A domain name. You may use
example.comto complete the tutorial.
Liên kết liên quan (docs Cloudflare) Related links (Cloudflare docs)
1. Khởi tạo your dự án 1. Initialize your project
Phần «Khởi tạo your dự án» — đọc hướng dẫn bên dưới, dùng liên kết docs gốc để xem ảnh minh họa và tab cấu hình đầy đủ.
Read the "1. Initialize your project" section below — open the official docs link for full screenshots and configuration tabs.
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.
. Create directory a. Create a directory
Phần «. Create directory» — đọc hướng dẫn bên dưới, dùng liên kết docs gốc để xem ảnh minh họa và tab cấu hình đầy đủ.
Read the "a. Create a directory" section below — open the official docs link for full screenshots and configuration tabs.
Use a new and empty directory for this tutorial.
Terminal window
mkdir addsite-cloudflare
cd addsite-cloudflare b. Login to Pulumi Cloud b. Login to Pulumi Cloud
Phần «b. Login to Pulumi Cloud» — đọc hướng dẫn bên dưới, dùng liên kết docs gốc để xem ảnh minh họa và tab cấu hình đầy đủ.
Read the "b. Login to Pulumi Cloud" section below — open the official docs link for full screenshots and configuration tabs.
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 ↗.
Terminal window
pulumi login C. Create mới program c. Create a new program
Phần «C. Create mới program» — đọc hướng dẫn bên dưới, dùng liên kết docs gốc để xem ảnh minh họa và tab cấu hình đầy đủ.
Read the "c. Create a new program" section below — open the official docs link for full screenshots and configuration tabs.
A Pulumi program is code written in a supported programming language ↗ that defines infrastructure resources.
To create a program, select your language of choice and run the pulumi command:
Terminal window
pulumi new javascript --name addsite-cloudflare --yes
# wait a few seconds while the project is initialized Terminal window
pulumi new typescript --name addsite-cloudflare --yes
# wait a few seconds while the project is initialized Terminal window
pulumi new python --name addsite-cloudflare --yes
# wait a few seconds while the project is initialized Terminal window
pulumi new go --name addsite-cloudflare --yes
# wait a few seconds while the project is initialized Terminal window
pulumi new java --name addsite-cloudflare --yes
# wait a few seconds while the project is initialized Terminal window
pulumi new csharp --name addsite-cloudflare --yes
# wait a few seconds while the project is initialized Terminal window
pulumi new yaml --name addsite-cloudflare --yes D. Create stack d. Create a stack
Phần «D. Create stack» — đọc hướng dẫn bên dưới, dùng liên kết docs gốc để xem ảnh minh họa và tab cấu hình đầy đủ.
Read the "d. Create a stack" section below — open the official docs link for full screenshots and configuration tabs.
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:
Terminal window
pulumi up --yes
# wait a few seconds for the stack to be instantiated. You have not defined any resources at this point, so you'll have an empty stack.
e. Save your settings e. Save your settings
Phần «e. Save your settings» — đọc hướng dẫn bên dưới, dùng liên kết docs gốc để xem ảnh minh họa và tab cấu hình đầy đủ.
Read the "e. Save your settings" section below — open the official docs link for full screenshots and configuration tabs.
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:
- Your Cloudflare account ID.
- A valid Cloudflare API token.
- A domain. For instance,
example.com.
Terminal window
# Define an ESC Environment name
E=cloudflare/my-dev-env
# Create a new Pulumi ESC Environment
pulumi config env init --env $E --yes Creating environment cloudflare/my-dev-env for stack dev... Terminal window
# Replace abc123 with your Cloudflare Account ID
pulumi env set $E --plaintext pulumiConfig.accountId abc123
# Replace API_TOKEN with your Cloudflare API Token
pulumi env set $E --secret pulumiConfig.cloudflare:apiToken API_TOKEN
# Replace example.com with your registered domain, or leave as is
pulumi env set $E --plaintext pulumiConfig.domain example.com Liên kết liên quan (docs Cloudflare) Related links (Cloudflare docs)
F. Install Cloudflare package f. Install the Cloudflare package
Phần «F. Install Cloudflare package» — đọc hướng dẫn bên dưới, dùng liên kết docs gốc để xem ảnh minh họa và tab cấu hình đầy đủ.
Read the "f. Install the Cloudflare package" section below — open the official docs link for full screenshots and configuration tabs.
You need to install the Cloudflare package for your language of choice in order to define Cloudflare resources in your Pulumi program.
Install the Cloudflare package by running the following command:
Terminal window
npm install @pulumi/cloudflare added 1 package ... Terminal window
npm install @pulumi/cloudflare added 1 package ... Terminal window
echo "pulumi_cloudflare>=5.38,<6.0.0" >> requirements.txt
source venv/bin/activate
pip install -r requirements.txt ...Collecting pulumi-cloudflare... Terminal window
go get github.com/pulumi/pulumi-cloudflare/sdk/v3/go/cloudflare go: downloading github.com/pulumi/pulumi-cloudflare ... Below are Apache Maven instructions. For other Java project managers such as Gradle, see the official Maven repository ↗
- Open your
pom.xmlfile. - Add the Pulumi Cloudflare dependency inside the
<dependencies>section.
<dependency>
<groupId>com.pulumi</groupId>
<artifactId>cloudflare</artifactId>
<version>5.38.0</version>
</dependency> - Run:
Terminal window
mvn clean install ...[INFO] BUILD SUCCESS... Terminal window
dotnet add package Pulumi.Cloudflare ...
info : Adding PackageReference for package 'Pulumi.Cloudflare' into project
... There are no dependencies to download for YAML. Skip ahead.
2. Define Cloudflare resources in code 2. Define Cloudflare resources in code
Phần «Define Cloudflare resources in code» — đọc hướng dẫn bên dưới, dùng liên kết docs gốc để xem ảnh minh họa và tab cấu hình đầy đủ.
Read the "2. Define Cloudflare resources in code" section below — open the official docs link for full screenshots and configuration tabs.
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.
. Add Zone a. Add a Zone
Phần «. Add Zone» — đọc hướng dẫn bên dưới, dùng liên kết docs gốc để xem ảnh minh họa và tab cấu hình đầy đủ.
Read the "a. Add a Zone" section below — open the official docs link for full screenshots and configuration tabs.
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:
Filename: index.js
JavaScript
"use strict";
const pulumi = require("@pulumi/pulumi");
const cloudflare = require("@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,
});
exports.zoneId = zone.id;
exports.nameservers = zone.nameServers;
exports.status = zone.status; Filename: index.ts
TypeScript
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 const zoneId = zone.id;
export const nameservers = zone.nameServers;
export const status = zone.status; Filename: _main_.py
Python
import pulumi
import pulumi_cloudflare as cloudflare
account_id = pulumi.Config().require("accountId")
domain = pulumi.Config().require("domain")
# Create a Cloudflare resource (Zone)
zone = cloudflare.Zone("my-zone",
zone=domain,
account_id=account_id,
plan="free",
jump_start=True)
pulumi.export("zoneId", zone.id)
pulumi.export('nameservers', zone.name_servers)
pulumi.export('status', zone.status) Filename: main.go
package main
import (
"github.com/pulumi/pulumi/sdk/v3/go/pulumi"
cloudflare "github.com/pulumi/pulumi-cloudflare/sdk/v3/go/cloudflare"
)
func main() {
pulumi.Run(func(ctx *pulumi.Context) error {
domain, _ := ctx.GetConfig("domain")
// Create a Cloudflare resource (Zone)
zone, err := cloudflare.NewZone(ctx, "my-zone", &cloudflare.ZoneArgs{
Zone: pulumi.String(domain),
Plan: pulumi.String("free"),
JumpStart: pulumi.Bool(true),
})
if err != nil {
return err
}
ctx.Export("zoneId", zone.ID())
ctx.Export("nameservers", zone.NameServers)
ctx.Export("status", zone.Status)
return nil
})
} Filename: src/main/java/myproject/App.java
package myproject;
import com.pulumi.Pulumi;
import com.pulumi.Context;
import com.pulumi.cloudflare.ZoneArgs;
import com.pulumi.cloudflare.Zone;
public class App {
public static void main(String[] args) {
Pulumi.run(ctx -> {
var config = ctx.config();
String accountId = config.require("accountId");
String domain = config.require("domain");
var zone = new Zone("my-zone", ZoneArgs.builder()
.zone(domain)
.accountId(accountId)
.plan("free")
.jumpStart(true)
.build());
ctx.export("zoneId", zone.id());
ctx.export("nameservers", zone.nameServers());
ctx.export("status", zone.status());
});
}
} Filename: Program.cs
using System.Threading.Tasks;
using System.Collections.Immutable;
using Pulumi;
using Pulumi.Cloudflare;
class Program
{
static Task<int> Main() => Deployment.RunAsync<MyStack>();
class MyStack : Stack
{
public MyStack()
{
var config = new Pulumi.Config();
var accountId = config.Require("accountId");
var domain = config.Require("domain");
var zone = new Zone("my-zone", new ZoneArgs
{
ZoneName = domain,
AccountId = accountId,
Plan = "free",
JumpStart = true
});
this.ZoneId = zone.Id;
this.Nameservers = zone.NameServers;
this.Status = zone.Status;
}
[Output]
public Output<string> ZoneId { get; set; }
public Output<ImmutableArray<string>> Nameservers { get; set; }
public Output<string> Status { get; set; }
}
} Filename: Pulumi.yaml
YAML
name: addsite-cloudflare
runtime: yaml
resources:
myZone:
type: cloudflare:Zone
properties:
zone: ${domain}
accountId: ${accountId}
plan: "free"
jumpStart: true
outputs:
zoneId: ${myZone.id}
nameservers: ${exampleZone.nameServers}
status: ${exampleZone.status} 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 DNS Record b. Add a DNS Record
Phần «B. Add DNS Record» — đọc hướng dẫn bên dưới, dùng liên kết docs gốc để xem ảnh minh họa và tab cấu hình đầy đủ.
Read the "b. Add a DNS Record" section below — open the official docs link for full screenshots and configuration tabs.
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:
Filename: index.js
JavaScript
const record = new cloudflare.Record("my-record", {
zoneId: zone.id,
name: domain,
content: "192.0.2.1",
type: "A",
proxied: true,
}); Filename: index.ts
TypeScript
const record = new cloudflare.Record("my-record", {
zoneId: zone.id,
name: domain,
content: "192.0.2.1",
type: "A",
proxied: true,
}); Filename: _main_.py
Python
record = cloudflare.Record("my-record",
zone_id=zone.id,
name=domain,
content="192.0.2.1",
type="A",
proxied=True
) Filename: main.go
_, err = cloudflare.NewRecord(ctx, "my-record", &cloudflare.RecordArgs{
ZoneId: zone.ID(),
Name: pulumi.String(domain),
Content: pulumi.String("192.0.2.1"),
Type: pulumi.String("A"),
Proxied: pulumi.Bool(true),
})
if err != nil {
return err
} Filename: src/main/java/myproject/App.java
// Add imports
import com.pulumi.cloudflare.Record;
import com.pulumi.cloudflare.RecordArgs;
// Below the Zone resource, add
new Record("my-record", RecordArgs.builder()
.zoneId(zone.id())
.name(domain)
.content("192.0.2.1")
.type("A")
.proxied(true)
.build()); Filename: Program.cs
new Record("my-record", new RecordArgs
{
ZoneId = zone.Id,
Name = domain,
Content = "192.0.2.1",
Type = "A",
Proxied = true
}); Filename: Pulumi.yaml
YAML
myRecord:
type: cloudflare:Record
properties:
zoneId: ${myZone.id}
name: ${domain}
content: 192.0.2.1
type: A
proxied: true 3. Triển khai your changes 3. Deploy your changes
Phần «Triển khai your changes» — đọc hướng dẫn bên dưới, dùng liên kết docs gốc để xem ảnh minh họa và tab cấu hình đầy đủ.
Read the "3. Deploy your changes" section below — open the official docs link for full screenshots and configuration tabs.
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:
Terminal window
pulumi up --yes wait for the dev stack to become ready 4. Cấu hình your DNS provider 4. Configure your DNS provider
Phần «Cấu hình your DNS provider» — đọc hướng dẫn bên dưới, dùng liên kết docs gốc để xem ảnh minh họa và tab cấu hình đầy đủ.
Read the "4. Configure your DNS provider" section below — open the official docs link for full screenshots and configuration tabs.
This process makes Cloudflare your authoritative DNS provider, allowing your DNS queries and web traffic to be served from and protected by the Cloudflare network.
Learn more about pending domains
If your site is example.com, skip ahead to Test your site.
Liên kết liên quan (docs Cloudflare) Related links (Cloudflare docs)
. Obtain your nameservers a. Obtain your nameservers
Phần «. Obtain your nameservers» — đọc hướng dẫn bên dưới, dùng liên kết docs gốc để xem ảnh minh họa và tab cấu hình đầy đủ.
Read the "a. Obtain your nameservers" section below — open the official docs link for full screenshots and configuration tabs.
Once you have added a domain to Cloudflare, that domain will receive two assigned authoritative nameservers.
To retrieve the assigned nameservers, run:
Terminal window
pulumi stack output b. Update your registrar b. Update your registrar
Phần «b. Update your registrar» — đọc hướng dẫn bên dưới, dùng liên kết docs gốc để xem ảnh minh họa và tab cấu hình đầy đủ.
Read the "b. Update your registrar" section below — open the official docs link for full screenshots and configuration tabs.
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.
Warning
Registrars take up to 24 hours to process nameserver changes.
Liên kết liên quan (docs Cloudflare) Related links (Cloudflare docs)
c. Check your domain status c. Check your domain status
Phần «c. Check your domain status» — đọc hướng dẫn bên dưới, dùng liên kết docs gốc để xem ảnh minh họa và tab cấu hình đầy đủ.
Read the "c. Check your domain status" section below — open the official docs link for full screenshots and configuration tabs.
Once successfully registered, your domain status will change to active.
Terminal window
pulumi stack output 5. Test your site 5. Test your site
Phần «Test your site» — đọc hướng dẫn bên dưới, dùng liên kết docs gốc để xem ảnh minh họa và tab cấu hình đầy đủ.
Read the "5. Test your site" section below — open the official docs link for full screenshots and configuration tabs.
You will run two nslookup commands against the Cloudflare-assigned nameservers.
To test your site, run:
Terminal window
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 For .NET, use Nameservers as the Output.
Confirm your response returns the IP address(es) for your site.
You will not receive a valid response if you use example.com as your site.
6. Dọn dẹp 6. Clean up
Phần «Dọn dẹp» — đọc hướng dẫn bên dưới, dùng liên kết docs gốc để xem ảnh minh họa và tab cấu hình đầy đủ.
Read the "6. Clean up" section below — open the official docs link for full screenshots and configuration tabs.
In this last step, you will remove the resources and stack used throughout the tutorial.
. Delete resources a. Delete the resources
Phần «. Delete resources» — đọc hướng dẫn bên dưới, dùng liên kết docs gốc để xem ảnh minh họa và tab cấu hình đầy đủ.
Read the "a. Delete the resources" section below — open the official docs link for full screenshots and configuration tabs.
Terminal window
pulumi destroy --yes B. Remove stack b. Remove the stack
Phần «B. Remove stack» — đọc hướng dẫn bên dưới, dùng liên kết docs gốc để xem ảnh minh họa và tab cấu hình đầy đủ.
Read the "b. Remove the stack" section below — open the official docs link for full screenshots and configuration tabs.
Terminal window
pulumi stack rm dev Bước tiếp theo Next steps
Phần «Bước tiếp theo» — đọc hướng dẫn bên dưới, dùng liên kết docs gốc để xem ảnh minh họa và tab cấu hình đầy đủ.
Read the "Next steps" section below — open the official docs link for full screenshots and configuration tabs.
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.
To deploy a serverless app with Pulumi, follow the Deploy a Worker tutorial.
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"item":{"@id":"/directory/","name":"Directory"}},{"@type":"ListItem","position":2,"item":{"@id":"/pulumi/","name":"Pulumi"}},{"@type":"ListItem","position":3,"item":{"@id":"/pulumi/tutorial/","name":"Tutorials"}},{"@type":"ListItem","position":4,"item":{"@id":"/pulumi/tutorial/add-site/","name":"Add a site"}}]} Liên kết liên quan (docs Cloudflare) Related links (Cloudflare docs)
Xem bản đầy đủ trên developers.cloudflare.com (ảnh, tab cấu hình). View the full guide on developers.cloudflare.com (images, config tabs).
Tài liệu gốc ↗ Official docs ↗