Google Cloud (GCP) Classic

v6.39.0 published on Wednesday, Sep 28, 2022 by Pulumi

IAMBinding

When managing IAM roles, you can treat a service account either as a resource or as an identity. This resource is to add iam policy bindings to a service account resource, such as allowing the members to run operations as or modify the service account. To configure permissions for a service account on other GCP resources, use the google_project_iam set of resources.

Three different resources help you manage your IAM policy for a service account. Each of these resources serves a different use case:

  • gcp.serviceAccount.IAMPolicy: Authoritative. Sets the IAM policy for the service account and replaces any existing policy already attached.
  • gcp.serviceAccount.IAMBinding: Authoritative for a given role. Updates the IAM policy to grant a role to a list of members. Other roles within the IAM policy for the service account are preserved.
  • gcp.serviceAccount.IAMMember: Non-authoritative. Updates the IAM policy to grant a role to a new member. Other members for the role for the service account are preserved.

Note: gcp.serviceAccount.IAMPolicy cannot be used in conjunction with gcp.serviceAccount.IAMBinding and gcp.serviceAccount.IAMMember or they will fight over what your policy should be.

Note: gcp.serviceAccount.IAMBinding resources can be used in conjunction with gcp.serviceAccount.IAMMember resources only if they do not grant privilege to the same role.

Example Usage

Service Account IAM Policy

using System.Collections.Generic;
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/iam.serviceAccountUser",
                Members = new[]
                {
                    "user:jane@example.com",
                },
            },
        },
    });

    var sa = new Gcp.ServiceAccount.Account("sa", new()
    {
        AccountId = "my-service-account",
        DisplayName = "A service account that only Jane can interact with",
    });

    var admin_account_iam = new Gcp.ServiceAccount.IAMPolicy("admin-account-iam", new()
    {
        ServiceAccountId = sa.Name,
        PolicyData = admin.Apply(getIAMPolicyResult => getIAMPolicyResult.PolicyData),
    });

});
package main

import (
	"github.com/pulumi/pulumi-gcp/sdk/v6/go/gcp/organizations"
	"github.com/pulumi/pulumi-gcp/sdk/v6/go/gcp/serviceAccount"
	"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{
				organizations.GetIAMPolicyBinding{
					Role: "roles/iam.serviceAccountUser",
					Members: []string{
						"user:jane@example.com",
					},
				},
			},
		}, nil)
		if err != nil {
			return err
		}
		sa, err := serviceAccount.NewAccount(ctx, "sa", &serviceAccount.AccountArgs{
			AccountId:   pulumi.String("my-service-account"),
			DisplayName: pulumi.String("A service account that only Jane can interact with"),
		})
		if err != nil {
			return err
		}
		_, err = serviceAccount.NewIAMPolicy(ctx, "admin-account-iam", &serviceAccount.IAMPolicyArgs{
			ServiceAccountId: sa.Name,
			PolicyData:       pulumi.String(admin.PolicyData),
		})
		if err != nil {
			return err
		}
		return nil
	})
}
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.serviceAccount.Account;
import com.pulumi.gcp.serviceAccount.AccountArgs;
import com.pulumi.gcp.serviceAccount.IAMPolicy;
import com.pulumi.gcp.serviceAccount.IAMPolicyArgs;
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/iam.serviceAccountUser")
                .members("user:jane@example.com")
                .build())
            .build());

        var sa = new Account("sa", AccountArgs.builder()        
            .accountId("my-service-account")
            .displayName("A service account that only Jane can interact with")
            .build());

        var admin_account_iam = new IAMPolicy("admin-account-iam", IAMPolicyArgs.builder()        
            .serviceAccountId(sa.name())
            .policyData(admin.applyValue(getIAMPolicyResult -> getIAMPolicyResult.policyData()))
            .build());

    }
}
import pulumi
import pulumi_gcp as gcp

admin = gcp.organizations.get_iam_policy(bindings=[gcp.organizations.GetIAMPolicyBindingArgs(
    role="roles/iam.serviceAccountUser",
    members=["user:jane@example.com"],
)])
sa = gcp.service_account.Account("sa",
    account_id="my-service-account",
    display_name="A service account that only Jane can interact with")
admin_account_iam = gcp.service_account.IAMPolicy("admin-account-iam",
    service_account_id=sa.name,
    policy_data=admin.policy_data)
import * as pulumi from "@pulumi/pulumi";
import * as gcp from "@pulumi/gcp";

