Cloudflare Workers用のCLIツール「Cloudflare Wrangler」を使ってみる

2023/11/14に公開

Cloudflare Wranglerを触ってみる

ドキュメント
https://developers.cloudflare.com/workers/wrangler/


インストール

$ brew install node
$ brew update
$ brew upgrade
$ npm install -g wrangler

ログイン

$ wrangler login
 ⛅️ wrangler 3.15.0
-------------------
Attempting to login via OAuth...
Opening a link in your default browser: https://dash.cloudflare.com/oauth2/auth?response_type=code&client_id=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxxx&redirect_uri=http%
~(中略)~


既定ブラウザで以下が表示されます。

内容を読んで「Allow」を押下。

※翻訳


このタイミングでブラウザに以下の画面も表示されていたようです。
(後で気付いたので正確には別のタイミングかも)

ログインに成功したようです。

Successfully logged in.
✔ Would you like to help improve Wrangler by sending usage metrics to Cloudflare? … yes
Your choice has been saved in the following file: ../../../Library/Preferences/.wrangler/metrics.json.

  You can override the user level setting for a project in `wrangler.toml`:

   - to disable sending metrics for a project: `send_metrics = false`
   - to enable sending metrics for a project: `send_metrics = true`

触ってみる。

init(今後はnpm create cloudflare@2 -- wranglertest)

$ wrangler init wranglertest

 ⛅️ wrangler 3.15.0
-------------------
Using npm as package manager.
▲ [WARNING] The `init` command is no longer supported. Please use `npm create cloudflare\@2 -- wranglertest` instead.

  The `init` command will be removed in a future version.


Running `npm create cloudflare\@2 -- wranglertest`...
Need to install the following packages:
  create-cloudflare@2.6.2
Ok to proceed? (y) y

using create-cloudflare version 2.6.2

╭ Create an application with Cloudflare Step 1 of 3
│
├ In which directory do you want to create your application?
│ dir ./wranglertest
│
├ What type of application do you want to create?
│ type "Hello World" Worker
│
├ Do you want to use TypeScript?
│ no typescript
│
├ Copying files from "hello-world" template
│
├ Retrieving current workerd compatibility date
│ compatibility date 2023-10-30
│
╰ Application created

╭ Installing dependencies Step 2 of 3
│
├ Installing dependencies
│ installed via `npm install`
│
├ Committing new files
│ git commit
│
╰ Dependencies Installed

╭ Deploy with Cloudflare Step 3 of 3
│
├ Do you want to deploy your application?
│ yes deploy via `npm run deploy`
│
├ Logging into Cloudflare checking authentication status
│ logged in
│
├ Selecting Cloudflare account retrieving accounts
│ account Xxxxxxxxx@xxxx.com's Account
│
├ Deploying your application
│ deployed via `npm run deploy`
│
├  SUCCESS  View your deployed application at https://wranglertest.Xxxxxxxxx.workers.dev
│
│ Navigate to the new directory cd wranglertest
│ Run the development server npm run start
│ Deploy your application npm run deploy
│ Read the documentation https://developers.cloudflare.com/workers
│ Stuck? Join us at https://discord.gg/cloudflaredev
│
├ Waiting for DNS to propagate
│ DNS propagation complete.
│
├ Waiting for deployment to become available
│ deployment is ready at: https://wranglertest.Xxxxxxxxx.workers.dev
│
├ Opening browser
│
╰ See you again soon!

「initコマンドはサポートされなくなりました。代わりに npm create cloudflare@2 -- wranglertest を使ってください。」との事。

↓も実行してざっと見比べてみた限り、CLI上で旧コマンド実行結果との見た目の違いは無さそうでした。

$ npm create cloudflare@2 -- wranglertest

※initの対話の中でdeployしますか?にyesと回答するとwrangler deployも実行してくれました。


puiblish(今後はwrangler deploy)

wrangler publish
 ⛅️ wrangler 3.15.0
-------------------
▲ [WARNING] `wrangler publish` is deprecated and will be removed in the next major version.

  Please use `wrangler deploy` instead, which accepts exactly the same arguments.


Total Upload: 0.19 KiB / gzip: 0.16 KiB
Uploaded wranglertest (0.77 sec)
Published wranglertest (0.34 sec)
  https://wranglertest.Xxxxxxxxx.workers.dev
Current Deployment ID: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx

「wrangler publishは非推奨であり、次のメジャーバージョンで削除される予定です。代わりに、全く同じ引数を受け付ける wrangler deploy` を使ってください。」との事。

こちらの実行結果も、CLI上で旧コマンド実行結果との見た目の違いは無さそうでした。

$ wrangler deploy

確認

表示されたURLにアクセスすると「Hello World!」が確認出来ました。


コンソールでも確認出来ました。


~ 遷移先画面上部 ~

ここでいう493ab67dはデプロイIDのようです。


~ 遷移先画面下部 ~

Metrics


Triggers


Logs


Deployments


Integrations


Settings

お片付け(delete)

削除します。

wrangler delete
 ⛅️ wrangler 3.15.0
-------------------
✔ Are you sure you want to delete wranglertest? This action cannot be undone. … yes
Successfully deleted wranglertest


以上でした

改めてwrangler -hを眺めてみた所、気になる所も色々ありましたので理解が深める為にこれを機に勉強してみようと思いました。

wrangler -h
$ wrangler -h
wrangler

Commands:
  wrangler docs [command..]            📚 Open wrangler's docs in your browser
  wrangler init [name]                 📥 Initialize a basic Worker project, including a wrangler.toml file
  wrangler generate [name] [template]  ✨ Generate a new Worker project from an existing Worker template. See https://github.com/cloudflare/templates
  wrangler dev [script]                👂 Start a local server for developing your worker
  wrangler deploy [script]             🆙 Deploy your Worker to Cloudflare.  [aliases: publish]
  wrangler delete [script]             🗑  Delete your Worker from Cloudflare.
  wrangler tail [worker]               🦚 Starts a log tailing session for a published Worker.
  wrangler secret                      🤫 Generate a secret that can be referenced in a Worker
  wrangler secret:bulk [json]          🗄️  Bulk upload secrets for a Worker
  wrangler kv:namespace                🗂️  Interact with your Workers KV Namespaces
  wrangler kv:key                      🔑 Individually manage Workers KV key-value pairs
  wrangler kv:bulk                     💪 Interact with multiple Workers KV key-value pairs at once
  wrangler pages                       ⚡️ Configure Cloudflare Pages
  wrangler queues                      🇶 Configure Workers Queues
  wrangler r2                          📦 Interact with an R2 store
  wrangler dispatch-namespace          📦 Interact with a dispatch namespace
  wrangler d1                          🗄  Interact with a D1 database
  wrangler hyperdrive                  🚀 Configure Hyperdrive databases
  wrangler ai                          🤖 Interact with AI models
  wrangler constellation               🤖 Interact with Constellation models
  wrangler vectorize                   🧮 Interact with Vectorize indexes
  wrangler pubsub                      📮 Interact and manage Pub/Sub Brokers
  wrangler mtls-certificate            🪪 Manage certificates used for mTLS connections
  wrangler login                       🔓 Login to Cloudflare
  wrangler logout                      🚪 Logout from Cloudflare
  wrangler whoami                      🕵️  Retrieve your user info and test your auth config
  wrangler types                       📝 Generate types from bindings & module rules in config
  wrangler deployments                 🚢 List and view details for deployments
  wrangler rollback [deployment-id]    🔙 Rollback a deployment

Flags:
  -j, --experimental-json-config  Experimental: Support wrangler.json  [boolean]
  -c, --config                    Path to .toml configuration file  [string]
  -e, --env                       Environment to use for operations and .env files  [string]
  -h, --help                      Show help  [boolean]
  -v, --version                   Show version number  [boolean]

有難うございました。

Discussion