0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

wrangler が「意図しないCloudflareアカウント」を掴む2つのパターンと対処

0
Last updated at Posted at 2026-08-07

wrangler が「意図しないCloudflareアカウント」を掴む2つのパターンと対処

1つのCloudflareログインに複数アカウントが紐づいていると、wrangler が「どのアカウントで実行するか」を決められず失敗することがあります。厄介なのは、対話的に叩いている限りほぼ気づかない点です。実際に踏んだ2パターンと、その切り分け方をまとめます。

パターン1: 非対話環境で即エラーになる

スクリプトやcronからD1を叩いたところ、次のエラーで止まりました。

✘ [ERROR] More than one account available but unable to select one in non-interactive mode.
Please set the appropriate `account_id` in your Wrangler configuration file
or assign it to the `CLOUDFLARE_ACCOUNT_ID` environment variable.
Available accounts are (`<name>`: `<account_id>`):
  `foo@example.com's Account`: `<account_id_A>`
  `bar@example.com's Account`: `<account_id_B>`

手元のターミナルでは、wrangler がアカウント選択のプロンプトを出してくれます。人間が選ぶので問題は表面化しません。ところがCI・cron・自動化スクリプトのように標準入力が無い環境では、選ばせる相手がいないため即座に失敗します。

対処はアカウントIDの明示です。環境変数なら次のとおりです。

CLOUDFLARE_ACCOUNT_ID=<account_id> npx wrangler d1 execute <DB_NAME> --remote --command "SELECT 1"

恒久的に固定するなら wrangler.toml に書きます。

account_id = "<account_id>"

パターン2: そもそも別アカウントにログインしている

こちらのほうが危険です。エラーにならず、別アカウント側で処理が通ってしまうか、リソースが見つからないという一見無関係なエラーになります。まず今どのアカウントなのかを確認します。

npx wrangler whoami

意図しないアカウントだった場合は、ログアウトしてから入り直します。既定ブラウザに別アカウントのセッションが残っていると、OAuth同意画面がそのまま素通りして同じアカウントを掴み直すため、ログイン前に正しいアカウントでCloudflareのダッシュボードを開いておくと確実です。

npx wrangler logout
npx wrangler login

学んだこと

whoami が通ることと、配信先が正しいことは別の話でした。複数アカウントが1つのログインに紐づく環境では、account_id を明示しない限り「解決先が実行環境によって変わる」状態が残り続けます。自動デプロイのように人の目が入らない経路ほど、環境変数か wrangler.toml でアカウントを固定しておくのが安全です。


筆者は宅建士試験の学習アプリ「宅建BOOST」を開発しています。開発知見を今後も投稿しますので、フォローいただけると励みになります。
X: https://x.com/takkenboost

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?