const admin = gcp.organizations.getIAMPolicy({
    bindings: [{
        role: "roles/iam.serviceAccountUser",
        members: ["user:jane@example.com"],
    }],
});
const sa = new gcp.serviceaccount.Account("sa", {
    accountId: "my-service-account",
    displayName: "A service account that only Jane can interact with",
});
const admin_account_iam = new gcp.serviceaccount.IAMPolicy("admin-account-iam", {
    serviceAccountId: sa.name,
    policyData: admin.then(admin => admin.policyData),
});
resources:
  sa:
    type: gcp:serviceAccount:Account
    properties:
      accountId: my-service-account
      displayName: A service account that only Jane can interact with
  admin-account-iam:
    type: gcp:serviceAccount:IAMPolicy
    properties:
      serviceAccountId: ${sa.name}
      policyData: ${admin.policyData}
variables:
  admin:
    Fn::Invoke:
      Function: gcp:organizations:getIAMPolicy
      Arguments:
        bindings:
          - role: roles/iam.serviceAccountUser
            members:
              - user:jane@example.com

Service Account IAM Binging

using System.Collections.Generic;
using Pulumi;
using Gcp = Pulumi.Gcp;

return await Deployment.RunAsync(() => 
{
    var sa = new Gcp.ServiceAccount.Account("sa", new()
    {
        AccountId = "my-service-account",
        DisplayName = "A service account that only Jane can use",
    });

    var admin_account_iam = new Gcp.ServiceAccount.IAMBinding("admin-account-iam", new()
    {
        ServiceAccountId = sa.Name,
        Role = "roles/iam.serviceAccountUser",
        Members = new[]
        {
            "user:jane@example.com",
        },
    });

});
package main

import (
	"github.com/pulumi/pulumi-gcp/sdk/v6/go/gcp/serviceAccount"
	"github.com/pulumi/pulumi/sdk/v3/go/pulumi"
)

func main() {
	pulumi.Run(func(ctx *pulumi.Context) error {
		sa, err := serviceAccount.NewAccount(ctx, "sa", &serviceAccount.AccountArgs{
			AccountId:   pulumi.String("my-service-account"),
			DisplayName: pulumi.String("A service account that only Jane can use"),
		})
		if err != nil {
			return err
		}
		_, err = serviceAccount.NewIAMBinding(ctx, "admin-account-iam", &serviceAccount.IAMBindingArgs{
			ServiceAccountId: sa.Name,
			Role:             pulumi.String("roles/iam.serviceAccountUser"),
			Members: pulumi.StringArray{
				pulumi.String("user:jane@example.com"),
			},
		})
		if err != nil {
			return err
		}
		return nil
	})
}
package generated_program;

import com.pulumi.Context;
import com.pulumi.Pulumi;
import com.pulumi.core.Output;
import com.pulumi.gcp.serviceAccount.Account;
import com.pulumi.gcp.serviceAccount.AccountArgs;
import com.pulumi.gcp.serviceAccount.IAMBinding;
import com.pulumi.gcp.serviceAccount.IAMBindingArgs;
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 sa = new Account("sa", AccountArgs.builder()        
            .accountId("my-service-account")
            .displayName("A service account that only Jane can use")
            .build());

        var admin_account_iam = new IAMBinding("admin-account-iam", IAMBindingArgs.builder()        
            .serviceAccountId(sa.name())
            .role("roles/iam.serviceAccountUser")
            .members("user:jane@example.com")
            .build());

    }
}
import pulumi
import pulumi_gcp as gcp

sa = gcp.service_account.Account("sa",
    account_id="my-service-account",
    display_name="A service account that only Jane can use")
admin_account_iam = gcp.service_account.IAMBinding("admin-account-iam",
    service_account_id=sa.name,
    role="roles/iam.serviceAccountUser",
    members=["user:jane@example.com"])
import * as pulumi from "@pulumi/pulumi";
import * as gcp from "@pulumi/gcp";

const sa = new gcp.serviceaccount.Account("sa", {
    accountId: "my-service-account",
    displayName: "A service account that only Jane can use",
});
const admin_account_iam = new gcp.serviceaccount.IAMBinding("admin-account-iam", {
    serviceAccountId: sa.name,
    role: "roles/iam.serviceAccountUser",
    members: ["user:jane@example.com"],
});
resources:
  sa:
    type: gcp:serviceAccount:Account
    properties:
      accountId: my-service-account
      displayName: A service account that only Jane can use
  admin-account-iam:
    type: gcp:serviceAccount:IAMBinding
    properties:
      serviceAccountId: ${sa.name}
      role: roles/iam.serviceAccountUser
      members:
        - user:jane@example.com

Service Account IAM Binding With IAM Conditions:

using System.Collections.Generic;
using Pulumi;
using Gcp = Pulumi.Gcp;

