<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Adrian Jade Manaday</title>
    <description>The latest articles on DEV Community by Adrian Jade Manaday (@adrianjadev).</description>
    <link>https://dev.to/adrianjadev</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4286147%2F869f57eb-382a-4723-a96f-5f1293bdfa1b.jpeg</url>
      <title>DEV Community: Adrian Jade Manaday</title>
      <link>https://dev.to/adrianjadev</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/adrianjadev"/>
    <language>en</language>
    <item>
      <title>Enforcing Least-Privilege GCP IAM Custom Roles with Terraform</title>
      <dc:creator>Adrian Jade Manaday</dc:creator>
      <pubDate>Sun, 06 Dec 2026 10:41:46 +0000</pubDate>
      <link>https://dev.to/adrianjadev/enforcing-least-privilege-gcp-iam-custom-roles-with-terraform-47gl</link>
      <guid>https://dev.to/adrianjadev/enforcing-least-privilege-gcp-iam-custom-roles-with-terraform-47gl</guid>
      <description>&lt;p&gt;Granting third-party auditors access to cloud infrastructure before a production cutover is one of the easiest ways to introduce lingering over-privileged identities. A quick click-ops assignment of a basic role (&lt;code&gt;roles/viewer&lt;/code&gt;) or even a service-specific predefined role often grants far more visibility than the audit actually requires—and leaves no version-controlled trail behind.&lt;/p&gt;

&lt;p&gt;In this write-up, I walk through how I replaced broad predefined GCP IAM access with a declarative, least-privilege &lt;strong&gt;Custom IAM Role&lt;/strong&gt; provisioned in Terraform and verified via &lt;strong&gt;Cloud Asset Inventory (Policy Analyzer)&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  TL;DR
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;What I built:&lt;/strong&gt; A reusable Terraform configuration that creates a production-grade (&lt;code&gt;GA&lt;/code&gt;) GCP Custom IAM Role (&lt;code&gt;Audit Team Reviewer&lt;/code&gt;) scoped to exactly two permissions and binds it non-authoritatively to an external audit principal.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Core security control:&lt;/strong&gt; Decomposing &lt;code&gt;roles/firebasedatabase.viewer&lt;/code&gt; down to two control-plane listing permissions (&lt;code&gt;firebase.clients.list&lt;/code&gt; and &lt;code&gt;firebasedatabase.instances.list&lt;/code&gt;) using &lt;code&gt;google_project_iam_member&lt;/code&gt; to avoid IAM policy lockouts.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Key takeaway:&lt;/strong&gt; Managing GCP IAM safely in Terraform isn't just about picking the right permissions—it requires understanding ETag concurrency, authoritative vs. non-authoritative bindings, and GCP's custom role soft-delete behavior.&lt;/li&gt;
&lt;/ul&gt;


&lt;div class="ltag-github-readme-tag"&gt;
  &lt;div class="readme-overview"&gt;
    &lt;h2&gt;
      &lt;img src="https://assets.dev.to/assets/github-logo-5a155e1f9a670af7944dd5e12375bc76ed542ea80224905ecaf878b9157cdefc.svg" alt="GitHub logo"&gt;
      &lt;a href="https://github.com/adrianjadev" rel="noopener noreferrer"&gt;
        adrianjadev
      &lt;/a&gt; / &lt;a href="https://github.com/adrianjadev/gcp-iam-custom-role-audit" rel="noopener noreferrer"&gt;
        gcp-iam-custom-role-audit
      &lt;/a&gt;
    &lt;/h2&gt;
    &lt;h3&gt;
      Least-privilege GCP IAM engineering with Terraform-creating, binding, and auditing custom IAM roles to eliminate overly permissive access.
    &lt;/h3&gt;
  &lt;/div&gt;
  &lt;div class="ltag-github-body"&gt;
    
&lt;div id="readme" class="md"&gt;&lt;div class="markdown-heading"&gt;
&lt;h1 class="heading-element"&gt;Least-Privilege Custom IAM Role for Third-Party Database Audits (GCP + Terraform)&lt;/h1&gt;
&lt;/div&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Origin &amp;amp; Scenario:&lt;/strong&gt; Declarative Infrastructure-as-Code (IaC) implementation of the Google Cloud Cybersecurity lab &lt;strong&gt;"Create a role in Google Cloud IAM"&lt;/strong&gt; (&lt;em&gt;Cymbal Bank&lt;/em&gt; cloud migration scenario).&lt;/p&gt;
&lt;/blockquote&gt;
&lt;div class="markdown-heading"&gt;
&lt;h2 class="heading-element"&gt;Overview&lt;/h2&gt;
&lt;/div&gt;
&lt;p&gt;During Cymbal Bank's cloud migration, a database storing sensitive customer billing and invoice data requires a third-party security audit before production deployment. Rather than assigning broad basic roles (&lt;code&gt;Viewer&lt;/code&gt;, &lt;code&gt;Editor&lt;/code&gt;, &lt;code&gt;Owner&lt;/code&gt;) or over-scoped predefined roles, this repository provisions a &lt;strong&gt;least-privilege Custom IAM Role&lt;/strong&gt; (&lt;code&gt;Audit Team Reviewer&lt;/code&gt;) in Google Cloud using Terraform, binds it non-authoritatively to an audit principal, and verifies effective permissions using &lt;strong&gt;Cloud Asset Inventory / Policy Analyzer&lt;/strong&gt;.&lt;/p&gt;

&lt;div class="markdown-heading"&gt;
&lt;h2 class="heading-element"&gt;Architecture &amp;amp; Workflow&lt;/h2&gt;
&lt;/div&gt;
&lt;div class="markdown-heading"&gt;
&lt;h3 class="heading-element"&gt;1. IAM Access Control Model&lt;/h3&gt;

&lt;/div&gt;
&lt;p&gt;In Google Cloud, permissions are never attached directly to identities. Instead, granular permissions are bundled into a &lt;strong&gt;Custom Role&lt;/strong&gt;, and a project-level &lt;strong&gt;IAM Policy Binding&lt;/strong&gt; associates a…&lt;/p&gt;&lt;/div&gt;
  &lt;/div&gt;
  &lt;div class="gh-btn-container"&gt;&lt;a class="gh-btn" href="https://github.com/adrianjadev/gcp-iam-custom-role-audit" rel="noopener noreferrer"&gt;View on GitHub&lt;/a&gt;&lt;/div&gt;
&lt;/div&gt;


&lt;p&gt;👉 &lt;strong&gt;Full repository:&lt;/strong&gt; &lt;a href="https://github.com/adrianjadev/gcp-iam-custom-role-audit" rel="noopener noreferrer"&gt;github.com/adrianjadev/gcp-iam-custom-role-audit&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  1. The Problem: Why This Matters in Production
&lt;/h2&gt;

&lt;p&gt;Consider a common pre-production cloud migration scenario: a financial services platform is preparing to launch a database storing sensitive customer billing and invoice data, and an external security team needs read-only inventory access to review the Firebase Realtime Database deployment before go-live.&lt;/p&gt;

&lt;p&gt;In practice, teams frequently run into three security risks when provisioning this access:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Over-scoped roles:&lt;/strong&gt; Assigning project-wide basic roles (&lt;code&gt;Viewer&lt;/code&gt;, &lt;code&gt;Editor&lt;/code&gt;, &lt;code&gt;Owner&lt;/code&gt;)—or even the full &lt;code&gt;Firebase Realtime Database Viewer&lt;/code&gt; predefined role—violates least privilege and expands the blast radius if an auditor's credentials are compromised.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Dangerous Terraform IAM mutations:&lt;/strong&gt; Using authoritative Terraform IAM resources (&lt;code&gt;google_project_iam_policy&lt;/code&gt; or &lt;code&gt;google_project_iam_binding&lt;/code&gt;) can silently wipe out unrelated human or service-account permissions on the project, causing accidental lockouts or production outages.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Orphaned static access:&lt;/strong&gt; Manually attaching permissions to an individual email address in the Cloud Console creates drift and makes it easy to forget to revoke access once the audit engagement ends.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Engineering objective:&lt;/strong&gt; Provision the minimum required permissions declaratively in Terraform, bind them safely without disturbing existing project IAM policies, and verify effective permissions programmatically through Cloud Asset Inventory.&lt;/p&gt;




&lt;h2&gt;
  
  
  2. Architecture &amp;amp; Design
&lt;/h2&gt;

&lt;p&gt;In Google Cloud IAM, permissions are never attached directly to identities. Instead, individual &lt;code&gt;&amp;lt;service&amp;gt;.&amp;lt;resource&amp;gt;.&amp;lt;verb&amp;gt;&lt;/code&gt; permissions are grouped into a &lt;strong&gt;Role&lt;/strong&gt;, and a project-level &lt;strong&gt;IAM Policy Binding&lt;/strong&gt; connects a &lt;strong&gt;Principal&lt;/strong&gt; to that role.&lt;/p&gt;

&lt;p&gt;When &lt;code&gt;terraform apply&lt;/code&gt; runs, Terraform orchestrates two distinct GCP APIs before verification occurs in Cloud Asset Inventory:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Cloud IAM API (&lt;code&gt;iam.googleapis.com&lt;/code&gt;):&lt;/strong&gt; Validates the requested permissions and creates the project-scoped custom role (&lt;code&gt;projects/{project_id}/roles/CustomRole&lt;/code&gt;) with launch stage &lt;code&gt;GA&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Cloud Resource Manager API (&lt;code&gt;cloudresourcemanager.googleapis.com&lt;/code&gt;):&lt;/strong&gt; Updates the project's single IAM allow-policy document using an optimistic-concurrency &lt;strong&gt;get-modify-set&lt;/strong&gt; operation backed by the policy's &lt;code&gt;etag&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Cloud Asset Inventory (&lt;code&gt;cloudasset.googleapis.com&lt;/code&gt;):&lt;/strong&gt; Policy Analyzer evaluates effective permissions across the resource hierarchy—resolving custom role definitions rather than just inspecting raw JSON bindings.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;![Provisioning and Verification Lifecycle]&lt;/p&gt;

&lt;h3&gt;
  
  
  Component Breakdown
&lt;/h3&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Component&lt;/th&gt;
&lt;th&gt;Cloud Service / Tool&lt;/th&gt;
&lt;th&gt;Security Purpose&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Custom IAM Role (&lt;code&gt;Audit Team Reviewer&lt;/code&gt;)&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;GCP Cloud IAM (&lt;code&gt;google_project_iam_custom_role&lt;/code&gt;)&lt;/td&gt;
&lt;td&gt;Restricts access exclusively to &lt;code&gt;firebase.clients.list&lt;/code&gt; and &lt;code&gt;firebasedatabase.instances.list&lt;/code&gt; (implicit deny on everything else).&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Project IAM Binding&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Cloud Resource Manager (&lt;code&gt;google_project_iam_member&lt;/code&gt;)&lt;/td&gt;
&lt;td&gt;Non-authoritatively binds the audit principal to the custom role without overwriting existing project IAM bindings.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Input Isolation (&lt;code&gt;terraform.tfvars&lt;/code&gt;)&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Terraform (&lt;code&gt;&amp;gt;= 1.5.0&lt;/code&gt;)&lt;/td&gt;
&lt;td&gt;Decouples environment-specific &lt;code&gt;project_id&lt;/code&gt; and &lt;code&gt;auditor_member&lt;/code&gt; values from reusable HCL code (and keeps &lt;code&gt;.tfvars&lt;/code&gt; out of Git).&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Effective Access Audit&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Cloud Asset Inventory / Policy Analyzer&lt;/td&gt;
&lt;td&gt;Independently verifies which principals hold effective access to target database resources across the hierarchy.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;h2&gt;
  
  
  3. Security Controls &amp;amp; Implementation Deep Dive
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Control 1: Decomposing Predefined Roles into a &lt;code&gt;GA&lt;/code&gt; Custom Role
&lt;/h3&gt;

&lt;p&gt;Instead of assigning the entire &lt;code&gt;roles/firebasedatabase.viewer&lt;/code&gt; predefined role, the custom role strips access down to two specific control-plane inventory permissions. Because GCP IAM operates on default-deny, any action outside this explicit list is automatically rejected.&lt;/p&gt;

&lt;p&gt;Setting &lt;code&gt;stage = "GA"&lt;/code&gt; (&lt;em&gt;General Availability&lt;/em&gt;) explicitly marks the role as production-ready and stable rather than experimental (&lt;code&gt;ALPHA&lt;/code&gt;) or evolving (&lt;code&gt;BETA&lt;/code&gt;).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight hcl"&gt;&lt;code&gt;&lt;span class="n"&gt;resource&lt;/span&gt; &lt;span class="s2"&gt;"google_project_iam_custom_role"&lt;/span&gt; &lt;span class="s2"&gt;"audit_team_reviewer"&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;project&lt;/span&gt;     &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;var&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;project_id&lt;/span&gt;
  &lt;span class="n"&gt;role_id&lt;/span&gt;     &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"CustomRole"&lt;/span&gt;
  &lt;span class="n"&gt;title&lt;/span&gt;       &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"Audit Team Reviewer"&lt;/span&gt;
  &lt;span class="n"&gt;description&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"Least-privilege role for third-party Firebase Realtime Database inventory audits"&lt;/span&gt;
  &lt;span class="n"&gt;stage&lt;/span&gt;       &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"GA"&lt;/span&gt;

  &lt;span class="n"&gt;permissions&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="s2"&gt;"firebase.clients.list"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s2"&gt;"firebasedatabase.instances.list"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Control 2: Non-Authoritative Bindings (&lt;code&gt;google_project_iam_member&lt;/code&gt;) &amp;amp; ETag Concurrency
&lt;/h3&gt;

&lt;p&gt;Choosing the right Terraform IAM resource is a critical safety decision in GCP:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;google_project_iam_policy&lt;/code&gt; is &lt;strong&gt;authoritative for the entire project&lt;/strong&gt;—it replaces &lt;em&gt;every&lt;/em&gt; IAM binding on the project (high lockout risk).&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;google_project_iam_binding&lt;/code&gt; is &lt;strong&gt;authoritative per role&lt;/strong&gt;—it removes any principal on that role not declared in the resource block.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;google_project_iam_member&lt;/code&gt; is &lt;strong&gt;non-authoritative (additive)&lt;/strong&gt;—it performs a read-modify-write update using the policy's &lt;code&gt;etag&lt;/code&gt; and safely manages a single &lt;code&gt;(role, member)&lt;/code&gt; tuple.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight hcl"&gt;&lt;code&gt;&lt;span class="n"&gt;resource&lt;/span&gt; &lt;span class="s2"&gt;"google_project_iam_member"&lt;/span&gt; &lt;span class="s2"&gt;"auditor_binding"&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;project&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;var&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;project_id&lt;/span&gt;
  &lt;span class="n"&gt;role&lt;/span&gt;    &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;google_project_iam_custom_role&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;audit_team_reviewer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;
  &lt;span class="n"&gt;member&lt;/span&gt;  &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;var&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;auditor_member&lt;/span&gt;

  &lt;span class="c1"&gt;# Optional IAM Condition pattern for time-bound third-party audits:&lt;/span&gt;
  &lt;span class="c1"&gt;# condition {&lt;/span&gt;
  &lt;span class="c1"&gt;#   title       = "Audit Window Expiration"&lt;/span&gt;
  &lt;span class="c1"&gt;#   description = "Automatically expires third-party audit access after the review window"&lt;/span&gt;
  &lt;span class="c1"&gt;#   expression  = "request.time &amp;lt; timestamp(\"2027-01-31T23:59:59Z\")"&lt;/span&gt;
  &lt;span class="c1"&gt;# }&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Control 3: Parameterizing for Group Principals &amp;amp; Ephemeral Access
&lt;/h3&gt;

&lt;p&gt;Binding IAM roles directly to individual user emails creates operational drag and stale access when personnel rotate. By externalizing &lt;code&gt;auditor_member&lt;/code&gt; into &lt;code&gt;terraform.tfvars&lt;/code&gt; (copied from &lt;code&gt;terraform.tfvars.example&lt;/code&gt;), the module supports both individual principals for testing and Google Groups (&lt;code&gt;group:external-auditors@example.com&lt;/code&gt;) for clean lifecycle management—and pairs naturally with time-bound IAM Conditions (&lt;code&gt;request.time &amp;lt; timestamp(...)&lt;/code&gt;) so access self-expires after the audit window.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight hcl"&gt;&lt;code&gt;&lt;span class="n"&gt;project_id&lt;/span&gt;     &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"your-gcp-project-id"&lt;/span&gt;
&lt;span class="n"&gt;auditor_member&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"user:auditor@example.com"&lt;/span&gt; &lt;span class="c1"&gt;# Prefer "group:..." in production&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Verification &amp;amp; Testing
&lt;/h3&gt;

&lt;p&gt;Before applying, ensure the target project has &lt;code&gt;iam.googleapis.com&lt;/code&gt;, &lt;code&gt;cloudresourcemanager.googleapis.com&lt;/code&gt;, and &lt;code&gt;cloudasset.googleapis.com&lt;/code&gt; enabled, and that the deploying identity holds &lt;code&gt;roles/iam.roleAdmin&lt;/code&gt; and &lt;code&gt;roles/resourcemanager.projectIamAdmin&lt;/code&gt;.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Provision the custom role and binding:&lt;/strong&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;   cp terraform.tfvars.example terraform.tfvars
   terraform init
   terraform plan
   terraform apply
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Verify effective permissions with Cloud Asset Inventory (Policy Analyzer):&lt;/strong&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;   gcloud asset analyze-iam-policy \
     --project=&lt;span class="s2"&gt;"your-gcp-project-id"&lt;/span&gt; \
     --identity=&lt;span class="s2"&gt;"user:auditor@example.com"&lt;/span&gt; \
     --expand-resources
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Policy Analyzer resolves the custom role definition against the resource hierarchy, confirming that &lt;code&gt;Audit Team Reviewer&lt;/code&gt; grants only the two expected listing permissions and nothing broader.&lt;/p&gt;




&lt;h2&gt;
  
  
  4. Lessons Learned &amp;amp; Production Hardening
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Key Engineering Takeaways
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;The Custom Role Soft-Delete Gotcha (&lt;code&gt;409 Already Exists&lt;/code&gt;):&lt;/strong&gt; Running &lt;code&gt;terraform destroy&lt;/code&gt; does &lt;strong&gt;not&lt;/strong&gt; immediately free a custom role's &lt;code&gt;role_id&lt;/code&gt;. GCP places deleted custom roles into a pending-deletion (soft-delete) window. Re-running &lt;code&gt;terraform apply&lt;/code&gt; right away with the same &lt;code&gt;role_id&lt;/code&gt; (&lt;code&gt;CustomRole&lt;/code&gt;) triggers a &lt;code&gt;409 Already Exists&lt;/code&gt; conflict. In ephemeral test environments, either append a randomized suffix to &lt;code&gt;role_id&lt;/code&gt; or restore the role first with:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;   gcloud iam roles undelete CustomRole --project=&lt;span class="s2"&gt;"&amp;lt;PROJECT_ID&amp;gt;"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Control-Plane vs. Data-Plane Scope:&lt;/strong&gt; &lt;code&gt;firebase.clients.list&lt;/code&gt; and &lt;code&gt;firebasedatabase.instances.list&lt;/code&gt; grant &lt;strong&gt;control-plane metadata visibility&lt;/strong&gt; (listing Firebase clients and RTDB instances), not arbitrary data-plane reads of database records (which are additionally governed by Firebase Security Rules and data-plane permissions). Clarifying whether auditors need configuration/inventory visibility vs. record-level data access prevents mid-audit friction.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;IAM Eventual Consistency:&lt;/strong&gt; Newly created custom roles and policy bindings propagate asynchronously across GCP's distributed IAM caches—sometimes taking seconds to a couple of minutes. Automated post-apply checks should always account for transient &lt;code&gt;403&lt;/code&gt; responses with retry/backoff logic.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Least Privilege Applies to Terraform Too:&lt;/strong&gt; Using &lt;code&gt;google_project_iam_member&lt;/code&gt; instead of authoritative policy blocks eliminates an entire class of self-inflicted IAM outages when multiple teams share a GCP project.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Taking This Further in Production
&lt;/h3&gt;

&lt;p&gt;This repository focuses on cleanly modeling the custom role, binding, and verification workflow. To harden this pattern for an enterprise production environment, the next steps I would implement are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Remote State with Locking:&lt;/strong&gt; Store Terraform state in a versioned, CMEK-encrypted &lt;strong&gt;Cloud Storage (GCS) backend&lt;/strong&gt; instead of local state files so IAM changes are auditable and concurrency-safe across teams.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Keyless DevSecOps CI/CD Pipeline:&lt;/strong&gt; Run &lt;code&gt;terraform fmt -check&lt;/code&gt;, &lt;code&gt;terraform validate&lt;/code&gt;, &lt;strong&gt;Checkov/Trivy&lt;/strong&gt; IaC misconfiguration scans, and &lt;strong&gt;Gitleaks&lt;/strong&gt; secret scanning in GitHub Actions—authenticating to GCP via &lt;strong&gt;Workload Identity Federation (OIDC)&lt;/strong&gt; with zero long-lived JSON service account keys.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Organization-Level Custom Roles &amp;amp; Audit Alerting:&lt;/strong&gt; Promote standardized audit roles to the GCP Organization level for reuse across projects, and route &lt;strong&gt;Cloud Audit Logs&lt;/strong&gt; (&lt;code&gt;SetIamPolicy&lt;/code&gt;, &lt;code&gt;CreateRole&lt;/code&gt;, &lt;code&gt;UpdateRole&lt;/code&gt;) to a SIEM or alerting sink to detect unexpected IAM drift or privilege escalation.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Explore the Code
&lt;/h2&gt;

&lt;p&gt;You can inspect the full Terraform configuration (&lt;code&gt;main.tf&lt;/code&gt;, &lt;code&gt;variables.tf&lt;/code&gt;, &lt;code&gt;outputs.tf&lt;/code&gt;, &lt;code&gt;versions.tf&lt;/code&gt;), prerequisites, and cleanup notes in the repository:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Repository:&lt;/strong&gt; &lt;a href="https://github.com/adrianjadev/gcp-iam-custom-role-audit" rel="noopener noreferrer"&gt;github.com/adrianjadev/gcp-iam-custom-role-audit&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;More GCP &amp;amp; Cloud Security IaC Projects:&lt;/strong&gt; &lt;a href="https://github.com/adrianjadev" rel="noopener noreferrer"&gt;github.com/adrianjadev&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;How does your team handle temporary third-party audit access in GCP—custom roles with IAM Conditions, or Privileged Access Manager (PAM)? I'd love to hear your patterns in the comments.&lt;/p&gt;

</description>
      <category>gcp</category>
      <category>terraform</category>
      <category>security</category>
      <category>iam</category>
    </item>
  </channel>
</rss>
