Tạo different resources using Pulumi và Wrangler Create different resources using Pulumi and Wrangler
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 «Tạo different resources using Pulumi và Wrangler» 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. Đọc phần tóm tắt và lưu ý trước — sau đó mở docs gốc để copy lệnh và cấu hình chi tiết.
Pulumi and Wrangler can be used to create different types of resources.
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.
- Cần tài khoản Cloudflare, Wrangler CLI và (thường) hoàn thành hướng dẫn Get started của Workers/Pages. Requires a Cloudflare account, Wrangler CLI, and (typically) completing the Workers/Pages Get started guide.
- 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.
This example creates a zone and other resources using two different strategies:
- Use Pulumi for some resources supported by the Cloudflare Pulumi provider.
- Use Wrangler to create other types of resources.
The example code covers the creation of resources such as Workers, Zero Trust Applications, Zero Trust Policies, and D1 databases.
Wrangler usage Wrangler usage
Phần «Wrangler usage» — đọ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 "Wrangler usage" section below — open the official docs link for full screenshots and configuration tabs.
The code in this example shows how you can create Workers by calling Wrangler directly instead of using the resource directly supported by the Cloudflare Pulumi provider. The advantage of using this method is that you can use it for any deployment-related task such as executing D1 migrations. When you run the infrastructure as code (IaC) script, Wrangler creates or updates Workers and D1, which includes performing database migrations.
In the current example, the D1 migrations state in Pulumi only changes when the hash of the migrations directory changes, in which case the Pulumi command will be executed.
Dynamic resource provider for Vectorize Dynamic resource provider for Vectorize
Phần «Dynamic resource provider for Vectorize» — đọ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 "Dynamic resource provider for Vectorize" section below — open the official docs link for full screenshots and configuration tabs.
The example also provides an example of a dynamic resource provider for a resource that is not directly supported by the Cloudflare Pulumi provider (in this case, Vectorize).
Example code Example code
Phần «Example 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 "Example code" section below — open the official docs link for full screenshots and configuration tabs.
JavaScript
"use strict";
const pulumi = require("@pulumi/pulumi");
const cloudflare = require("@pulumi/cloudflare");
const command = require("@pulumi/command");
const path = require("path");
const axios = require("axios");
const https = require("https");
const crypto = require("crypto");
const fs = require("fs");
// Load configuration
const config = new pulumi.Config();
const domainName = config.require("domainName");
const accountId = config.require("accountId");
const apiToken = config.requireSecret("apiToken");
// Function to compute hash of a file
function computeFileHashSync(filePath) {
const fileBuffer = fs.readFileSync(filePath);
const hash = crypto.createHash("sha256");
hash.update(fileBuffer);
return hash.digest("hex");
}
// Function to compute the hash of a directory
async function hashDirectory(dirPath) {
const files = await fs.promises.readdir(dirPath);
const fileHashes = [];
for (const file of files) {
const filePath = path.join(dirPath, file);
const fileStat = await fs.promises.stat(filePath);
if (fileStat.isFile()) {
const fileData = await fs.promises.readFile(filePath);
const hash = crypto.createHash("sha256").update(fileData).digest("hex");
fileHashes.push(hash);
}
}
// Combine all file hashes and hash the result to get a unique hash for the directory
const combinedHash = crypto
.createHash("sha256")
.update(fileHashes.join(""))
.digest("hex");
return combinedHash;
}
// Instantiate Cloudflare provider
// https://www.pulumi.com/registry/packages/cloudflare/
//-----------------------------------------------------------------------------
const cloudflareProvider = new cloudflare.Provider("cloudflare", {
apiToken: apiToken,
});
// Create a Cloudflare Zone
// https://www.pulumi.com/registry/packages/cloudflare/api-docs/zone/
//-----------------------------------------------------------------------------
const myZone = new cloudflare.Zone(
"myZone",
{
zone: domainName,
plan: "enterprise",
accountId: accountId,
},
{ provider: cloudflareProvider },
);
// Create a Cloudflare Queue (used as a binding in Worker)
// https://www.pulumi.com/registry/packages/cloudflare/api-docs/queue/
//-----------------------------------------------------------------------------
const myqueue = new cloudflare.Queue(
"myqueue",
{
zoneId: myZone.id,
name: "myqueue",
description: "Queue for my messages",
accountId: accountId,
},
{ provider: cloudflareProvider },
);
// Create a Cloudflare Queue (used as a binding in Worker)
// https://www.pulumi.com/registry/packages/cloudflare/api-docs/queue/
//-----------------------------------------------------------------------------
const myqueuedeadletter = new cloudflare.Queue(
"myqueuedeadletter",
{
zoneId: myZone.id,
name: "myqueuedeadletter",
description: "Queue for messages that were not processed correctly",
accountId: accountId,
},
{ provider: cloudflareProvider },
);
// Create a D1 Database
// https://www.pulumi.com/registry/packages/cloudflare/api-docs/d1database/
//-----------------------------------------------------------------------------
const myD1Database = new cloudflare.D1Database(
"myD1Database",
{
accountId: accountId,
name: "mydb",
},
{ provider: cloudflareProvider },
);
// Deploy Changes to D1 Schema
// - Cloudflare Wrangler stores a list of migrations in the D1 database.
// - To check which migrations were run, go to the Cloudflare dashboard
// and run "SELECT * FROM d1_migrations" on the console of the D1 database.
//-----------------------------------------------------------------------------
const d1Dir = "../../mydb/";
const d1Migration = new command.local.Command(
"d1Migration",
{
dir: d1Dir,
create: `npx wrangler d1 migrations apply mydb --remote`,
triggers: [hashDirectory(`${d1Dir}migrations`)],
},
Access Pulumi exports Access Pulumi exports
Phần «Access Pulumi exports» — đọ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 "Access Pulumi exports" section below — open the official docs link for full screenshots and configuration tabs.
Once you run your Pulumi script with pulumi up, your resources will be created or updated.
The example script above also exports outputs that you can access from other tools. This is useful for example when integrating the Pulumi script into a deployment pipeline.
You could use a command similar to the following:
Terminal window
pulumi stack output myServiceTokenClientSecret --show-secrets {"@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/dynamic-provider-and-wrangler/","name":"Create different resources using Pulumi and Wrangler"}}]} 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 ↗