5
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

はじめに

Claude CodeはNode.js 18以上が動作するUnix互換OS(macOS / Linux / WSLなど)であればCLIとして実行できます。
それならGoogle Cloud Shellでも動くのでは? と考え、実際に試してみました。この記事ではCloud Shell上でClaude Codeをセットアップし、Google Cloudリソースを操作するまでの手順を紹介します。

初期設定

Vertex AIをモデルプロバイダーとする方法で進めます。

Vertex AI Model GardenでClaudeモデルを有効化したGoogle Cloudプロジェクトを用意します。

Claudeの呼び出しコストはまあまあお値段が張るので、予算アラートは必ず設定しておきましょう。

またそのプロジェクトに「Vertex AI ユーザー」権限を持ったIAMを追加します。

そのIAMと同じプリンシパル(メルアド)のユーザーで任意のGoogle Cloudプロジェクトを操作します(Claudeモデルを有効化したGoogle Cloudプロジェクトでなくても構いません)。

Cloud Shellを起動し、Vertex AIでClaude Codeを使うための設定ファイルを作成します。
プロジェクトIDはお使いのClaudeモデルを有効化したGoogle CloudプロジェクトのIDに置き換えてください。
ホームディレクトリで実行します。

mkdir -p .claude && cat <<'EOF' > .claude/settings.json
{
  "env": {
    "ANTHROPIC_MODEL": "claude-opus-4@20250514",
    "ANTHROPIC_SMALL_FAST_MODEL": "claude-sonnet-4@20250514",
    "CLAUDE_CODE_USE_VERTEX": 1,
    "CLOUD_ML_REGION": "us-east5",
    "ANTHROPIC_VERTEX_PROJECT_ID": "<your_project_id>"
  }
}
EOF

Claude Codeをインストールします。

npm install -g @anthropic-ai/claude-code

Claude Codeを起動します。

claude

追加コンテキストを与えるために下記のようなCLAUDE.mdもホームディレクトリに作成しておきます。

CLAUDE.md
* 必ず日本語で応答してください。
* あなたの実行環境はGoogle CloudのCloud Shellであることを念頭においてください。

使ってみる

Cloud Shellで実行している強みを活かし、Google Cloudのリソースを操作してみます。
ローカルで動かしていてもgcloudコマンドが使えればできることではありますが、特にセットアップすることなく利用できるのは便利ですしなんかワクワクします。

適当にCloud Storageバケットを作らせてみます。

> 東京リージョンにバケットを作成して。

● 東京リージョン(asia-northeast1)に新しいバケットを作成します。

● Bash(gcloud storage buckets create gs://******-test-tokyo-$(date +%Y%m%d) --location=asia-northeast1)
  ⎿  Welcome to Cloud Shell! Type "help" to get started.                           
     Your Cloud Platform project in this session is set to <your_project_id>.
     Use `gcloud config set project [PROJECT_ID]` to change to a different project.
     … +1 line (ctrl+r to expand)

● Bash(gcloud storage buckets describe gs://******-test-tokyo-20250622 --format="table(name,location,storageClass)")
  ⎿  Welcome to Cloud Shell! Type "help" to get started.                           
     Your Cloud Platform project in this session is set to <your_project_id>.
     Use `gcloud config set project [PROJECT_ID]` to change to a different project.
     … +3 lines (ctrl+r to expand)

● 東京リージョン(asia-northeast1)に ******-test-tokyo-20250622 というバケットを作成しました。

> Hello Worldと書かれたテキストファイルを作成。バケットにアップロードして。

● Hello Worldと書かれたテキストファイルを作成してバケットにアップロードします。

● Bash(echo "Hello World" > hello.txt)
  ⎿  Welcome to Cloud Shell! Type "help" to get started.                           
     Your Cloud Platform project in this session is set to <your_project_id>.
     Use `gcloud config set project [PROJECT_ID]` to change to a different project.

● Bash(gcloud storage cp hello.txt gs://******-test-tokyo-20250622/)
  ⎿  Welcome to Cloud Shell! Type "help" to get started.                           
     Your Cloud Platform project in this session is set to <your_project_id>.
     Use `gcloud config set project [PROJECT_ID]` to change to a different project.
     … +1 line (ctrl+r to expand)

● Bash(gcloud storage ls gs://******-test-tokyo-20250622/)
  ⎿  Welcome to Cloud Shell! Type "help" to get started.                           
     Your Cloud Platform project in this session is set to <your_project_id>.
     Use `gcloud config set project [PROJECT_ID]` to change to a different project.
     … +1 line (ctrl+r to expand)

● 完了しました。hello.txtファイルを作成し、gs://******-test-tokyo-20250622/バケットにアップロードしました。

image.png

Welcomeメッセージがちょっとうざったいですが意図した操作が実行できました。
特に指示しなくてもプロジェクトIDは起動している場所を採用して動いてくれているので使いやすいです。

gcloudコマンドでできる操作は全てできるので、Cloud Runのデプロイをさせたり、Cloud Loggingの解析をさせたりとオペレーション自動化の可能性も感じます。

破壊的なコマンドが実行される恐れがあるため、実行IAMユーザーの権限管理、Claude Codeの権限設定を適切に行った上で運用しましょう。

また、Cloud Shellエディタ経由でコーディング支援にも使えました。

image.png

ここでは自作のADKで作ったアプリにテストコードを追加させています。

おまけ

Vertex AIを利用して、チームでClaude CodeやClaude Code Actionを使ってみたい方はこちらの記事をどうぞ。

5
1
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
5
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?