gcloudの認証の設定と複数アカウントを使い分ける設定
ここでは事前にGoogle Cloud SDKが導入されて、gcloudコマンドが扱えることを前提とします。
インストール方法は各環境によって違うのでこちらを参考にインストールしてみてください。
自分のgcloudの環境は以下のようになっています。
$ gcloud -v
Google Cloud SDK 289.0.0
alpha 2019.05.17
app-engine-python 1.9.90
app-engine-python-extras 1.9.90
beta 2019.05.17
bq 2.0.56
cloud-datastore-emulator 2.1.0
core 2020.04.10
gsutil 4.49
初期設定を行う
まずはgcliud initで初期設定を行います。初めての場合は以下のような文言が表示されるはずです。
$ 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を入力してEnterを押しましょう。するとブラウザが立ち上がるはずです。
アカウントの選択とGoogleアカウントの選択が表示され、認証作業を終えれば認証作業は完了です。
認証作業が完了すれば「Google Cloud SDK 認証の完了」という以下のリンクにリダイレクトされるはずです。
https://cloud.google.com/sdk/auth_success
複数アカウントを使い分ける設定
通常の設定通りにgcloud initなどをすれば一つのプロジェクトに設定できます。しかし、場合によっては一つのアカウントに対して複数のプロジェクトを紐付けたり、デプロイ時や設定時にプロジェクトを切り替えたい場合もあるはずです。
一つのアカウントに対して複数のプロジェクトを紐づけたり、プロジェクトを一コマンドで切り替えたりするやり方を紹介していきます。
現在のプロジェクトの設定を確認
まずは現在のプロジェクトの設定を確認していきます。 初期設定ではdefaultという名前になっているはずです。
Your active configuration is: となっているのが現在設定しているプロジェクトになります。
$ gcloud config list
[compute]
region = asia-northeast1
zone = asia-northeast1-a
[core]
account = xxx.yyy@gmail.com
disable_usage_reporting = False
project = sample-project
Your active configuration is: [default]
紐づいている複数のプロジェクトを確認
gcloud config configurationsで現在紐づいているプロジェクトを確認します。IS_ACTIVEがTrueになっている物が現在設定されているアカウントになります。
現在は一つだけですが、追加していくとこれが複数に積み重なっていきます。
$ gcloud config configurations list
NAME IS_ACTIVE ACCOUNT PROJECT COMPUTE_DEFAULT_ZONE COMPUTE_DEFAULT_REGION
default True xxx.yyy@gmail.com sample-project
新しくプロジェクトを追加する
次に新しいプロジェクトを追加していく方法です。以下のコマンドでsample2というプロジェクト設定名を追加します。
gcloud config configurations create [プロジェクト設定名]
$ gcloud config configurations create sample2
Created [sample2].
Activated [sample2].
ここでの「プロジェクト設定名」はgcp上のプロジェクト名でなくても問題なく、あくまでも自分のgcloud上での名称を設定します。
Activated [sample2]. となってるように、これでdefaultからsample2に設定が切り替わってしまっているはずです。
gcloud config listで設定を確認すると先ほどと内容が変わっているはずです。
$ gcloud config list
[core]
disable_usage_reporting = False
Your active configuration is: [sample2]
再度、gcloud config configurations list をしてみると、一つプロジェクトが増えているはずです。
$ gcloud config configurations list
NAME IS_ACTIVE ACCOUNT PROJECT COMPUTE_DEFAULT_ZONE COMPUTE_DEFAULT_REGION
default False xxx.yyy@gmail.com sample-project
sample2 True
プロジェクトに設定を施す
一番最初のプロジェクトでは、gcloud initで勝手によしなに設定をしてくれます。
新しくプロジェクトを追加したとしても何も設定を施さなければ扱うことはできません。そこで新たに設定を加えていきましょう。
$ gcloud config set core/account xxx.yyy2@gmail.com
Updated property [core/account].
$ gcloud config set core/project sample2-project
Updated property [core/project].
$ gcloud config set core/disable_usage_reporting False
Updated property [core/disable_usage_reporting].
再度、gcloud config listのコマンドを叩いてみましょう。以下のようにdisable_usage_reportingだけだったのが、項目がいくつか増えているはずです。
[core]
account = xxx.yyy2@gmail.com
disable_usage_reporting = False
project = sample2-project
Your active configuration is: [sample2]
認証処理をしてプロジェクトとアカウントの紐付けを行う
この後は実際に認証処理を施してアカウントとgcpのプロジェクトの紐付けを行う必要があります。gcloud projects listを叩いてみましょう。以下のような文言が表示されるはずです。
$ gcloud projects list
ERROR: (gcloud.projects.list) Your current active account [xxx.yyy2@gmail.com] does not have any valid credentials
Please run:
$ gcloud auth login
to obtain new credentials, or 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 loginを叩いて認証処理を施しましょう。
ブラウザが起動してアカウントの選択と認証処理が始まります。
認証が完了した後に、再度 gcloud projects list を叩いてみましょう。(xxxyyydddには数値型が入るはずです)
紐付けが行われていれば以下のように表示されているはずです。
PROJECT_ID NAME PROJECT_NUMBER
sample2-project sample2 xxxyyyddd
プロジェクトの切り替え
プロジェクトは以下のコマンドで簡単に切り替えることができます。
$ gcloud config configurations activate [切り替えたいプロジェクト名]
Activated [default].
$ gcloud config configurations activate default
Activated [default].
$ gcloud config configurations activate sample2
Activated [sample2].
参考資料
この記事が気に入ったらサポートをしてみませんか?