LoginSignup
29
10

Cloud Runの権限設定のために初めてTerraformを使った。むず。

Last updated at Posted at 2023-12-13

こんにちは。
フロントエンドエンジニアのみつです。

先日、Cloud Runを利用するためTerraformを初めて書きました。

権限を付与しようとしたんですが、.tfファイルなんてそもそも作ったことはなく、google_project_iamのページを読んで色々と学びになったので残します。

目次

今回追加したコード(.tfファイル) 

resource "google_project_iam_member" "何かしら権限の任意名" {
  project = PROJECT_ID
  role    = "roles/run.developer"
  member = "user:権限を付与するメールアドレス"
  condition {
    title       = "expires_after_2023-10-31"
    description = "Expiring at midnight of 2023-10-31"
    expression  = "request.time < timestamp(\"2023-11-01T00:00:00Z\")"
  }
}

そもそも、Google CloudのIAMって??

  • Identity and Access Management(IAM)の略称
  • 誰がどのリソースに対してどのようなアクセス権を持つかを管理しているもの
  • IAMでは、権限をservice.resource.verbで表現する

IAMは、Google Cloudで使われている仕組みの一つ。

基本的には、1つのParentに紐づく様々あるリソースに対して、各アカウントで何のアクションの権限があるかを管理するものらしい。

そもそも、Terraformって??

「Infrastructure as Code」らしいけど、つまりは、インフラリソースの管理を設定ファイルとして管理しましょう!ができるもの。

今回であれば、Google Cloudのリソース管理を設定ファイル(.tf)でやりましょうということ。

Terraformでresourceに指定できる4つ

Four different resources help you manage your IAM policy for a project. Each of these resources serves a different use case:

4つの異なるリソースは、それぞれのプロジェクトにおけるIAM管理のためのもの。

今回は、「google_project_iam_member」を使用した。

以下の4種類があるらしい。

  • google_project_iam_policy

_policyは、すでに設定されている既存のポリシーを置き換える強いやつ。

Sets the IAM policy for the project and replaces any existing policy already attached.

  • google_project_iam_binding

a list of membersで指定するメンバーたちに対して、権限を付与できるやつ。

Authoritative for a given role. Updates the IAM policy to grant a role to a list of members. Other roles within the IAM policy for the project are preserved.

  • google_project_iam_member

指定するメンバーに対して、権限を付与できるやつ。

Non-authoritative. Updates the IAM policy to grant a role to a new member. Other members for the role for the project are preserved.

  • google_project_iam_audit_config

指定するメンバーに対して、ログに関する権限を付与できるやつ。

Authoritative for a given service. Updates the IAM policy to enable audit logging for the given service.

今回付与したrole

今回は、CloudRunを使用できるようにするため、run.developerというロールを付与した。

roles/run.developer

すべてのCloud Runリソースへの読み取りおよび書き込みアクセス権を付与するもの。

Read and write access to all Cloud Run resources.
Lowest-level resources where you can grant this role:

conditionでは条件が指定できるみたい

  condition {
    title       = "expires_after_2023-10-31"
    description = "Expiring at midnight of 2023-10-31"
    expression  = "request.time < timestamp(\"2023-11-01T00:00:00Z\")"
  }
  • condition: 任意で設定できるもの
    • title: 任意でつけておくタイトル(式のタイトル)
    • description: 任意でつけておく説明文(式の説明を記述するもの)
    • expression: CELによる条件式

CELは、Common Expression LanguageというGoogleが開発した言語らしい。

最後に

色々と調べてみると、この10行くらいのコードもやっと理解できました・・・。

ただ、知らない状態で見ると、resourceには何を指定したらいいの?ってなるし、roleってadmin, userの2つくらいじゃないの?みたいな疑問も浮かんできたり。

Googleのサービスは奥が深いなぁと感じました。

次は、google_project_iam_member以外のも使ってみたいな。

終わり。

参考

29
10
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
29
10