LoginSignup
2
0

More than 1 year has passed since last update.

CloudSDK環境構築【macOS】

Last updated at Posted at 2021-12-25

やりたいこと

BigQueryをローカルから操作する必要がありました。
BigQueryを操作するbqコマンド、CloudStorageを操作するgsutilコマンド、GCEやGKE等を操作するgcloudコマンドをローカルで使うにはGoogle Cloud SDKが必要です。

こちらの記事では、CloudSDKを動かすのに必要なPython3.7の環境構築を行いました。
こちらの記事では、いよいよCloudSDKの環境構築を行います。
とはいっても、Homebrewであっという間に構築できちゃうんですけどね。一応記録しておきます。

そもそもCloudSDKとは

公式ドキュメントより

Cloud SDK は、Google Cloud でホストされるリソースやアプリケーションの管理に使用できる一連のツールです。これらのツールには、gcloud、gsutil、bq コマンドライン ツールが含まれます

要は、GCPのサービスをコマンドで操作するためのツールということらしいです。

CloudSDKインストール

環境

  • Homebrew 3.3.9
  • Homebrew/homebrew-core (git revision bf17255fe5d; last commit 2021-12-25)
  • Homebrew/homebrew-cask (git revision 323530bc91; last commit 2021-12-25)

Homebrewでインストール

Homebrewでインストールします。

余談ですが、caskコマンドの書き方が変わったそうです。

# 旧
$ brew cask install hogehoge
# 新
$ brew install hogehoge --cask

今回も、新しい書き方に則ってインストールしましょう。

$ brew install google-cloud-sdk --cask

私の環境では、インストールに5分ほどかかりました。

インストールの途中に、以下のようにありましたので、

==> Source [/usr/local/Caskroom/google-cloud-sdk/latest/google-cloud-sdk/completion.zsh.inc] in your profile to enable shell command completion for gcloud.
==> Source [/usr/local/Caskroom/google-cloud-sdk/latest/google-cloud-sdk/path.zsh.inc] in your profile to add the Google Cloud SDK command line tools to your $PATH.

.zshrcに以下を追記します。

.zshrc
source '/usr/local/Caskroom/google-cloud-sdk/latest/google-cloud-sdk/completion.zsh.inc'
source '/usr/local/Caskroom/google-cloud-sdk/latest/google-cloud-sdk/path.zsh.inc'

.zshrcに追記後は $ source ~/.zshrc をお忘れなく。

ここまで完了したら、バージョンを確認してみましょう。

$ gcloud --version
Google Cloud SDK 367.0.0
bq 2.0.72
core 2021.12.10
gsutil 5.5

無事インストールできました!

アカウントの登録(初期設定)

初回実行時は、GCPサービスアカウントの登録が必要になります。

アカウントが登録されていない状態でbqコマンドを実行すると、以下のエラーとなりました。

ERROR: (bq) You do not currently have an active account selected.
Please run:
  $ gcloud auth login
to obtain new credentials.
If you have already logged in with a different account:
    $ gcloud config set account ACCOUNT
to select an already authenticated account to use.

現在設定されている認証情報を確認することができます。

$ gcloud auth list
No credentialed accounts.
To login, run:
  $ gcloud auth login `ACCOUNT`

認証情報が登録されていないようですね。

認証には、CloudSDKの初期設定が必要なようでした。

$ gcloud init
Welcome! This command will take you through the configuration of gcloud.

Your current configuration has been set to: [default]

You can skip diagnostics next time by using the following flag:
  gcloud init --skip-diagnostics

Network diagnostic detects and fixes local network connection issues.
Checking network connection...done.
Reachability Check passed.
Network diagnostic passed (1/1 checks passed).

You must log in to continue. Would you like to log in (Y/n)?

Yで続けると、ブラウザが起動します。
作成済みのGCPサービスアカウントでログインすると、こちらのページに飛びます。

ターミナルに戻ると、デフォルトで使用するプロジェクトを選ぶように言われております。

You are logged in as: [fuga@hoge].

Pick cloud project to use:
 [1] hoge-dev
 [2] hoge-staging
 [3] hoge-prd
 [4] Create a new project
Please enter numeric choice or text value (must exactly match list item):

使用するプロジェクトの番号を入力します。

これで初回の認証は完了です。

参考にさせていただいた記事

2
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
2
0