Updated Nov 5, 20269 min read

How to Give AI Coding Agents Access to Cloud Infrastructure Safely

Scoped credentials, sandbox accounts, CI-only deploys and declaring infrastructure in code, compared by what an agent can break and who approves the change.

Coding agents run shell commands, and a shell on a developer laptop usually has whatever the developer has: an ~/.aws/credentials file, a gcloud login, a .env with a database URL.

That goes unnoticed while the agent is editing a React component, but it becomes a real problem the first time it decides the fastest way to fix a failing deploy is to run aws or terraform apply itself, with the developer's admin role, against the account that also holds production.

The agent vendors treat this as a configuration problem, and Codex runs locally with no network access and writes limited to the workspace by default, Claude Code has allow, ask and deny rules for every tool, and Cursor can run terminal commands in a sandbox that blocks unauthorized file and network access. Those controls decide what the agent may run, while the cloud side decides what a credential can do once it is used, and a safe setup needs both.

What to weigh when choosing:

  • Whether the agent holds cloud credentials at all, and for how long they are valid.
  • Which account the credentials reach, and whether production is in it.
  • Whether the agent writes IAM and network configuration, or something deterministic generates it.
  • Who approves a new resource or a deletion before it happens.

Ways to give agents cloud access at a glance

ApproachWhat the agent holdsBest fit
EncoreInfrastructure declarations in code; cloud access sits with Encore's IAM role, not the agentTeams that want agents to add databases, queues and buckets without touching IAM
Agent permission rulesWhatever the shell has, filtered by allow, ask and deny rulesA first layer on any setup
Scoped, short-lived credentialsAn assumed role in a sandbox account, capped by a permissions boundaryAgents that must call cloud APIs directly
CI-only deploys with OIDCNothing; CI assumes a role after a mergeTeams with Terraform or CDK pipelines already in place
Developer credentialsThe developer's own role, often adminNothing an agent runs unattended
Compare the approaches in detail
CapabilityEncoreAgent permission rulesScoped, short-lived credentialsCI-only with OIDCDeveloper credentials
Credentials in the agent's environmentNo cloud role needed for infrastructure changesUnchangedTemporary, scopedNoneLong-lived, broad
Who writes IAM and networkingGenerated from the code by the platformUnchangedA person, ahead of timeWhoever writes the Terraform, often the agentThe agent, if it wants
Local testing of new infrastructureencore run starts databases, Pub/Sub and buckets locallyUnchangedAgainst the sandbox accountUsually requires a deployAgainst real cloud resources
Approval before resources changeOptional infrastructure approval per environment, manual approval for deletionsPrompts per commandLimited by the boundaryPull request reviewNone
Blast radius of a mistakeResources the code declares, in the target environmentDepends on the shellThe sandbox accountWhat the pipeline role allowsEverything the developer can reach

agents declare infrastructure in code

Encore

Encore (the safe way for AI agents to ship backend changes to your cloud) combines an open source SDK, where developers and agents declare the databases, Pub/Sub topics, buckets, cron jobs and secrets an application uses in TypeScript or Go, with a platform that provisions them in your own AWS or GCP account through an IAM role or service account you grant and can revoke. Agents declare infrastructure in code, and the cloud access sits with Encore's IAM role, not the agent, so adding a database or a bucket does not require giving the agent a cloud role. The IAM policies, networking and security groups for each resource are generated from the code rather than written by the agent, and deployment settings such as capacity, networking and backups are not part of the declarations an agent edits. This does not make the agent's shell safe on its own: any keys already on the machine still need the permission rules below.

Application code passes through Encore and infrastructure guardrails to resources provisioned in your own AWS or GCP account.
Encore provisions the infrastructure your application needs in your own AWS or GCP account.

When an agent needs storage for receipts in an orders service, the whole infrastructure change is a declaration next to the code that uses it:

import { Bucket } from "encore.dev/storage/objects"; import { Topic } from "encore.dev/pubsub"; export const receipts = new Bucket("receipts", { versioned: false }); export interface ReceiptCreated { orderId: string; key: string; } export const receiptCreated = new Topic<ReceiptCreated>("receipt-created", { deliveryGuarantee: "at-least-once", });

encore run starts the bucket and the topic locally, so the agent can call the endpoint and read the trace through the local MCP server without any cloud account involved. On deploy, the platform creates an S3 bucket and SNS/SQS on AWS, or Cloud Storage and Pub/Sub on GCP, with an IAM role per service scoped to the resources it uses, following least privilege. Sizing, networking and other settings are managed centrally through infrastructure configuration, outside the code the agent edits.

Approvals sit at the environment level rather than in every command. An environment can require infrastructure approval, in which case an application admin approves new infrastructure before the deployment proceeds, and the platform does not automatically destroy infrastructure that is no longer required, so deleting it requires manual approval in the Encore dashboard. On the Enterprise plan, Encore's own access to your cloud can be just-in-time through GCP Privileged Access Manager.

Key features

  • Agents declare infrastructure in code, and cloud access sits with Encore's IAM role, not the agent.
  • IAM, networking and security configuration generated from the declarations.
  • Local infrastructure with encore run, and a preview environment per pull request.
  • Optional infrastructure approval per environment, and manual approval before infrastructure is deleted.
  • Runs in your own AWS or GCP account, with no runtime dependency on Encore.

Good to know

The agent still needs some access to deploy, which is a git push to Encore or a merged pull request, and roles decide who can push, configure secrets or approve infrastructure. Resources Encore does not model can still be managed directly in your cloud account, and existing databases, buckets and topics can be connected instead of recreated.

Try Encore →

Agent permission rules: the first layer

Every major coding agent has a way to restrict what it runs. Claude Code evaluates deny rules first, then ask rules, then allow rules, and a project can check its rules into .claude/settings.json, so the whole team gets them:

{ "permissions": { "deny": [ "Bash(aws *)", "Bash(gcloud *)", "Bash(terraform *)", "Read(~/.aws/**)", "Read(./.env)" ], "ask": ["Bash(git push *)"] } }

Key features

  • Deny rules apply before any allow rule matches.
  • Rules can block reading credential files as well as running commands.
  • Codex and Cursor offer sandboxes that restrict network and file access at the operating system level.

Good to know

Command patterns filter the agent's behavior rather than limiting the credential, and Claude Code's documentation notes that a deny rule does not match the same program called by its full path or inside sh -c, and recommends pairing deny rules with the sandbox when a restriction must hold. The credential on the machine still has its full permissions if something else uses it, so these rules work best on top of one of the approaches below.

Scoped, short-lived credentials in a sandbox account

When an agent genuinely needs to call cloud APIs, for example to inspect a resource while debugging, the AWS answer is temporary security credentials from an assumed role, which expire after a set time, in an account that does not hold production. A permissions boundary caps what the role can do even if someone attaches a broader policy later:

{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "s3:GetObject", "s3:ListBucket", "logs:FilterLogEvents", "cloudwatch:GetMetricData" ], "Resource": "*" }, { "Effect": "Deny", "Action": ["iam:*", "organizations:*"], "Resource": "*" } ] }

Key features

  • Credentials expire on their own, so a leaked session has a short life.
  • A separate account limits the blast radius to non-production resources.
  • Read-only roles cover most debugging work.

Good to know

Someone has to design the role, keep the sandbox account in sync with what the app needs and decide when the agent may write rather than read. Once the agent creates resources this way, they exist outside any code review, which is the gap the next two approaches close.

CI-only deploys with OIDC

A common pattern for teams already on Terraform or CDK is to keep credentials out of laptops entirely and let CI assume a role through OpenID Connect after a pull request merges:

on: push: branches: [main] permissions: id-token: write contents: read jobs: deploy: runs-on: ubuntu-latest environment: production steps: - uses: actions/checkout@v4 - uses: aws-actions/configure-aws-credentials@v6 with: role-to-assume: arn:aws:iam::123456789012:role/deploy aws-region: eu-central-1 - run: terraform apply -auto-approve

Key features

  • No long-lived keys anywhere, and the role trusts only your repository.
  • Every change goes through a pull request, and the production environment can require a reviewer before terraform apply runs.

Good to know

With this setup the agent no longer holds cloud credentials, but it usually still writes the Terraform, IAM policies and security groups that the pipeline applies, and reviewers have to catch an over-broad policy in a generated diff. The only way to test a new resource is often to merge and deploy it, which slows the agent's loop down to the length of a pipeline run. Why letting an agent write infrastructure config is risky covers this in more detail.

Developer credentials: the setup to avoid

Running an agent in a shell that has the developer's own long-lived keys gives it everything the developer can do, in every account the developer can reach, with no expiry. The Stack Overflow 2025 Developer Survey found that more developers distrust the accuracy of AI tools (46%) than trust it (33%), and broad credentials turn that accuracy question into a security one. The minimum change is to remove the keys from the agent's environment or to block cloud CLIs and credential files with deny rules.

How to choose

  • Encore when you want agents to add databases, topics and buckets by declaring them in code, test them locally, and have them provisioned in your AWS or GCP account by Encore's IAM role rather than by the agent, without the agent writing IAM.
  • Agent permission rules on every setup, as the layer that stops an agent from reading credential files or running cloud CLIs.
  • Scoped, short-lived credentials when an agent needs read access to a sandbox account for debugging.
  • CI-only deploys with OIDC when the team keeps Terraform or CDK and wants credentials out of laptops.
  • Developer credentials only for work a person runs and watches.

Frequently asked questions

Should an AI coding agent have AWS credentials?

Usually not long-lived ones. A safer setup lets the agent describe the infrastructure it needs in code, with a separate system holding the IAM role that provisions it. When an agent does need direct access, use short-lived credentials from an assumed role, scoped to a sandbox account and capped by a permissions boundary, and block cloud CLIs in the agent's own permission rules.

How do I let an AI coding agent create cloud resources without giving it credentials?

Have the agent declare the resources in application code and let a deploy system with its own IAM role provision them. With Encore, an agent adds a database, topic or bucket as a declaration, runs it locally with encore run, and Encore provisions it in your AWS or GCP account on deploy, optionally after an admin approves the infrastructure change. The cloud access sits with Encore's IAM role, not the agent.

How do I stop Claude Code from running AWS CLI commands?

Add a deny rule to the project's .claude/settings.json, for example "Bash(aws *)" under permissions.deny. Deny rules are evaluated before ask and allow rules, so the agent cannot run matching commands even if another rule would allow them.

Deploy to your cloud on AWS and Google Cloud
without managing infrastructure

Encore lets developers and agents define application infrastructure directly in code, then automatically provisions it from local development to production in your AWS or GCP account.

~/orders — claude
Claude Code
Claude Code v2.1.180Opus 5.5~/orders