0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

サービスアカウントキーなしでTerraformを実行する方法

Posted at

概要

Terraformの実行方法で多くの場合サービスアカウントキーをダウンロードする手順となっているため、
GCP上のリソース作成をサービスアカウントキーを生成せずにTerraformで行う方法を記載する

手順

1. Terraform用のサービスアカウント作成

gcloud iam service-accounts create terraform --display-name "Account for Terraform"
gcloud projects add-iam-policy-binding ${PROJECT_ID} \
  --member serviceAccount:terraform@${PROJECT_ID}.iam.gserviceaccount.com \
  --role roles/editor

2. 操作ユーザにサービス アカウント トークン作成者 (roles/iam.serviceAccountTokenCreator) 権限を付与

gcloud projects add-iam-policy-binding ${PROJECT_ID} \
  --member user:${USER_EMAIL} \
  --role roles/iam.serviceAccountTokenCreator

3. 実行するMakeFileを作成する

"sa"を「1.」で作成したサービスアカウント名に変更

SERVICE_ACCOUNT = "sa"

SHELL := /usr/bin/env zsh

init:
	@GOOGLE_OAUTH_ACCESS_TOKEN="$(shell gcloud --impersonate-service-account=${SERVICE_ACCOUNT} auth print-access-token)" terraform init

plan:
	@GOOGLE_OAUTH_ACCESS_TOKEN="$(shell gcloud --impersonate-service-account=${SERVICE_ACCOUNT} auth print-access-token)" terraform plan

apply:
	@GOOGLE_OAUTH_ACCESS_TOKEN="$(shell gcloud --impersonate-service-account=${SERVICE_ACCOUNT} auth print-access-token)" terraform apply

destroy:
	@GOOGLE_OAUTH_ACCESS_TOKEN="$(shell gcloud --impersonate-service-account=${SERVICE_ACCOUNT} auth print-access-token)" terraform destroy

.PHONY: init plan apply destroy

4. TerraformをMakeFile経由で実行

make plan

「make: Nothing to be done for `plan'.」が発生

コマンド行がTABで始まっていない場合発生する。

参考サイト (元ネタ)

Stop Downloading Google Cloud Service Account Keys! | by Ryan Canty | Medium

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?