{GCP}サービス アカウントとして認証する

https://cloud.google.com/docs/authentication/production?hl=ja#linux-or-macos
https://blog.1q77.com/2020/01/gcloud-switch-account-and-project/
https://cloud.google.com/docs/authentication/gcloud?hl=ja

 


-- 1. 前作業

gcloud init
gcloud auth list

gcloud --version

gcloud projects create project01-9999999 \
--name="project01"

gcloud config list
gcloud config set project project01-9999999
gcloud config set compute/region asia-northeast1 --quiet
gcloud config set compute/zone asia-northeast1-a --quiet

gcloud beta billing accounts list
gcloud beta billing projects link project01-9999999 --billing-account=111111-111111-111111

gcloud services enable compute.googleapis.com --project project01-9999999

gcloud components update


-- 2. サービス アカウントの作成

gcloud iam service-accounts create sa99999999 \
--description="sa99999999" \
--display-name="sa99999999"

gcloud iam service-accounts list


gcloud projects add-iam-policy-binding project01-9999999 \
--member="serviceAccount:sa99999999@project01-9999999.iam.gserviceaccount.com" \
--role="roles/owner"

gcloud projects get-iam-policy project01-9999999


gcloud iam service-accounts keys create ~/key01.json \
--iam-account=sa99999999@project01-9999999.iam.gserviceaccount.com

cat ~/key01.json

gcloud iam service-accounts keys list \
--iam-account=sa99999999@project01-9999999.iam.gserviceaccount.com

 

 

-- 3. サービス アカウントでログイン

gcloud auth list
gcloud auth revoke
gcloud auth list


gcloud auth login --cred-file=/Users/test/key01.json

jsonファイルは絶対パスで指定必要


gcloud auth list

gcloud services enable cloudresourcemanager.googleapis.com

gcloud config set project project01-9999999
gcloud config set compute/region asia-northeast1 --quiet
gcloud config set compute/zone asia-northeast1-a --quiet

gcloud projects list

 


gcloud compute instances create vm01 \
--machine-type=e2-micro \
--image-project=centos-cloud \
--image=centos-7-v20221004 \
--network-interface=network-tier=STANDARD

gcloud compute instances list

gcloud compute ssh vm01

 


-- 4. クリーンアップ


gcloud auth list
gcloud auth revoke sa99999999@project01-9999999.iam.gserviceaccount.com
gcloud auth list

 

gcloud init

gcloud projects list

gcloud projects delete project01-9999999 \
--quiet