The safest way to give an AI coding agent cloud access is to never hand it a long-lived key at all. AWS, Azure, and GCP each let a workload trade a short-lived token it already has — often one issued by whatever CI system or identity platform is running the agent — for a cloud credential that expires in minutes and is scoped to exactly one role. The three clouds implement this federation differently enough that copying an AWS setup onto Azure or GCP will not work, and knowing the differences is what lets you actually scope what an agent can do.
An agent’s own permission settings are not where this problem gets solved
Claude Code’s settings.json lets you write rules like Bash(npm run build) or Read(./.env) under permissions.allow, ask, and deny, evaluated deny-first with the first match winning. It is tempting to treat a deny rule on Read(./.env) as the control that keeps an agent away from cloud credentials. The documentation itself is explicit that this is not a security boundary: a Bash(curl *) rule is a prefix match on the literal command text, and misses /usr/bin/curl called by absolute path or a sh -c 'curl ...' wrapper entirely. The documented alternative is sandboxing the process itself or a PreToolUse hook that inspects a command before it runs — see blocking dangerous Bash commands with a PreToolUse hook for how that works in practice. Either way, the rule lives at the tool layer, not the credential layer, which is why the credential itself still has to be scoped at the cloud provider.
That split matters for a second reason: an agent running in dontAsk or bypassPermissions mode in a CI pipeline has already traded away the human-in-the-loop check. What is left to limit the damage of a bad or manipulated command is what the cloud credential in its environment is actually allowed to do — which is a cloud-side configuration problem, not an agent setting.
AWS: an OIDC identity provider plus a role whose trust policy names the exact caller
AWS IAM federation works by registering an external OIDC or SAML identity provider as a trusted entity on the account, then writing an IAM role whose trust policy says which identities from that provider can assume it. For an OIDC provider like GitHub Actions, the trust policy’s Principal names the provider by ARN, the Action is sts:AssumeRoleWithWebIdentity, and a Condition block restricts the exchange with StringEquals on the audience claim and the subject claim:
{
"Effect": "Allow",
"Principal": {
"Federated": "arn:aws:iam::012345678910:oidc-provider/token.actions.githubusercontent.com"
},
"Action": "sts:AssumeRoleWithWebIdentity",
"Condition": {
"StringEquals": {
"token.actions.githubusercontent.com:aud": "sts.amazonaws.com",
"token.actions.githubusercontent.com:sub": "repo:YourOrg/YourRepo:ref:refs/heads/main"
}
}
}
The :sub condition is what actually scopes this — AWS documents that it rejects a trust policy for this provider whose :sub key is missing or is only a wildcard, and specifically warns against a pattern as broad as repo:YourOrg/*, which would let any repository in the organization assume the role. A trust policy that pins :sub to one repository and one branch is what stops an agent running in an unrelated pipeline from picking up the same role. The resulting session credential is temporary, and the token exchange itself is checked against the external JWT’s exp claim with a five-minute clock-skew allowance — there is no separate “how long is this credential good for” knob to configure on the AWS side beyond what the role session itself specifies.
Azure: a federated identity credential that matches issuer, subject, and audience
Azure’s equivalent is a federated identity credential attached to a user-assigned managed identity or an app registration in Microsoft Entra ID. The flow runs in the other direction from how it reads: the workload first gets a token from the external identity provider (GitHub Actions, for instance), then presents that token to the Microsoft identity platform using the client-credentials flow, with the external JWT standing in for a client secret. Microsoft Entra ID checks the federated identity credential’s configured issuer, subject, and audience values against the corresponding claims in that external token — case-sensitively — and only then issues its own short-lived access token back to the workload.
Microsoft’s documentation calls out the practical upside directly: because the trust is established this way, the workload needs no client secret or certificate stored anywhere, which removes both the leaked-secret risk and the recurring certificate-expiry problem that comes with the alternative. One operational detail worth knowing before you rely on this for an agent that runs for a long time: Microsoft caches only the first 100 signing keys published at the external IdP’s OIDC endpoint, which can cause validation failures if that IdP rotates through an unusually large number of keys.
GCP: a workload identity pool, a provider, and an attribute condition written in CEL
Google Cloud’s Workload Identity Federation introduces two objects Azure and AWS don’t have named separately: a workload identity pool, which is the container for a set of external identities (Google recommends one pool per non-Google-Cloud environment), and a workload identity pool provider inside it, which describes the specific relationship to one external IdP. An external credential is exchanged through Google’s Security Token Service for a federated token, and attribute mapping — expressions of the form google.subject=assertion.sub, written in Common Expression Language — decides which claims from the external token become attributes Google Cloud can match on. google.subject is required and drives principal:// identity bindings; attribute.NAME values drive principalSet:// bindings for matching a group of identities at once.
To let a federated identity actually act as a service account, you grant it the roles/iam.workloadIdentityUser role on that service account, scoped to a specific principal:
principal://iam.googleapis.com/projects/PROJECT_NUMBER/locations/global/workloadIdentityPools/POOL_ID/subject/SUBJECT_ATTRIBUTE_VALUE
Two details here trip people up. First, that binding needs the project number, not the project ID — using the ID silently fails to match. Second, Google’s own documentation warns against granting this role to every identity in a pool at once, and recommends layering an attribute condition — another CEL expression evaluated against the provider — so only tokens matching a specific attribute are accepted at all, before the IAM binding is even checked.
The three models side by side
| AWS | Azure | GCP | |
|---|---|---|---|
| Trust object | IAM role trust policy | Federated identity credential on a managed identity / app registration | Workload identity pool + provider |
| Matched claims | aud, sub via policy Condition | issuer, subject, audience, case-sensitive | CEL attribute mapping + attribute condition |
| Scoping unit | One role per trusted caller pattern | One federated credential per managed identity | principal:// or principalSet:// binding, by project number |
| Common misconfiguration | Wildcard :sub across a whole GitHub org | Relying on issuer match alone without a narrow subject | Granting workloadIdentityUser to an entire pool |
Why this is worth the setup cost specifically for an agent
A static credential — an access key, a service account key file, a connection string in an environment variable — stays valid until someone finds it and rotates it, which in practice means it stays valid for as long as it takes someone to notice something went wrong, figure out which key was involved, and track down everywhere that key was copied to. None of the three federation models above store a long-lived secret anywhere in the first place: AWS’s temporary credentials expire with the session, Azure explicitly removes the stored-secret and certificate-expiry problems by design, and GCP issues only short-lived OAuth 2.0 access tokens through the federation exchange. The practical consequence for incident response is that containing a compromised agent session doesn’t require finding and rotating a secret at all — tightening or removing the trust relationship (the :sub condition on AWS, the federated identity credential on Azure, the attribute condition on GCP) makes every future token exchange fail immediately, and whatever temporary credential the agent already obtained simply expires on its own within minutes.
Combining cloud-side scoping with agent-side hooks
Neither layer is a substitute for the other. The cloud-side federation above controls the blast radius of a credential if an agent is tricked into misusing it — a role trust policy pinned to one repository and branch means a prompt-injected command still can’t reach past what that one role can do. A PreToolUse hook or a deny rule in settings.json controls what the agent attempts in the first place, which is the layer that actually stops the bad command before it runs. An agent connecting out to an MCP server that itself touches cloud resources needs the same scrutiny at that boundary — reviewing an MCP server’s permissions before connecting it and confirming it uses proper OAuth-based authorization rather than a static API key are both part of the same discipline applied one hop further out.
Where this sits relative to other agentic coding tools is worth checking too, since default shell permissions vary by tool — see how Claude Code, Codex CLI, and Gemini CLI compare on default shell permissions before assuming any of them sandbox credential access the same way.
Setting this up
- Register the OIDC provider your agent’s runtime already issues tokens from (GitHub Actions, GitLab CI, or a self-hosted issuer) with the target cloud, rather than minting a new long-lived key.
- On AWS, write the role trust policy’s
:subcondition as narrowly as the task allows — one repository and one branch, never a wildcarded organization. - On Azure, create a separate federated identity credential per workload rather than reusing one managed identity’s trust across unrelated agents.
- On GCP, grant
roles/iam.workloadIdentityUserto a specificprincipal://or narrowly-scopedprincipalSet://, never to the whole pool, and double-check you used the project number. - Keep a
PreToolUsehook or equivalent command-inspection layer in place regardless — the credential scope limits damage, it doesn’t stop a bad command from running.



