Skip to content

GCP.IAM reference

Source: packages/gcp/src/IAM/Member.ts

One role → member edge on a single resource’s IAM policy — the GCP analog of a Terraform google_*_iam_member. Only this edge is managed: other members of the role, other roles, and bindings of the same role under another condition are left alone. Updates are etag-guarded and retried on concurrent writes.

Event sources use it to let a push identity invoke its Cloud Run host.

yield* GCP.IAM.Member("Invoker", {
kind: "run.service",
name: service.name,
role: "roles/run.invoker",
member: Output.interpolate`serviceAccount:${caller.email}`,
});

Member: Granting a role on part of a resource

Section titled “Member: Granting a role on part of a resource”

A condition narrows a grant to the requests its expression is true for, on the services that support IAM Conditions. A grant under one condition is separate from the same role under another, or under none.

yield* GCP.IAM.Member("PublicReader", {
kind: "storage.bucket",
name: bucket.bucketName,
role: "roles/storage.objectViewer",
member: Output.interpolate`serviceAccount:${reader.email}`,
condition: {
title: "public objects only",
expression: Output.interpolate`resource.name.startsWith("projects/_/buckets/${bucket.bucketName}/objects/public/")`,
},
});

Source: packages/gcp/src/IAM/Policy.ts

An IAM v2 deny policy attached to a project, folder, or organization.

This is not GCP.OrgPolicy.Policy (organization policy constraints). Deny policies use annotations for Alchemy ownership. Create, update, and delete are long-running operations.

const policy = yield* GCP.IAM.Policy("Probe", {
displayName: "alchemy-probe",
rules: [{
denyRule: {
deniedPermissions: ["iam.googleapis.com/roles.list"],
deniedPrincipals: [
"principal://goog/subject/alchemy-deny-probe@example.invalid",
],
},
}],
});

Source: packages/gcp/src/IAM/ServiceAccount.ts

A user-managed Google Cloud IAM service account.

Grant it roles with GCP.IAM.Member (kind: "project" for project-wide roles), and pass its email to resources that run as an identity, such as GKE node pools or Compute instances.

ServiceAccount: Creating a Service Account

Section titled “ServiceAccount: Creating a Service Account”

Service account with a generated id

const account = yield* GCP.IAM.ServiceAccount("Worker", {
displayName: "Background worker",
});

Service account with an explicit id

const account = yield* GCP.IAM.ServiceAccount("GkeNodes", {
accountId: "prod-gke-nodes",
displayName: "Production GKE nodes",
});
const account = yield* GCP.IAM.ServiceAccount("Worker", {});
yield* GCP.IAM.Member("WorkerLogs", {
kind: "project",
name: account.project,
role: "roles/logging.logWriter",
member: Output.interpolate`serviceAccount:${account.email}`,
});

ServiceAccount: Looking up an existing service account

Section titled “ServiceAccount: Looking up an existing service account”

ServiceAccount.lookup references a service account Alchemy doesn’t manage (created in the console or by another stack). It is read during plan/deploy, never created, updated or deleted, and binds like a declared service account.

const serviceAccount = GCP.IAM.ServiceAccount.lookup({
accountId: "my-account",
});

Source: packages/gcp/src/IAM/ServiceAccountKey.ts

A key for a Google Cloud IAM service account, for code that runs outside GCP and has to sign as the account.

Prefer running as the service account (Cloud Run, Cloud Functions, GKE Workload Identity) where you can: a key is a long-lived secret. Some organizations block key creation with the iam.disableServiceAccountKeyCreation policy.

const account = yield* GCP.IAM.ServiceAccount("Ci", {});
const key = yield* GCP.IAM.ServiceAccountKey("CiKey", {
serviceAccount: account.email,
});

Source: packages/gcp/src/IAM/WorkloadIdentityPool.ts

A Google Cloud IAM workload identity pool: a trust boundary that lets external identities (GitHub Actions, AWS, any OIDC or SAML issuer) exchange their tokens for short-lived Google credentials without service-account keys.

Add an issuer with GCP.IAM.WorkloadIdentityProvider, then let the federated principals impersonate a service account with GCP.IAM.Member (roles/iam.workloadIdentityUser).

const pool = yield* GCP.IAM.WorkloadIdentityPool("GitHub", {
workloadIdentityPoolId: "github-actions",
displayName: "GitHub Actions",
});

WorkloadIdentityPool: Impersonating a Service Account

Section titled “WorkloadIdentityPool: Impersonating a Service Account”
const deployer = yield* GCP.IAM.ServiceAccount("Deployer", {});
yield* GCP.IAM.Member("GitHubDeployer", {
kind: "iam.serviceAccount",
name: deployer.name,
role: "roles/iam.workloadIdentityUser",
member: Output.interpolate`principalSet://iam.googleapis.com/${pool.name}/attribute.repository/my-org/my-repo`,
});

WorkloadIdentityPool: Looking up an existing workload identity pool

Section titled “WorkloadIdentityPool: Looking up an existing workload identity pool”

WorkloadIdentityPool.lookup references a workload identity pool Alchemy doesn’t manage (created in the console or by another stack). It is read during plan/deploy, never created, updated or deleted, and binds like a declared workload identity pool.

const workloadIdentityPool = GCP.IAM.WorkloadIdentityPool.lookup({
workloadIdentityPoolId: "my-workload-identity-pool",
});

Source: packages/gcp/src/IAM/WorkloadIdentityProvider.ts

An issuer in a Google Cloud IAM workload identity pool (a “workload identity pool provider”). Tokens from the issuer that pass the attribute condition can be exchanged for short-lived Google credentials.

const pool = yield* GCP.IAM.WorkloadIdentityPool("GitHub", {
workloadIdentityPoolId: "github-actions",
});
yield* GCP.IAM.WorkloadIdentityProvider("GitHubOidc", {
workloadIdentityPoolId: pool.workloadIdentityPoolId,
workloadIdentityPoolProviderId: "github",
attributeMapping: {
"google.subject": "assertion.sub",
"attribute.repository": "assertion.repository",
},
attributeCondition: `assertion.repository == "my-org/my-repo"`,
oidc: { issuerUri: "https://token.actions.githubusercontent.com" },
});
yield* GCP.IAM.WorkloadIdentityProvider("Aws", {
workloadIdentityPoolId: pool.workloadIdentityPoolId,
attributeMapping: {
"google.subject": "assertion.arn",
"attribute.aws_role": "assertion.arn.extract('assumed-role/{role}/')",
},
aws: { accountId: "123456789012" },
});

Changing workloadIdentityPoolId or workloadIdentityPoolProviderId replaces the provider. GCP keeps a deleted provider for 30 days and reserves its id; deploying that id again restores it and re-applies the declared settings.

WorkloadIdentityProvider: Looking up an existing workload identity provider

Section titled “WorkloadIdentityProvider: Looking up an existing workload identity provider”

WorkloadIdentityProvider.lookup references a workload identity provider Alchemy doesn’t manage (created in the console or by another stack). It is read during plan/deploy, never created, updated or deleted, and binds like a declared workload identity provider.

const workloadIdentityProvider = GCP.IAM.WorkloadIdentityProvider.lookup({
workloadIdentityPoolProviderId: "my-workload-identity-pool-provider",
workloadIdentityPoolId: "my-workload-identity-pool",
});