Manage GCP Service Directory IAM Policies

The gcp:servicedirectory/serviceIamPolicy:ServiceIamPolicy resource, part of the Pulumi GCP provider, manages IAM policies for Service Directory services, controlling who can access and manage service registry entries. This guide focuses on three approaches: authoritative policy replacement, role-level member binding, and incremental member addition.

Service Directory offers three IAM resources with different update behaviors. ServiceIamPolicy replaces the entire policy, ServiceIamBinding manages all members for a specific role, and ServiceIamMember adds individual members without affecting others. The examples are intentionally small. Combine them with your own Service Directory namespaces and services.

Replace the entire IAM policy for a service

When you need complete control over access, you can set the entire IAM policy at once, replacing any existing permissions.

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

const admin = gcp.organizations.getIAMPolicy({
    bindings: [{
        role: "roles/viewer",
        members: ["user:jane@example.com"],
    }],
});
const policy = new gcp.servicedirectory.ServiceIamPolicy("policy", {
    name: example.name,
    policyData: admin.then(admin => admin.policyData),
});
import pulumi
import pulumi_gcp as gcp

admin = gcp.organizations.get_iam_policy(bindings=[{
    "role": "roles/viewer",
    "members": ["user:jane@example.com"],
}])
policy = gcp.servicedirectory.ServiceIamPolicy("policy",
    name=example["name"],
    policy_data=admin.policy_data)
package main

import (
	"github.com/pulumi/pulumi-gcp/sdk/v9/go/gcp/organizations"
	"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 {
		admin, err := organizations.LookupIAMPolicy(ctx, &organizations.LookupIAMPolicyArgs{
			Bindings: []organizations.GetIAMPolicyBinding{
				{
					Role: "roles/viewer",
					Members: []string{
						"user:jane@example.com",
					},
				},
			},
		}, nil)
		if err != nil {
			return err
		}
		_, err = servicedirectory.NewServiceIamPolicy(ctx, "policy", &servicedirectory.ServiceIamPolicyArgs{
			Name:       pulumi.Any(example.Name),
			PolicyData: pulumi.String(admin.PolicyData),
		})
		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 admin = Gcp.Organizations.GetIAMPolicy.Invoke(new()
    {
        Bindings = new[]
        {
            new Gcp.Organizations.Inputs.GetIAMPolicyBindingInputArgs
            {
                Role = "roles/viewer",
                Members = new[]
                {
                    "user:jane@example.com",
                },
            },
        },
    });

    var policy = new Gcp.ServiceDirectory.ServiceIamPolicy("policy", new()
    {
        Name = example.Name,
        PolicyData = admin.Apply(getIAMPolicyResult => getIAMPolicyResult.PolicyData),
    });

});
package generated_program;

import com.pulumi.Context;
import com.pulumi.Pulumi;
import com.pulumi.core.Output;
import com.pulumi.gcp.organizations.OrganizationsFunctions;
import com.pulumi.gcp.organizations.inputs.GetIAMPolicyArgs;
import com.pulumi.gcp.servicedirectory.ServiceIamPolicy;
import com.pulumi.gcp.servicedirectory.ServiceIamPolicyArgs;
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) {
        final var admin = OrganizationsFunctions.getIAMPolicy(GetIAMPolicyArgs.builder()
            .bindings(GetIAMPolicyBindingArgs.builder()
                .role("roles/viewer")
                .members("user:jane@example.com")
                .build())
            .build());

        var policy = new ServiceIamPolicy("policy", ServiceIamPolicyArgs.builder()
            .name(example.name())
            .policyData(admin.policyData())
            .build());

    }
}
resources:
  policy:
    type: gcp:servicedirectory:ServiceIamPolicy
    properties:
      name: ${example.name}
      policyData: ${admin.policyData}
variables:
  admin:
    fn::invoke:
      function: gcp:organizations:getIAMPolicy
      arguments:
        bindings:
          - role: roles/viewer
            members:
              - user:jane@example.com

The ServiceIamPolicy resource takes policy data from the getIAMPolicy data source and applies it authoritatively to the service. This replaces all existing IAM bindings, giving you a clean slate. The policyData property contains the complete policy definition, including all roles and members.

Grant a role to multiple members at once

Teams often need to assign the same role to several users simultaneously while preserving other role assignments.

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

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

binding = gcp.servicedirectory.ServiceIamBinding("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.NewServiceIamBinding(ctx, "binding", &servicedirectory.ServiceIamBindingArgs{
			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.ServiceIamBinding("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.ServiceIamBinding;
import com.pulumi.gcp.servicedirectory.ServiceIamBindingArgs;
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 ServiceIamBinding("binding", ServiceIamBindingArgs.builder()
            .name(example.name())
            .role("roles/viewer")
            .members("user:jane@example.com")
            .build());

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

The ServiceIamBinding resource manages all members for a specific role. The members array lists everyone who should have this role. This approach is authoritative for the specified role but preserves other roles on the service. If you later remove a member from the array, they lose access.

Add a single member to a role incrementally

When you need to grant access to one user without disturbing existing permissions, you can add members individually.

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

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

member = gcp.servicedirectory.ServiceIamMember("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.NewServiceIamMember(ctx, "member", &servicedirectory.ServiceIamMemberArgs{
			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.ServiceIamMember("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.ServiceIamMember;
import com.pulumi.gcp.servicedirectory.ServiceIamMemberArgs;
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 ServiceIamMember("member", ServiceIamMemberArgs.builder()
            .name(example.name())
            .role("roles/viewer")
            .member("user:jane@example.com")
            .build());

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

The ServiceIamMember resource adds one member to a role non-authoritatively. Other members with the same role remain unchanged. This is useful when multiple teams manage access independently or when you’re adding permissions incrementally.

Beyond these examples

These snippets focus on specific IAM management approaches: authoritative vs non-authoritative updates, and policy-level, role-level, and member-level access control. They’re intentionally minimal rather than full access management solutions.

The examples reference pre-existing infrastructure such as Service Directory services (referenced by name). They focus on configuring IAM policies rather than provisioning the services themselves.

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

  • Conditional IAM bindings (condition blocks)
  • Custom role definitions
  • Service account impersonation
  • Audit logging configuration

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

Let's manage GCP Service Directory IAM Policies

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 & Compatibility
What's the difference between ServiceIamPolicy, ServiceIamBinding, and ServiceIamMember?
ServiceIamPolicy is authoritative and replaces the entire IAM policy. ServiceIamBinding is authoritative for a specific role, preserving other roles. ServiceIamMember is non-authoritative, adding a single member while preserving other members for that role.
Can I use ServiceIamPolicy with ServiceIamBinding or ServiceIamMember?
No, ServiceIamPolicy cannot be used alongside ServiceIamBinding or ServiceIamMember because they will conflict over policy control.
Can I use ServiceIamBinding and ServiceIamMember together?
Yes, but only if they manage different roles. If both resources grant privileges to the same role, they will conflict.
What happens if I mix incompatible IAM resources?
Mixing ServiceIamPolicy with ServiceIamBinding or ServiceIamMember causes them to “fight over what your policy should be,” resulting in unpredictable policy states.
Configuration & Import
Can I change the name after creating the resource?
No, the name property is immutable and cannot be changed after creation.
Where does policyData come from?
The policyData property must be generated by the gcp.organizations.getIAMPolicy data source, as shown in the examples.
How do I import a resource with a custom role?
Use the full custom role name in the format projects/my-project/roles/my-custom-role or organizations/my-org/roles/my-custom-role.

Using a different cloud?

Explore security guides for other cloud providers: