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?

Terrafromの学習メモ その1

Posted at

概要

Terraformを初めて使ったので、その時の作業メモ
GCSのバケットを作成して、それにアクセスするサービスアカウントを作る。

手順

GCSのバケットを作る

resource "google_storage_bucket" "xxx_gcs_bucket" {
  name                        = var.xxx_gcs_bucket_name
  project                     = var.project_id
  location                    = var.region
  uniform_bucket_level_access = true
  storage_class               = "STANDARD"
  public_access_prevention    = "enforced"

  versioning {
    enabled = true
  }
}

public_access_prevention = "enforced"と指定すると、公開アクセスを禁止できる。
public_access_prevention = "inherited" だとプロジェクトや組織のポリシーを継承する。
参考 → https://cloud.google.com/storage/docs/public-access-prevention?hl=ja

サービスアカウントとIAMを作成

サービスアカウントを定義して

resource "google_service_account" "xxx_gcs_access_sa" {
  project      = var.project_id
  account_id   = "xxx-gcs-access-sa"
  display_name = "GCS Access Service Account"
}

それにIAMの権限をつける。

resource "google_storage_bucket_iam_member" "cdn_bucket_sa_access" {
  bucket = google_storage_bucket.xxx_gcs_bucket.name
  role   = "roles/storage.objectViewer"
  member = "serviceAccount:${google_service_account.xxx_gcs_access_sa.email}"
}

これに以下のようなoutputを定義しておくと、別のモジュールから利用できるようになる。

output "xxx_gcs_access_sa_email" {
  value       = google_service_account.xxx_gcs_access_sa.email
  description = "Email of the GCS access service account."
}

もし全ユーザーからアクセス(読み取り)させてよいならば以下のようになる
(バケットの定義で public_access_prevention = "inherited" としておかないと allUsers にアクセスを許可できない)

# CDN用バケットのパブリック読み取り権限
resource "google_storage_bucket_iam_member" "xxx_bucket_public_read" {
  bucket = google_storage_bucket.xxx_gcs_bucket.name
  role   = "roles/storage.objectViewer"
  member = "allUsers"  # インターネット上の全ユーザーにアクセス許可
}

感想

UIから手動での操作と、terraformの記述を比較しながら慣れていくのが良さそう。
AIさんに書いてもらうと自分が理解する前に実装が終わってしまうので、一旦まとめる時間は大切かも。

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?