return await Deployment.RunAsync(() => 
{
    var sa = new Gcp.ServiceAccount.Account("sa", new()
    {
        AccountId = "my-service-account",
        DisplayName = "A service account that only Jane can use",
    });

    var admin_account_iam = new Gcp.ServiceAccount.IAMBinding("admin-account-iam", new()
    {
        ServiceAccountId = sa.Name,
        Role = "roles/iam.serviceAccountUser",
        Members = new[]
        {
            "user:jane@example.com",
        },
        Condition = new Gcp.ServiceAccount.Inputs.IAMBindingConditionArgs
        {
            Title = "expires_after_2019_12_31",
            Description = "Expiring at midnight of 2019-12-31",
            Expression = "request.time < timestamp(\"2020-01-01T00:00:00Z\")",
        },
    });

});
package main

import (
	"github.com/pulumi/pulumi-gcp/sdk/v6/go/gcp/serviceAccount"
	"github.com/pulumi/pulumi/sdk/v3/go/pulumi"
)

func main() {
	pulumi.Run(func(ctx *pulumi.Context) error {
		sa, err := serviceAccount.NewAccount(ctx, "sa", &serviceAccount.AccountArgs{
			AccountId:   pulumi.String("my-service-account"),
			DisplayName: pulumi.String("A service account that only Jane can use"),
		})
		if err != nil {
			return err
		}
		_, err = serviceAccount.NewIAMBinding(ctx, "admin-account-iam", &serviceAccount.IAMBindingArgs{
			ServiceAccountId: sa.Name,
			Role:             pulumi.String("roles/iam.serviceAccountUser"),
			Members: pulumi.StringArray{
				pulumi.String("user:jane@example.com"),
			},
			Condition: &serviceaccount.IAMBindingConditionArgs{
				Title:       pulumi.String("expires_after_2019_12_31"),
				Description: pulumi.String("Expiring at midnight of 2019-12-31"),
				Expression:  pulumi.String("request.time < timestamp(\"2020-01-01T00:00:00Z\")"),
			},
		})
		if err != nil {
			return err
		}
		return nil
	})
}
package generated_program;

import com.pulumi.Context;
import com.pulumi.Pulumi;
import com.pulumi.core.Output;
import com.pulumi.gcp.serviceAccount.Account;
import com.pulumi.gcp.serviceAccount.AccountArgs;
import com.pulumi.gcp.serviceAccount.IAMBinding;
import com.pulumi.gcp.serviceAccount.IAMBindingArgs;
import com.pulumi.gcp.serviceAccount.inputs.IAMBindingConditionArgs;
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 sa = new Account("sa", AccountArgs.builder()        
            .accountId("my-service-account")
            .displayName("A service account that only Jane can use")
            .build());

        var admin_account_iam = new IAMBinding("admin-account-iam", IAMBindingArgs.builder()        
            .serviceAccountId(sa.name())
            .role("roles/iam.serviceAccountUser")
            .members("user:jane@example.com")
            .condition(IAMBindingConditionArgs.builder()
                .title("expires_after_2019_12_31")
                .description("Expiring at midnight of 2019-12-31")
                .expression("request.time < timestamp(\"2020-01-01T00:00:00Z\")")
                .build())
            .build());

    }
}
import pulumi
import pulumi_gcp as gcp

sa = gcp.service_account.Account("sa",
    account_id="my-service-account",
    display_name="A service account that only Jane can use")
admin_account_iam = gcp.service_account.IAMBinding("admin-account-iam",
    service_account_id=sa.name,
    role="roles/iam.serviceAccountUser",
    members=["user:jane@example.com"],
    condition=gcp.service_account.IAMBindingConditionArgs(
        title="expires_after_2019_12_31",
        description="Expiring at midnight of 2019-12-31",
        expression="request.time < timestamp(\"2020-01-01T00:00:00Z\")",
    ))
import * as pulumi from "@pulumi/pulumi";
import * as gcp from "@pulumi/gcp";

const sa = new gcp.serviceaccount.Account("sa", {
    accountId: "my-service-account",
    displayName: "A service account that only Jane can use",
});
const admin_account_iam = new gcp.serviceaccount.IAMBinding("admin-account-iam", {
    serviceAccountId: sa.name,
    role: "roles/iam.serviceAccountUser",
    members: ["user:jane@example.com"],
    condition: {
        title: "expires_after_2019_12_31",
        description: "Expiring at midnight of 2019-12-31",
        expression: "request.time < timestamp(\"2020-01-01T00:00:00Z\")",
    },
});
resources:
  sa:
    type: gcp:serviceAccount:Account
    properties:
      accountId: my-service-account
      displayName: A service account that only Jane can use
  admin-account-iam:
    type: gcp:serviceAccount:IAMBinding
    properties:
      serviceAccountId: ${sa.name}
      role: roles/iam.serviceAccountUser
      members:
        - user:jane@example.com
      condition:
        title: expires_after_2019_12_31
        description: Expiring at midnight of 2019-12-31
        expression: request.time < timestamp("2020-01-01T00:00:00Z")

Service Account IAM Member

using System.Collections.Generic;
using Pulumi;
using Gcp = Pulumi.Gcp;

return await Deployment.RunAsync(() => 
{
    var @default = Gcp.Compute.GetDefaultServiceAccount.Invoke();

    var sa = new Gcp.ServiceAccount.Account("sa", new()
    {
        AccountId = "my-service-account",
        DisplayName = "A service account that Jane can use",
    });

    var admin_account_iam = new Gcp.ServiceAccount.IAMMember("admin-account-iam", new()
    {
        ServiceAccountId = sa.Name,
        Role = "roles/iam.serviceAccountUser",
        Member = "user:jane@example.com",
    });

    // Allow SA service account use the default GCE account
    var gce_default_account_iam = new Gcp.ServiceAccount.IAMMember("gce-default-account-iam", new()
    {
        ServiceAccountId = @default.Apply(getDefaultServiceAccountResult => getDefaultServiceAccountResult).Apply(@default => @default.Apply(getDefaultServiceAccountResult => getDefaultServiceAccountResult.Name)),
        Role = "roles/iam.serviceAccountUser",
        Member = sa.Email.Apply(email => $"serviceAccount:{email}"),
    });

});
package main

import (
	"fmt"

	"github.com/pulumi/pulumi-gcp/sdk/v6/go/gcp/compute"
	"github.com/pulumi/pulumi-gcp/sdk/v6/go/gcp/serviceAccount"
	"github.com/pulumi/pulumi/sdk/v3/go/pulumi"
)

func main() {
	pulumi.Run(func(ctx *pulumi.Context) error {
		_default, err := compute.GetDefaultServiceAccount(ctx, nil, nil)
		if err != nil {
			return err
		}
		sa, err := serviceAccount.NewAccount(ctx, "sa", &serviceAccount.AccountArgs{
			AccountId:   pulumi.String("my-service-account"),
			DisplayName: pulumi.String("A service account that Jane can use"),
		})
		if err != nil {
			return err
		}
		_, err = serviceAccount.NewIAMMember(ctx, "admin-account-iam", &serviceAccount.IAMMemberArgs{
			ServiceAccountId: sa.Name,
			Role:             pulumi.String("roles/iam.serviceAccountUser"),
			Member:           pulumi.String("user:jane@example.com"),
		})
		if err != nil {
			return err
		}
		_, err = serviceAccount.NewIAMMember(ctx, "gce-default-account-iam", &serviceAccount.IAMMemberArgs{
			ServiceAccountId: pulumi.String(_default.Name),
			Role:             pulumi.String("roles/iam.serviceAccountUser"),
			Member: sa.Email.ApplyT(func(email string) (string, error) {
				return fmt.Sprintf("serviceAccount:%v", email), nil
			}).(pulumi.StringOutput),
		})
		if err != nil {
			return err
		}
		return nil
	})
}
package generated_program;

import com.pulumi.Context;
import com.pulumi.Pulumi;
import com.pulumi.core.Output;
import com.pulumi.gcp.compute.ComputeFunctions;
import com.pulumi.gcp.appengine.inputs.GetDefaultServiceAccountArgs;
import com.pulumi.gcp.serviceAccount.Account;
import com.pulumi.gcp.serviceAccount.AccountArgs;
import com.pulumi.gcp.serviceAccount.IAMMember;
import com.pulumi.gcp.serviceAccount.IAMMemberArgs;
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 default = ComputeFunctions.getDefaultServiceAccount();

        var sa = new Account("sa", AccountArgs.builder()        
            .accountId("my-service-account")
            .displayName("A service account that Jane can use")
            .build());

        var admin_account_iam = new IAMMember("admin-account-iam", IAMMemberArgs.builder()        
            .serviceAccountId(sa.name())
            .role("roles/iam.serviceAccountUser")
            .member("user:jane@example.com")
            .build());

        var gce_default_account_iam = new IAMMember("gce-default-account-iam", IAMMemberArgs.builder()        
            .serviceAccountId(default_.name())
            .role("roles/iam.serviceAccountUser")
            .member(sa.email().applyValue(email -> String.format("serviceAccount:%s", email)))
            .build());

    }
}
import pulumi
import pulumi_gcp as gcp

default = gcp.compute.get_default_service_account()
sa = gcp.service_account.Account("sa",
    account_id="my-service-account",
    display_name="A service account that Jane can use")
admin_account_iam = gcp.service_account.IAMMember("admin-account-iam",
    service_account_id=sa.name,
    role="roles/iam.serviceAccountUser",
    member="user:jane@example.com")
# Allow SA service account use the default GCE account
gce_default_account_iam = gcp.service_account.IAMMember("gce-default-account-iam",
    service_account_id=default.name,
    role="roles/iam.serviceAccountUser",
    member=sa.email.apply(lambda email: f"serviceAccount:{email}"))
import * as pulumi from "@pulumi/pulumi";
import * as gcp from "@pulumi/gcp";

const default = gcp.compute.getDefaultServiceAccount({});
const sa = new gcp.serviceaccount.Account("sa", {
    accountId: "my-service-account",
    displayName: "A service account that Jane can use",
});
const admin_account_iam = new gcp.serviceaccount.IAMMember("admin-account-iam", {
    serviceAccountId: sa.name,
    role: "roles/iam.serviceAccountUser",
    member: "user:jane@example.com",
});
// Allow SA service account use the default GCE account
const gce_default_account_iam = new gcp.serviceaccount.IAMMember("gce-default-account-iam", {
    serviceAccountId: _default.then(_default => _default.name),
    role: "roles/iam.serviceAccountUser",
    member: pulumi.interpolate`serviceAccount:${sa.email}`,
});
resources:
  sa:
    type: gcp:serviceAccount:Account
    properties:
      accountId: my-service-account
      displayName: A service account that Jane can use
  admin-account-iam:
    type: gcp:serviceAccount:IAMMember
    properties:
      serviceAccountId: ${sa.name}
      role: roles/iam.serviceAccountUser
      member: user:jane@example.com
  # Allow SA service account use the default GCE account
  gce-default-account-iam:
    type: gcp:serviceAccount:IAMMember
    properties:
      serviceAccountId: ${default.name}
      role: roles/iam.serviceAccountUser
      member: serviceAccount:${sa.email}
variables:
  default:
    Fn::Invoke:
      Function: gcp:compute:getDefaultServiceAccount
      Arguments: {}

Service Account IAM Member With IAM Conditions:

using System.Collections.Generic;
using Pulumi;
using Gcp = Pulumi.Gcp;

return await Deployment.RunAsync(() => 
{
    var sa = new Gcp.ServiceAccount.Account("sa", new()
    {
        AccountId = "my-service-account",
        DisplayName = "A service account that Jane can use",
    });

    var admin_account_iam = new Gcp.ServiceAccount.IAMMember("admin-account-iam", new()
    {
        ServiceAccountId = sa.Name,
        Role = "roles/iam.serviceAccountUser",
        Member = "user:jane@example.com",
        Condition = new Gcp.ServiceAccount.Inputs.IAMMemberConditionArgs
        {
            Title = "expires_after_2019_12_31",
            Description = "Expiring at midnight of 2019-12-31",
            Expression = "request.time < timestamp(\"2020-01-01T00:00:00Z\")",
        },
    });

});
package main

import (
	"github.com/pulumi/pulumi-gcp/sdk/v6/go/gcp/serviceAccount"
	"github.com/pulumi/pulumi/sdk/v3/go/pulumi"
)

func main() {
	pulumi.Run(func(ctx *pulumi.Context) error {
		sa, err := serviceAccount.NewAccount(ctx, "sa", &serviceAccount.AccountArgs{
			AccountId:   pulumi.String("my-service-account"),
			DisplayName: pulumi.String("A service account that Jane can use"),
		})
		if err != nil {
			return err
		}
		_, err = serviceAccount.NewIAMMember(ctx, "admin-account-iam", &serviceAccount.IAMMemberArgs{
			ServiceAccountId: sa.Name,
			Role:             pulumi.String("roles/iam.serviceAccountUser"),
			Member:           pulumi.String("user:jane@example.com"),
			Condition: &serviceaccount.IAMMemberConditionArgs{
				Title:       pulumi.String("expires_after_2019_12_31"),
				Description: pulumi.String("Expiring at midnight of 2019-12-31"),
				Expression:  pulumi.String("request.time < timestamp(\"2020-01-01T00:00:00Z\")"),
			},
		})
		if err != nil {
			return err
		}
		return nil
	})
}
package generated_program;

import com.pulumi.Context;
import com.pulumi.Pulumi;
import com.pulumi.core.Output;
import com.pulumi.gcp.serviceAccount.Account;
import com.pulumi.gcp.serviceAccount.AccountArgs;
import com.pulumi.gcp.serviceAccount.IAMMember;
import com.pulumi.gcp.serviceAccount.IAMMemberArgs;
import com.pulumi.gcp.serviceAccount.inputs.IAMMemberConditionArgs;
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 sa = new Account("sa", AccountArgs.builder()        
            .accountId("my-service-account")
            .displayName("A service account that Jane can use")
            .build());

        var admin_account_iam = new IAMMember("admin-account-iam", IAMMemberArgs.builder()        
            .serviceAccountId(sa.name())
            .role("roles/iam.serviceAccountUser")
            .member("user:jane@example.com")
            .condition(IAMMemberConditionArgs.builder()
                .title("expires_after_2019_12_31")
                .description("Expiring at midnight of 2019-12-31")
                .expression("request.time < timestamp(\"2020-01-01T00:00:00Z\")")
                .build())
            .build());

    }
}
import pulumi
import pulumi_gcp as gcp

sa = gcp.service_account.Account("sa",
    account_id="my-service-account",
    display_name="A service account that Jane can use")
admin_account_iam = gcp.service_account.IAMMember("admin-account-iam",
    service_account_id=sa.name,
    role="roles/iam.serviceAccountUser",
    member="user:jane@example.com",
    condition=gcp.service_account.IAMMemberConditionArgs(
        title="expires_after_2019_12_31",
        description="Expiring at midnight of 2019-12-31",
        expression="request.time < timestamp(\"2020-01-01T00:00:00Z\")",
    ))
import * as pulumi from "@pulumi/pulumi";
import * as gcp from "@pulumi/gcp";

const sa = new gcp.serviceaccount.Account("sa", {
    accountId: "my-service-account",
    displayName: "A service account that Jane can use",
});
const admin_account_iam = new gcp.serviceaccount.IAMMember("admin-account-iam", {
    serviceAccountId: sa.name,
    role: "roles/iam.serviceAccountUser",
    member: "user:jane@example.com",
    condition: {
        title: "expires_after_2019_12_31",
        description: "Expiring at midnight of 2019-12-31",
        expression: "request.time < timestamp(\"2020-01-01T00:00:00Z\")",
    },
});
resources:
  sa:
    type: gcp:serviceAccount:Account
    properties:
      accountId: my-service-account
      displayName: A service account that Jane can use
  admin-account-iam:
    type: gcp:serviceAccount:IAMMember
    properties:
      serviceAccountId: ${sa.name}
      role: roles/iam.serviceAccountUser
      member: user:jane@example.com
      condition:
        title: expires_after_2019_12_31
        description: Expiring at midnight of 2019-12-31
        expression: request.time < timestamp("2020-01-01T00:00:00Z")

Create a IAMBinding Resource

new IAMBinding(name: string, args: IAMBindingArgs, opts?: CustomResourceOptions);
@overload
def IAMBinding(resource_name: str,
               opts: Optional[ResourceOptions] = None,
               condition: Optional[_serviceaccount.IAMBindingConditionArgs] = None,
               members: Optional[Sequence[str]] = None,
               role: Optional[str] = None,
               service_account_id: Optional[str] = None)
@overload
def IAMBinding(resource_name: str,
               args: IAMBindingArgs,
               opts: Optional[ResourceOptions] = None)
func NewIAMBinding(ctx *Context, name string, args IAMBindingArgs, opts ...ResourceOption) (*IAMBinding, error)
public IAMBinding(string name, IAMBindingArgs args, CustomResourceOptions? opts = null)
public IAMBinding(String name, IAMBindingArgs args)
public IAMBinding(String name, IAMBindingArgs args, CustomResourceOptions options)
type: gcp:serviceAccount:IAMBinding
properties: # The arguments to resource properties.
options: # Bag of options to control resource's behavior.

name string
The unique name of the resource.
args IAMBindingArgs
The arguments to resource properties.
opts CustomResourceOptions
Bag of options to control resource's behavior.
resource_name str
The unique name of the resource.
args IAMBindingArgs
The arguments to resource properties.
opts ResourceOptions
Bag of options to control resource's behavior.
ctx Context
Context object for the current deployment.
name string
The unique name of the resource.
args IAMBindingArgs
The arguments to resource properties.
opts ResourceOption
Bag of options to control resource's behavior.
name string
The unique name of the resource.
args IAMBindingArgs
The arguments to resource properties.
opts CustomResourceOptions
Bag of options to control resource's behavior.
name String
The unique name of the resource.
args IAMBindingArgs
The arguments to resource properties.
options CustomResourceOptions
Bag of options to control resource's behavior.

IAMBinding Resource Properties

To learn more about resource properties and how to use them, see Inputs and Outputs in the Architecture and Concepts docs.

Inputs

The IAMBinding resource accepts the following input properties:

Members List<string>
Role string

The role that should be applied. Only one gcp.serviceAccount.IAMBinding can be used per role. Note that custom roles must be of the format [projects|organizations]/{parent-name}/roles/{role-name}.

ServiceAccountId string

The fully-qualified name of the service account to apply policy to.

Condition IAMBindingConditionArgs

An IAM Condition for a given binding. Structure is documented below.

Members []string
Role string

The role that should be applied. Only one gcp.serviceAccount.IAMBinding can be used per role. Note that custom roles must be of the format [projects|organizations]/{parent-name}/roles/{role-name}.

ServiceAccountId string

The fully-qualified name of the service account to apply policy to.

Condition IAMBindingConditionArgs

An IAM Condition for a given binding. Structure is documented below.

members List<String>
role String

The role that should be applied. Only one gcp.serviceAccount.IAMBinding can be used per role. Note that custom roles must be of the format [projects|organizations]/{parent-name}/roles/{role-name}.

serviceAccountId String

The fully-qualified name of the service account to apply policy to.

condition IAMBindingConditionArgs

An IAM Condition for a given binding. Structure is documented below.

members string[]
role string

The role that should be applied. Only one gcp.serviceAccount.IAMBinding can be used per role. Note that custom roles must be of the format [projects|organizations]/{parent-name}/roles/{role-name}.

serviceAccountId string

The fully-qualified name of the service account to apply policy to.

condition IAMBindingConditionArgs

An IAM Condition for a given binding. Structure is documented below.

members Sequence[str]
role str

The role that should be applied. Only one gcp.serviceAccount.IAMBinding can be used per role. Note that custom roles must be of the format [projects|organizations]/{parent-name}/roles/{role-name}.

service_account_id str

The fully-qualified name of the service account to apply policy to.

condition IAMBindingConditionArgs

An IAM Condition for a given binding. Structure is documented below.

members List<String>
role String

The role that should be applied. Only one gcp.serviceAccount.IAMBinding can be used per role. Note that custom roles must be of the format [projects|organizations]/{parent-name}/roles/{role-name}.

serviceAccountId String

The fully-qualified name of the service account to apply policy to.

condition Property Map

An IAM Condition for a given binding. Structure is documented below.

Outputs

All input properties are implicitly available as output properties. Additionally, the IAMBinding resource produces the following output properties:

Etag string

(Computed) The etag of the service account IAM policy.

Id string

The provider-assigned unique ID for this managed resource.

Etag string

(Computed) The etag of the service account IAM policy.

Id string

The provider-assigned unique ID for this managed resource.

etag String

(Computed) The etag of the service account IAM policy.

id String

The provider-assigned unique ID for this managed resource.

etag string

(Computed) The etag of the service account IAM policy.

id string

The provider-assigned unique ID for this managed resource.

etag str

(Computed) The etag of the service account IAM policy.

id str

The provider-assigned unique ID for this managed resource.

etag String

(Computed) The etag of the service account IAM policy.

id String

The provider-assigned unique ID for this managed resource.

Look up an Existing IAMBinding Resource

Get an existing IAMBinding resource’s state with the given name, ID, and optional extra properties used to qualify the lookup.

public static get(name: string, id: Input<ID>, state?: IAMBindingState, opts?: CustomResourceOptions): IAMBinding
@staticmethod
def get(resource_name: str,
        id: str,
        opts: Optional[ResourceOptions] = None,
        condition: Optional[_serviceaccount.IAMBindingConditionArgs] = None,
        etag: Optional[str] = None,
        members: Optional[Sequence[str]] = None,
        role: Optional[str] = None,
        service_account_id: Optional[str] = None) -> IAMBinding
func GetIAMBinding(ctx *Context, name string, id IDInput, state *IAMBindingState, opts ...ResourceOption) (*IAMBinding, error)
public static IAMBinding Get(string name, Input<string> id, IAMBindingState? state, CustomResourceOptions? opts = null)
public static IAMBinding get(String name, Output<String> id, IAMBindingState state, CustomResourceOptions options)
Resource lookup is not supported in YAML
name
The unique name of the resulting resource.
id
The unique provider ID of the resource to lookup.
state
Any extra arguments used during the lookup.
opts
A bag of options that control this resource's behavior.
resource_name
The unique name of the resulting resource.
id
The unique provider ID of the resource to lookup.
name
The unique name of the resulting resource.
id
The unique provider ID of the resource to lookup.
state
Any extra arguments used during the lookup.
opts
A bag of options that control this resource's behavior.
name
The unique name of the resulting resource.
id
The unique provider ID of the resource to lookup.
state
Any extra arguments used during the lookup.
opts
A bag of options that control this resource's behavior.
name
The unique name of the resulting resource.
id
The unique provider ID of the resource to lookup.
state
Any extra arguments used during the lookup.
opts
A bag of options that control this resource's behavior.
The following state arguments are supported:
Condition IAMBindingConditionArgs

An IAM Condition for a given binding. Structure is documented below.

Etag string

(Computed) The etag of the service account IAM policy.

Members List<string>
Role string

The role that should be applied. Only one gcp.serviceAccount.IAMBinding can be used per role. Note that custom roles must be of the format [projects|organizations]/{parent-name}/roles/{role-name}.

ServiceAccountId string

The fully-qualified name of the service account to apply policy to.

Condition IAMBindingConditionArgs

An IAM Condition for a given binding. Structure is documented below.

Etag string

(Computed) The etag of the service account IAM policy.

Members []string
Role string

The role that should be applied. Only one gcp.serviceAccount.IAMBinding can be used per role. Note that custom roles must be of the format [projects|organizations]/{parent-name}/roles/{role-name}.

ServiceAccountId string

The fully-qualified name of the service account to apply policy to.

condition IAMBindingConditionArgs

An IAM Condition for a given binding. Structure is documented below.

etag String

(Computed) The etag of the service account IAM policy.

members List<String>
role String

The role that should be applied. Only one gcp.serviceAccount.IAMBinding can be used per role. Note that custom roles must be of the format [projects|organizations]/{parent-name}/roles/{role-name}.

serviceAccountId String

The fully-qualified name of the service account to apply policy to.

condition IAMBindingConditionArgs

An IAM Condition for a given binding. Structure is documented below.

etag string

(Computed) The etag of the service account IAM policy.

members string[]
role string

The role that should be applied. Only one gcp.serviceAccount.IAMBinding can be used per role. Note that custom roles must be of the format [projects|organizations]/{parent-name}/roles/{role-name}.

serviceAccountId string

The fully-qualified name of the service account to apply policy to.

condition IAMBindingConditionArgs

An IAM Condition for a given binding. Structure is documented below.

etag str

(Computed) The etag of the service account IAM policy.

members Sequence[str]
role str

The role that should be applied. Only one gcp.serviceAccount.IAMBinding can be used per role. Note that custom roles must be of the format [projects|organizations]/{parent-name}/roles/{role-name}.

service_account_id str

The fully-qualified name of the service account to apply policy to.

condition Property Map

An IAM Condition for a given binding. Structure is documented below.

etag String

(Computed) The etag of the service account IAM policy.

members List<String>
role String

The role that should be applied. Only one gcp.serviceAccount.IAMBinding can be used per role. Note that custom roles must be of the format [projects|organizations]/{parent-name}/roles/{role-name}.

serviceAccountId String

The fully-qualified name of the service account to apply policy to.

Supporting Types

IAMBindingCondition

Expression string

Textual representation of an expression in Common Expression Language syntax.

Title string

A title for the expression, i.e. a short string describing its purpose.

Description string

An optional description of the expression. This is a longer text which describes the expression, e.g. when hovered over it in a UI.

Expression string

Textual representation of an expression in Common Expression Language syntax.

Title string

A title for the expression, i.e. a short string describing its purpose.

Description string

An optional description of the expression. This is a longer text which describes the expression, e.g. when hovered over it in a UI.

expression String

Textual representation of an expression in Common Expression Language syntax.

title String

A title for the expression, i.e. a short string describing its purpose.

description String

An optional description of the expression. This is a longer text which describes the expression, e.g. when hovered over it in a UI.

expression string

Textual representation of an expression in Common Expression Language syntax.

title string

A title for the expression, i.e. a short string describing its purpose.

description string

An optional description of the expression. This is a longer text which describes the expression, e.g. when hovered over it in a UI.

expression str

Textual representation of an expression in Common Expression Language syntax.

title str

A title for the expression, i.e. a short string describing its purpose.

description str

An optional description of the expression. This is a longer text which describes the expression, e.g. when hovered over it in a UI.

expression String

Textual representation of an expression in Common Expression Language syntax.

title String

A title for the expression, i.e. a short string describing its purpose.

description String

An optional description of the expression. This is a longer text which describes the expression, e.g. when hovered over it in a UI.

Import

Service account IAM resources can be imported using the project, service account email, role, member identity, and condition (beta).

 $ pulumi import gcp:serviceAccount/iAMBinding:IAMBinding admin-account-iam projects/{your-project-id}/serviceAccounts/{your-service-account-email}
 $ pulumi import gcp:serviceAccount/iAMBinding:IAMBinding admin-account-iam "projects/{your-project-id}/serviceAccounts/{your-service-account-email} roles/iam.serviceAccountUser"
 $ pulumi import gcp:serviceAccount/iAMBinding:IAMBinding admin-account-iam "projects/{your-project-id}/serviceAccounts/{your-service-account-email} roles/editor user:foo@example.com"

-> Custom RolesIf you’re importing a IAM resource with a custom role, make sure to use the full name of the custom role, e.g. [projects/my-project|organizations/my-org]/roles/my-custom-role. With conditions

 $ pulumi import gcp:serviceAccount/iAMBinding:IAMBinding admin-account-iam "projects/{your-project-id}/serviceAccounts/{your-service-account-email} roles/iam.serviceAccountUser expires_after_2019_12_31"
 $ pulumi import gcp:serviceAccount/iAMBinding:IAMBinding admin-account-iam "projects/{your-project-id}/serviceAccounts/{your-service-account-email} roles/iam.serviceAccountUser user:foo@example.com expires_after_2019_12_31"

Package Details

Repository
https://github.com/pulumi/pulumi-gcp
License
Apache-2.0
Notes

This Pulumi package is based on the google-beta Terraform Provider.