클라우드/GCP 자원 테라폼으로 관리해보기

(3-1) GCP IAM Policy 관리 - custom role을 구성하는 모듈

Fanic 2024. 6. 9. 01:44
반응형

IAM Policy를 테라폼으로 관리하는 방법에 대해 기술하려고 한다.

 

IAM 관련해서는 두 개의 모듈을 만들어 관리하였다.

(1) custom role을 구성하는 모듈

(2) serviceAccount, group, user를 role에 연결해주는 모듈

 

이 페이지에서는 custom role을 구성하는 모듈에 대해 설명한다.

 

1.custom role을 구성하는 모듈 

project_id를 지정하여 custom role이 등록될 프로젝트를 선택한다.

role_id가 실제 사용하는 role 이름이다.

permissions에 부여할 권한을 지정한다.

 

module "custom_role" {
  source = "./modules/iam_custom_role"

  project_id  = "pjt-an3-dev-vm-2"

  #role_id 의 경우 영어,숫자,_ 만 사용 가능
  role_id     = "test_iam"
  title       = "Custom Role Title"
  description = "Description of the custom role."

  permissions = [
    "iam.roles.get",
    "iam.roles.list",
    "iam.roles.create",
    "iam.roles.delete",
    "iam.roles.update",
  ]

  stage = "GA"
}



module "artifactregistry_viewer" {
  source = "./modules/iam_custom_role"

  project_id  = "pjt-an3-dev-vm-2"

  #role_id 의 경우 영어,숫자,_ 만 사용 가능
  role_id     = "artifactregistry_viewer"
  title       = "artifactregistry_viewer"
  description = "Description of the custom role."

  permissions = [
    "artifactregistry.repositories.list",
    "artifactregistry.repositories.get",
    "artifactregistry.projectsettings.update",
    "storage.buckets.update",
    "artifactregistry.repositories.downloadArtifacts",
  ]

  stage = "GA"
}

 

iam_custom_role 모듈 정보

 

1) main.tf

모듈로 전달 받은 리소스를 생성한다.

resource "google_project_iam_custom_role" "custom_role" {

  project     = var.project_id
  role_id     = var.role_id
  title       = var.title
  description = var.description
  permissions = var.permissions
  stage       = var.stage
}

 

2)output

serviceAccount, group, user를 role에 연결해주는 모듈에서 커스텀 롤을 연결하기 위해 output을 추가했다.

output "google_project_iam_custom_role_id" {
  description = "ID of the created google_compute_network id"
  value       = "projects/${var.project_id}/roles/${google_project_iam_custom_role.custom_role.role_id}"
}

 

3)variables.tf

permissions 는 리스트 형식으로 값이 넘어오기 때문에 해당 타입으로 지정했다.

variable "project_id" {
  description = "프로젝트 ID"
  type        = string
}




variable "role_id" {
  type        = string
  description = "The ID of the custom role."
}

variable "title" {
  type        = string
  description = "The title of the custom role."
}

variable "description" {
  type        = string
  description = "The description of the custom role."
}

variable "permissions" {
  type        = list(string)
  description = "The permissions assigned to the custom role."
}

variable "stage" {
  type        = string
  description = "The stage of the custom role."
}

 

 

 

2. GCP 콘솔에서 생성된 Role 확인

 

1) Custom Role Title

 

2) artifactregistry_viewer

반응형