Skip to content

Update main.tf#1025

Open
PaggieChen wants to merge 1 commit intoterraform-google-modules:mainfrom
PaggieChen:patch-1
Open

Update main.tf#1025
PaggieChen wants to merge 1 commit intoterraform-google-modules:mainfrom
PaggieChen:patch-1

Conversation

@PaggieChen
Copy link

fix(iam): resolve null service identity email error

Refactor service agent identity logic to use deterministic email construction.

Calculate service agent emails using ${project_number} to bypass API race conditions.

Add explicit depends_on between project services and IAM members.

Standardize Compute Engine default service account retrieval.

fix(iam): resolve null service identity email error

Refactor service agent identity logic to use deterministic email construction.

Calculate service agent emails using ${project_number} to bypass API race conditions.

Add explicit depends_on between project services and IAM members.

Standardize Compute Engine default service account retrieval.
@PaggieChen PaggieChen requested review from a team and imrannayer as code owners January 1, 2026 07:44
@google-cla
Copy link

google-cla bot commented Jan 1, 2026

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@gemini-code-assist
Copy link

Summary of Changes

Hello @PaggieChen, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request addresses a critical issue where service agent identity emails could be null due to race conditions during API calls. It refactors the core logic for constructing service agent emails to be deterministic, leveraging the project number for consistent generation. Furthermore, it introduces explicit dependencies between key resource types, such as project service enablement, service identity creation, and IAM role assignments, to guarantee that all necessary components are fully initialized before subsequent operations. This significantly enhances the stability and reliability of resource provisioning within the module.

Highlights

  • Deterministic Service Agent Email Construction: Service agent emails are now constructed deterministically using the project number, which helps to prevent 'null service identity email' errors caused by API race conditions during resource creation. This involves using a new data source for google_project to retrieve the project number.
  • Standardized Compute Engine Default Service Account Retrieval: The method for retrieving the Compute Engine default service account has been standardized and integrated into the new deterministic email construction logic.
  • Explicit Dependency Management: Explicit depends_on attributes have been added to google_project_service_identity, data.google_compute_default_service_account, and google_project_iam_member resources. This ensures that project services are enabled, service identities are created/retrieved, and IAM roles are assigned in the correct order, mitigating race conditions and improving provisioning reliability.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request refactors the service agent identity logic to use deterministic email construction, which is a good approach to avoid race conditions and issues with values not being known during Terraform's plan phase. The explicit depends_on clauses added to various resources also improve the robustness of the module by enforcing the correct order of resource creation.

However, I've found a critical issue with the implementation of the deterministic email construction. The current logic assumes a single, common format for service agent emails, which is not correct for all GCP services. This will cause the module to fail when used with services like BigQuery or Cloud Storage that use a different email format. My review includes a detailed comment and a code suggestion to address this by handling exceptions for different email formats.

Comment on lines +25 to +30
service_agent_emails = {
for i in var.activate_api_identities :
i.api => i.api == "compute.googleapis.com" ?
data.google_compute_default_service_account.default[0].email :
"service-${data.google_project.project.number}@gcp-sa-${replace(i.api, ".googleapis.com", "")}.iam.gserviceaccount.com"
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

The deterministic construction of service agent emails assumes a single format (service-...@gcp-sa-...), which is not universally true for all Google Cloud services. This will cause failures for services with different service agent email formats, such as BigQuery or Cloud Storage.

To make this more robust, you could use a map of exceptions for services that don't follow the common pattern. Here is a suggested implementation using lookup to handle these exceptions. You may want to expand this map with other known exceptions.

  service_agent_emails = {
    for i in var.activate_api_identities :
    i.api => i.api == "compute.googleapis.com" ?
      data.google_compute_default_service_account.default[0].email :
      lookup(
        {
          "bigquery.googleapis.com" = "bq-${data.google_project.project.number}@bigquery-iam.gserviceaccount.com",
          "storage.googleapis.com"  = "service-${data.google_project.project.number}@gs-project-accounts.iam.gserviceaccount.com"
        },
        i.api,
        "service-${data.google_project.project.number}@gcp-sa-${replace(i.api, ".googleapis.com", "")}.iam.gserviceaccount.com"
      )
  }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant