Manage GCP API Gateway IAM Policies

The gcp:apigateway/apiIamPolicy:ApiIamPolicy resource, part of the Pulumi GCP provider, manages IAM permissions for API Gateway APIs, controlling who can view, edit, or invoke the API. This guide focuses on three approaches: authoritative policy replacement (ApiIamPolicy), role-level member management (ApiIamBinding), and individual member grants (ApiIamMember).

These resources reference an existing API Gateway API. ApiIamPolicy replaces the entire policy and cannot be used with ApiIamBinding or ApiIamMember. ApiIamBinding and ApiIamMember can coexist if they manage different roles. The examples are intentionally small. Combine them with your own API Gateway APIs and organizational access patterns.

Replace the entire IAM policy for an API

When you need complete control over API 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/apigateway.viewer",
        members: ["user:jane@example.com"],
    }],
});
const policy = new gcp.apigateway.ApiIamPolicy("policy", {
    project: api.project,
    api: api.apiId,
    policyData: admin.then(admin => admin.policyData),
});
import pulumi
import pulumi_gcp as gcp

admin = gcp.organizations.get_iam_policy(bindings=[{
    "role": "roles/apigateway.viewer",
    "members": ["user:jane@example.com"],
}])
policy = gcp.apigateway.ApiIamPolicy("policy",
    project=api["project"],
    api=api["apiId"],
    policy_data=admin.policy_data)
package main

import (
	"github.com/pulumi/pulumi-gcp/sdk/v9/go/gcp/apigateway"
	"github.com/pulumi/pulumi-gcp/sdk/v9/go/gcp/organizations"
	"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/apigateway.viewer",
					Members: []string{
						"user:jane@example.com",
					},
				},
			},
		}, nil)
		if err != nil {
			return err
		}
		_, err = apigateway.NewApiIamPolicy(ctx, "policy", &apigateway.ApiIamPolicyArgs{
			Project:    pulumi.Any(api.Project),
			Api:        pulumi.Any(api.ApiId),
			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/apigateway.viewer",
                Members = new[]
                {
                    "user:jane@example.com",
                },
            },
        },
    });

    var policy = new Gcp.ApiGateway.ApiIamPolicy("policy", new()
    {
        Project = api.Project,
        Api = api.ApiId,
        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.apigateway.ApiIamPolicy;
import com.pulumi.gcp.apigateway.ApiIamPolicyArgs;
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/apigateway.viewer")
                .members("user:jane@example.com")
                .build())
            .build());

        var policy = new ApiIamPolicy("policy", ApiIamPolicyArgs.builder()
            .project(api.project())
            .api(api.apiId())
            .policyData(admin.policyData())
            .build());

    }
}
resources:
  policy:
    type: gcp:apigateway:ApiIamPolicy
    properties:
      project: ${api.project}
      api: ${api.apiId}
      policyData: ${admin.policyData}
variables:
  admin:
    fn::invoke:
      function: gcp:organizations:getIAMPolicy
      arguments:
        bindings:
          - role: roles/apigateway.viewer
            members:
              - user:jane@example.com

The ApiIamPolicy resource is authoritative: it replaces all IAM bindings on the API. The policyData property accepts output from getIAMPolicy, which defines roles and members in a structured format. This approach ensures the API has exactly the permissions you specify, removing any bindings not declared in the policy.

Grant a role to multiple members

Teams often need to assign the same role to several users or service accounts without managing the full policy document.

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

const binding = new gcp.apigateway.ApiIamBinding("binding", {
    project: api.project,
    api: api.apiId,
    role: "roles/apigateway.viewer",
    members: ["user:jane@example.com"],
});
import pulumi
import pulumi_gcp as gcp

binding = gcp.apigateway.ApiIamBinding("binding",
    project=api["project"],
    api=api["apiId"],
    role="roles/apigateway.viewer",
    members=["user:jane@example.com"])
package main

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

