简体   繁体   中英

How to create resource for not for every module in terraform?

So I've got a common module that has:

module "foo1" {
  source            = "./modules/bar"
  name              = "foo1"
  id                = "123"
}

module "foo2" {
  source            = "./modules/bar"
  name              = "foo2"
  id                = "456"
}
// module bar
resource "google_service_account" "service_account" {
  account_id = var.name
  ...
}

that is being referenced by 2 other modules as you can see above. The thing is this module bar generates a resource with an email: google_service_account.service_account.email that I'd like to use to create a bunch of other resources but I only want to create them for foo1 and not for foo2 ( bar module uses both name and id vars to generate the email). How can I exclude foo2 from creating resource from?

// needs to be created using the email generated for `foo1` only
resource "google_project_iam_member" "admin" {
  ...
  member = "serviceAccount:${google_service_account.service_account.email}"
}

If I understand correctly your question, you could use count to conditionally create your project:

resource "google_project_iam_member" "admin" {
 
  count = var.name == "foo1" ? 1 : 0

  ...
  member = "serviceAccount:${google_service_account.service_account.email}"
}

The above will create google_project_iam_member.admin when name is foo1 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM