そろそろTerraformでGCP駆使したいなあと思い、
AWSと同じようにローカルMacにクレデンシャルを設定する必要があるので(aws cli的なもの)
今回は超備忘録!ということでいってみましょう。
※前回はCentOSからGCPに認証したい場合はやった
■Mac OS X 用のクイックスタート
https://cloud.google.com/sdk/docs/quickstart-mac-os-x?hl=ja
とりあえず公式通りやってみる。
■サービスアカウントの作成
こんな感じでterraformアカウントを作成します。
一旦役割はオーナーでキーのタイプはJSONにします。
作成するとkeyがjsonでダウンロードできるのでデスクトップに保存します。
■Install Google Cloud SDK for Mac
・download
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
[~/gcloud] adachin $ wget https://dl.google.com/dl/cloudsdk/channels/rapid/downloads/google-cloud-sdk-170.0.1-darwin-x86_64.tar.gz --2017-11-22 15:30:56-- https://dl.google.com/dl/cloudsdk/channels/rapid/downloads/google-cloud-sdk-170.0.1-darwin-x86_64.tar.gz Resolving dl.google.com... 216.58.200.206, 2404:6800:4004:817::200e Connecting to dl.google.com|216.58.200.206|:443... connected. HTTP request sent, awaiting response... 200 OK Length: 13814180 (13M) [application/octet-stream] Saving to: 'google-cloud-sdk-170.0.1-darwin-x86_64.tar.gz' google-cloud-sdk-170.0.1-darwin-x 100%[=============================================================>] 13.17M 31.5MB/s in 0.4s 2017-11-22 15:30:56 (31.5 MB/s) - 'google-cloud-sdk-170.0.1-darwin-x86_64.tar.gz' saved [13814180/13814180] [~/gcloud] adachin $ tar -zxvf google-cloud-sdk-170.0.1-darwin-x86_64.tar.gz ~省略~ [~/gcloud] adachin $ rm google-cloud-sdk-170.0.1-darwin-x86_64.tar.gz |
とりあえずホームディレクトリにgcloudディレクトリ作って突っ込みます。
(隠しディレクトリでもよかったかも)
・run install.sh
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
adachin $ ./google-cloud-sdk/install.sh Welcome to the Google Cloud SDK! To help improve the quality of this product, we collect anonymized usage data and anonymized stacktraces when crashes are encountered; additional information is available at <https://cloud.google.com/sdk/usage-statistics>. You may choose to opt out of this collection now (by choosing 'N' at the below prompt), or at any time in the future by running the following command: gcloud config set disable_usage_reporting true Do you want to help improve the Google Cloud SDK (Y/n)? y Your current Cloud SDK version is: 170.0.1 The latest available version is: 180.0.1 ┌────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐ │ Components │ ├──────────────────┬──────────────────────────────────────────────────────┬──────────────────────────┬───────────┤ │ Status │ Name │ ID │ Size │ ├──────────────────┼──────────────────────────────────────────────────────┼──────────────────────────┼───────────┤ │ Update Available │ BigQuery Command Line Tool │ bq │ < 1 MiB │ │ Update Available │ Cloud SDK Core Libraries │ core │ 7.5 MiB │ │ Update Available │ Cloud Storage Command Line Tool │ gsutil │ 3.3 MiB │ │ Not Installed │ App Engine Go Extensions │ app-engine-go │ 97.7 MiB │ │ Not Installed │ Cloud Bigtable Command Line Tool │ cbt │ 4.0 MiB │ │ Not Installed │ Cloud Bigtable Emulator │ bigtable │ 3.5 MiB │ │ Not Installed │ Cloud Datalab Command Line Tool │ datalab │ < 1 MiB │ │ Not Installed │ Cloud Datastore Emulator │ cloud-datastore-emulator │ 17.7 MiB │ │ Not Installed │ Cloud Datastore Emulator (Legacy) │ gcd-emulator │ 38.1 MiB │ │ Not Installed │ Cloud Pub/Sub Emulator │ pubsub-emulator │ 33.2 MiB │ │ Not Installed │ Emulator Reverse Proxy │ emulator-reverse-proxy │ 14.5 MiB │ │ Not Installed │ Google Container Local Builder │ container-builder-local │ 3.7 MiB │ │ Not Installed │ Google Container Registry's Docker credential helper │ docker-credential-gcr │ 2.2 MiB │ │ Not Installed │ gcloud Alpha Commands │ alpha │ < 1 MiB │ │ Not Installed │ gcloud Beta Commands │ beta │ < 1 MiB │ │ Not Installed │ gcloud app Java Extensions │ app-engine-java │ 118.4 MiB │ │ Not Installed │ gcloud app PHP Extensions │ app-engine-php │ 21.9 MiB │ │ Not Installed │ gcloud app Python Extensions │ app-engine-python │ 6.2 MiB │ │ Not Installed │ kubectl │ kubectl │ 12.2 MiB │ └──────────────────┴──────────────────────────────────────────────────────┴──────────────────────────┴───────────┘ To install or remove components at your current SDK version [170.0.1], run: $ gcloud components install COMPONENT_ID $ gcloud components remove COMPONENT_ID To update your SDK installation to the latest version [180.0.1], run: $ gcloud components update ==> Source [/Users/adachin/gcloud/google-cloud-sdk/completion.zsh.inc] in your profile to enable shell command completion for gcloud. ==> Source [/Users/adachin/gcloud/google-cloud-sdk/path.zsh.inc] in your profile to add the Google Cloud SDK command line tools to your $PATH. For more information on how to get started, please visit: https://cloud.google.com/sdk/docs/quickstarts |
からの私はzshを利用しているのでシェルを読み込ませます。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
[~/gcloud/google-cloud-sdk] adachin $ source path.zsh.inc adachin $ source completion.zsh.inc [~/gcloud/google-cloud-sdk] adachin $ gcloud -h Usage: gcloud [optional flags] <group | command> group may be app | auth | components | compute | config | container | dataflow | dataproc | datastore | debug | deployment-manager | dns | firebase | iam | kms | logging | ml | ml-engine | organizations | projects | service-management | source | spanner | sql | topic command may be docker | feedback | help | info | init | version For detailed information on this command and its flags, run: gcloud --help |
これでgcloudコマンドが使えるようになった。
・gcloud auth
1 2 3 4 5 6 7 8 9 10 11 12 13 |
[~/gcloud/google-cloud-sdk] adachin $ cat terraform-xxxxxxxxx.json "type": "service_account", "project_id": "mythic-xxxxxxxxxxx", "private_key_id": "exxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", "private_key": "-----BEGIN PRIVATE KEY-----\xxxxxxxxxxxxxxxxxxxxxxxxxx-----END PRIVATE KEY-----\n", "client_email": "xxxxxxxxxxx-terraform@mythic-xxxxxxxxxxxxxx.iam.gserviceaccount.com", "client_id": "xxxxxxxxxxxxxxxxxxxxxxxxxxxx", "auth_uri": "https://accounts.google.com/o/oauth2/auth", "token_uri": "https://accounts.google.com/o/oauth2/token", "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs", "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/verification-terraform%40mythic-xxxxxxxxxxxxxx.gserviceaccount.com" } |
一旦先ほどサービスアカウントを作成したjsonファイルを見てみましょう。
上記を参考にしてアカウントを認証してみます。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
[~/gcloud] adachin $ gcloud auth activate-service-account xxxxxxxxxx-terraform@mythic-xxxxxxxxxxxxxx.iam.gserviceaccount.com --key-file /Users/adachin/gcloud/terraform-xxxxxxxxx.json Activated service account credentials for: [xxxxxxxxxx-terraform@mythic-xxxxxxxxxxxxxx.iam.gserviceaccount.com] [~/gcloud] adachin $ gcloud config set project mythic-xxxxxxxxxxx Updated property [core/project]. [~/gcloud] adachin $ gcloud auth list Credentialed Accounts ACTIVE ACCOUNT * xxxxxxxxxx-terraform@mythic-xxxxxxxxxxxxxx.iam.gserviceaccount.com To set the active account, run: $ gcloud config set account `ACCOUNT` [~/gcloud] adachin $ gcloud config list [core] account = xxxxxxxxxx-terraform@mythic-xxxxxxxxxxxxxx.iam.gserviceaccount.com disable_usage_reporting = False project = mythic-xxxxxxxxxxx Your active configuration is: [default] |
よしできた!
■まとめ
ハマったのzsh読み込ませるくらいw
アカウント切り替えたい場合は$ gcloud config set account アカウント名
でOK。Terraformやります。
0件のコメント