func main() {
	pulumi.Run(func(ctx *pulumi.Context) error {
		_, err := apigateway.NewApiIamBinding(ctx, "binding", &apigateway.ApiIamBindingArgs{
			Project: pulumi.Any(api.Project),
			Api:     pulumi.Any(api.ApiId),
			Role:    pulumi.String("roles/apigateway.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.ApiGateway.ApiIamBinding("binding", new()
    {
        Project = api.Project,
        Api = api.ApiId,
        Role = "roles/apigateway.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.apigateway.ApiIamBinding;
import com.pulumi.gcp.apigateway.ApiIamBindingArgs;
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 ApiIamBinding("binding", ApiIamBindingArgs.builder()
            .project(api.project())
            .api(api.apiId())
            .role("roles/apigateway.viewer")
            .members("user:jane@example.com")
            .build());

    }
}
resources:
  binding:
    type: gcp:apigateway:ApiIamBinding
    properties:
      project: ${api.project}
      api: ${api.apiId}
      role: roles/apigateway.viewer
      members:
        - user:jane@example.com

The ApiIamBinding resource is authoritative for a single role: it replaces all members assigned to that role while preserving other roles on the API. The members property accepts a list of identities (users, service accounts, groups). This approach is useful when you manage one role’s membership as a unit.

Add a single member to a role

When you need to grant access to one additional user without affecting other members of the same role, a non-authoritative approach preserves existing permissions.

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

const member = new gcp.apigateway.ApiIamMember("member", {
    project: api.project,
    api: api.apiId,
    role: "roles/apigateway.viewer",
    member: "user:jane@example.com",
});
import pulumi
import pulumi_gcp as gcp

member = gcp.apigateway.ApiIamMember("member",
    project=api["project"],
    api=api["apiId"],
    role="roles/apigateway.viewer",
    member="user:jane@example.com")
package main

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

func main() {
	pulumi.Run(func(ctx *pulumi.Context) error {
		_, err := apigateway.NewApiIamMember(ctx, "member", &apigateway.ApiIamMemberArgs{
			Project: pulumi.Any(api.Project),
			Api:     pulumi.Any(api.ApiId),
			Role:    pulumi.String("roles/apigateway.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.ApiGateway.ApiIamMember("member", new()
    {
        Project = api.Project,
        Api = api.ApiId,
        Role = "roles/apigateway.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.apigateway.ApiIamMember;
import com.pulumi.gcp.apigateway.ApiIamMemberArgs;
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 ApiIamMember("member", ApiIamMemberArgs.builder()
            .project(api.project())
            .api(api.apiId())
            .role("roles/apigateway.viewer")
            .member("user:jane@example.com")
            .build());

    }
}
resources:
  member:
    type: gcp:apigateway:ApiIamMember
    properties:
      project: ${api.project}
      api: ${api.apiId}
      role: roles/apigateway.viewer
      member: user:jane@example.com

The ApiIamMember resource is non-authoritative: it adds one member to a role without removing others. The member property accepts a single identity. This approach is useful when different teams or modules manage access for different users, avoiding conflicts over who controls the role’s membership.

Beyond these examples

These snippets focus on specific IAM management approaches: authoritative vs non-authoritative IAM management and role-based access control for API Gateway APIs. They’re intentionally minimal rather than full access control systems.

The examples reference pre-existing infrastructure such as the API Gateway API resource (via api.apiId and api.project). They focus on configuring IAM permissions rather than provisioning the API itself.

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

  • Conditional IAM bindings (condition blocks)
  • Custom role definitions
  • Service account creation and management
  • 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 API Gateway ApiIamPolicy resource reference for all available configuration options.

Let's manage GCP API Gateway 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 ApiIamPolicy, ApiIamBinding, and ApiIamMember?
ApiIamPolicy is authoritative and replaces the entire IAM policy. ApiIamBinding is authoritative for a specific role, updating the member list for that role while preserving other roles. ApiIamMember is non-authoritative, adding a single member to a role while preserving other members.
Can I use ApiIamPolicy together with ApiIamBinding or ApiIamMember?
No, ApiIamPolicy cannot be used with ApiIamBinding or ApiIamMember because they will conflict over the policy configuration.
Can I use ApiIamBinding and ApiIamMember together?
Yes, but only if they don’t grant privileges to the same role. Each role must be managed by only one resource type.
What does 'authoritative' mean for IAM resources?
Authoritative resources replace existing configuration. ApiIamPolicy replaces the entire policy, while ApiIamBinding replaces all members for a specific role. Non-authoritative ApiIamMember adds members without removing existing ones.
Configuration & Immutability
What properties can't I change after creating an ApiIamPolicy?
The api and project properties are immutable and cannot be changed after creation.
How do I generate the policyData for ApiIamPolicy?
Use the gcp.organizations.getIAMPolicy data source to generate the policyData value, as shown in the examples.
Beta Status & Stability
Is this resource production-ready?
This resource is in beta and requires the terraform-provider-google-beta provider. Be aware of potential breaking changes in beta resources.

Using a different cloud?

Explore security guides for other cloud providers: