Configure GCP Service Directory Namespace IAM Bindings

The gcp:servicedirectory/namespaceIamBinding:NamespaceIamBinding resource, part of the Pulumi GCP provider, manages IAM role bindings for Service Directory namespaces by granting a specific role to a list of members. This guide focuses on two capabilities: granting roles to multiple members and adding individual members incrementally.

IAM bindings reference existing Service Directory namespaces and grant access to Google Cloud identities. The examples are intentionally small. Combine them with your own namespace resources and identity management workflows.

Grant a role to multiple members at once

Teams managing Service Directory namespaces often need to grant the same role to multiple users or service accounts simultaneously, such as giving viewer access to an entire team.

import * as pulumi from "@pulumi/pulumi";
import * as gcp from "@pulumi/gcp";

const binding = new gcp.servicedirectory.NamespaceIamBinding("binding", {
    name: example.name,
    role: "roles/viewer",
    members: ["user:jane@example.com"],
});
import pulumi
import pulumi_gcp as gcp

binding = gcp.servicedirectory.NamespaceIamBinding("binding",
    name=example["name"],
    role="roles/viewer",
    members=["user:jane@example.com"])
package main

import (
	"github.com/pulumi/pulumi-gcp/sdk/v9/go/gcp/servicedirectory"
	"github.com/pulumi/pulumi/sdk/v3/go/pulumi"
)

func main() {
	pulumi.Run(func(ctx *pulumi.Context) error {
		_, err := servicedirectory.NewNamespaceIamBinding(ctx, "binding", &servicedirectory.NamespaceIamBindingArgs{
			Name: pulumi.Any(example.Name),
			Role: pulumi.String("roles/viewer"),
			Members: pulumi.StringArray{
				pulumi.String("user:jane@example.com"),
			},
		})
		if err != nil {
			return err
		}
		return nil
	})
}
using System.Collections.Generic;
using System.Linq;
using Pulumi;
using Gcp = Pulumi.Gcp;

return await Deployment.RunAsync(() => 
{
    var binding = new Gcp.ServiceDirectory.NamespaceIamBinding("binding", new()
    {
        Name = example.Name,
        Role = "roles/viewer",
        Members = new[]
        {
            "user:jane@example.com",
        },
    });

});
package generated_program;

import com.pulumi.Context;
import com.pulumi.Pulumi;
import com.pulumi.core.Output;
import com.pulumi.gcp.servicedirectory.NamespaceIamBinding;
import com.pulumi.gcp.servicedirectory.NamespaceIamBindingArgs;
import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Paths;

public class App {
    public static void main(String[] args) {
        Pulumi.run(App::stack);
    }

    public static void stack(Context ctx) {
        var binding = new NamespaceIamBinding("binding", NamespaceIamBindingArgs.builder()
            .name(example.name())
            .role("roles/viewer")
            .members("user:jane@example.com")
            .build());

    }
}
resources:
  binding:
    type: gcp:servicedirectory:NamespaceIamBinding
    properties:
      name: ${example.name}
      role: roles/viewer
      members:
        - user:jane@example.com

The NamespaceIamBinding resource is authoritative for the specified role: it replaces all members for that role on the namespace. The members array accepts various identity formats including user emails, service account emails, groups, and domains. The name property references the namespace to which the binding applies.

Add a single member to a role incrementally

When onboarding individual users or service accounts, teams often need to add one member at a time without affecting existing role assignments.

import * as pulumi from "@pulumi/pulumi";
import * as gcp from "@pulumi/gcp";

const member = new gcp.servicedirectory.NamespaceIamMember("member", {
    name: example.name,
    role: "roles/viewer",
    member: "user:jane@example.com",
});
import pulumi
import pulumi_gcp as gcp

member = gcp.servicedirectory.NamespaceIamMember("member",
    name=example["name"],
    role="roles/viewer",
    member="user:jane@example.com")
package main

import (
	"github.com/pulumi/pulumi-gcp/sdk/v9/go/gcp/servicedirectory"
	"github.com/pulumi/pulumi/sdk/v3/go/pulumi"
)

func main() {
	pulumi.Run(func(ctx *pulumi.Context) error {
		_, err := servicedirectory.NewNamespaceIamMember(ctx, "member", &servicedirectory.NamespaceIamMemberArgs{
			Name:   pulumi.Any(example.Name),
			Role:   pulumi.String("roles/viewer"),
			Member: pulumi.String("user:jane@example.com"),
		})
		if err != nil {
			return err
		}
		return nil
	})
}
using System.Collections.Generic;
using System.Linq;
using Pulumi;
using Gcp = Pulumi.Gcp;

return await Deployment.RunAsync(() => 
{
    var member = new Gcp.ServiceDirectory.NamespaceIamMember("member", new()
    {
        Name = example.Name,
        Role = "roles/viewer",
        Member = "user:jane@example.com",
    });

});
package generated_program;

import com.pulumi.Context;
import com.pulumi.Pulumi;
import com.pulumi.core.Output;
import com.pulumi.gcp.servicedirectory.NamespaceIamMember;
import com.pulumi.gcp.servicedirectory.NamespaceIamMemberArgs;
import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Paths;

public class App {
    public static void main(String[] args) {
        Pulumi.run(App::stack);
    }

    public static void stack(Context ctx) {
        var member = new NamespaceIamMember("member", NamespaceIamMemberArgs.builder()
            .name(example.name())
            .role("roles/viewer")
            .member("user:jane@example.com")
            .build());

    }
}
resources:
  member:
    type: gcp:servicedirectory:NamespaceIamMember
    properties:
      name: ${example.name}
      role: roles/viewer
      member: user:jane@example.com

The NamespaceIamMember resource is non-authoritative: it adds a single member to a role without removing other members who already have that role. Use member (singular) instead of members (plural) to specify one identity. This approach works well for incremental access grants where you don’t want to manage the complete member list.

Beyond these examples

These snippets focus on specific IAM binding features: role-based access control and bulk and incremental member grants. They’re intentionally minimal rather than full access management solutions.

The examples reference pre-existing infrastructure such as Service Directory namespaces. They focus on configuring IAM bindings rather than provisioning the underlying namespace resources.

To keep things focused, common IAM patterns are omitted, including:

  • Conditional IAM bindings (condition property)
  • Full policy replacement (NamespaceIamPolicy)
  • Custom role definitions
  • Federated identity configuration

These omissions are intentional: the goal is to illustrate how each IAM binding feature is wired, not provide drop-in access control modules. See the Service Directory NamespaceIamBinding resource reference for all available configuration options.

Let's configure GCP Service Directory Namespace IAM Bindings

Get started with Pulumi Cloud, then follow our quick setup guide to deploy this infrastructure.

Try Pulumi Cloud for FREE

Frequently Asked Questions

Resource Selection & Conflicts
What's the difference between NamespaceIamPolicy, NamespaceIamBinding, and NamespaceIamMember?
NamespaceIamPolicy is authoritative and replaces the entire IAM policy. NamespaceIamBinding is authoritative for a single role, managing all members for that role while preserving other roles. NamespaceIamMember is non-authoritative, adding individual members without affecting other members or roles.
Can I use NamespaceIamPolicy with NamespaceIamBinding or NamespaceIamMember?
No, NamespaceIamPolicy cannot be used with NamespaceIamBinding or NamespaceIamMember because they will conflict over policy management.
Can I use NamespaceIamBinding and NamespaceIamMember together?
Yes, but only if they manage different roles. Using both resources for the same role will cause conflicts.
Configuration & Identity Management
What member identity formats are supported?
You can use allUsers, allAuthenticatedUsers, user:{email}, serviceAccount:{email}, group:{email}, domain:{domain}, projectOwner/Editor/Viewer:{projectid}, and federated identities like principal://iam.googleapis.com/....
How do I specify custom roles?
Custom roles must use the full path format: projects/{project}/roles/{role-name} or organizations/{org}/roles/{role-name}. This applies to both configuration and import.
Can I use multiple NamespaceIamBinding resources for the same namespace?
Yes, but each NamespaceIamBinding must manage a different role. Only one binding per role is allowed.
Immutability & Lifecycle
What properties can't I change after creation?
The name, role, and condition properties are immutable and require resource replacement if changed.
What import formats are supported?
You can import using projects/{project}/locations/{location}/namespaces/{namespace_id}, {project}/{location}/{namespace_id}, or {location}/{namespace_id}. Variables not provided are taken from the provider configuration.

Using a different cloud?

Explore security guides for other cloud providers: