Updated Nov 23, 20268 min read

How to Build a Backend with an AI Coding Agent and Deploy It to Your Own Cloud

Pick an agent, give it a repo it can run, let it declare the database, queue and cron job in the code, and ship to your own AWS or GCP account without handing it cloud credentials.

Illustration for How to Build a Backend with an AI Coding Agent and Deploy It to Your Own Cloud

To build a backend with an AI coding agent and deploy it to your own cloud, give the agent a repo it can run locally, let it declare the database, queue and cron job in the application code, and ship through a reviewed pipeline that holds the cloud credentials. The agent writes and tests the change, and the pipeline provisions it.

With Encore, the infrastructure the agent declares is what gets provisioned in your AWS or GCP account.

Why do agent-built backends stall at deploy?

Agents are good at writing handlers and migrations. They get stuck at the edges of the repo. Hosted app builders deploy to their own platform, which is fine for a prototype but not for a backend that has to live in your company's AWS account. Editor and cloud agents such as Cursor's cloud agents and GitHub's Copilot coding agent work on a branch and open a pull request, and you still have to connect the repo to hosting, a database and a pipeline.

Three things usually go wrong after that:

  1. The agent cannot run what it wrote. The database and queue exist only in the cloud, so the agent ships code it never executed.
  2. The infrastructure lives somewhere else. A new table is in the app repo, but the queue it needs is a Terraform change in another repo the agent does not touch.
  3. Credentials leak into the loop. To get unstuck, someone gives the agent an AWS profile, and now a prompt-injected session can call cloud APIs with that person's access.

What does the agent need from the repo?

NeedWhy it mattersHow to provide it
A local command that starts everythingThe agent fixes its own errors only if it can run the codeencore run starts the API, Postgres, Pub/Sub and buckets
Infrastructure declared next to the codeThe agent adds a queue in the same change as the handlerEncore primitives in the service code
Real testsMocks miss constraint, migration and redelivery bugsencore test with separate test databases
Context about the systemThe agent sees services, APIs and schemas, not only one fileEncore skills and the local MCP server
A deploy path without cloud keysProduction changes go through reviewEncore platform connected to your cloud

How do you set up the repo for the agent?

Encore

Encore is a backend framework with built-in infrastructure, and its platform deploys into your own AWS or GCP account. Teams bring their own coding agent, and Encore provides the local environment, the system context and the deploy controls.

Get started
brew install encoredev/tap/encore &&
encore app create
Copy

Run in your terminal to get started locally.

~
$ encore app create orders --example=ts/empty$ cd orders && npx skills add encoredev/skills

The Encore skills teach Claude Code, Cursor, Codex or Copilot how to create services, databases, Pub/Sub, cron jobs, buckets and tests. The local MCP server, started with encore mcp start, gives the agent the application's services, APIs, database schemas and traces as tools. An agent can also do the whole setup itself if you tell it to read https://encore.dev/agents/getting-started.md.

What does the agent write?

Ask for a feature in product terms, for example: "Add an orders API with Postgres, send a confirmation when an order is placed, and cancel orders left pending for a day." A typical result is one service file plus a migration:

// orders/orders.ts import { api, APIError } from "encore.dev/api"; import { SQLDatabase } from "encore.dev/storage/sqldb"; import { Topic, Subscription } from "encore.dev/pubsub"; import { CronJob } from "encore.dev/cron"; import log from "encore.dev/log"; // Local Postgres under `encore run`, RDS or Cloud SQL when deployed. const db = new SQLDatabase("orders", { migrations: "./migrations" }); interface Order { id: number; email: string; status: string; } interface OrderPlaced { orderId: number; email: string; } const orderPlaced = new Topic<OrderPlaced>("order-placed", { deliveryGuarantee: "at-least-once", }); // A public guest checkout keeps the tutorial short. Add auth: true or a rate limit // before production, so nobody can create orders in bulk. export const create = api( { expose: true, method: "POST", path: "/orders" }, async ({ email }: { email: string }): Promise<Order> => { if (!email.includes("@")) throw APIError.invalidArgument("a valid email is required"); // The order commits only after the publish, so a failed publish stores nothing. await using tx = await db.begin(); const order = await tx.queryRow<Order>` INSERT INTO orders (email) VALUES (${email}) RETURNING id, email, status`; await orderPlaced.publish({ orderId: order!.id, email }); await tx.commit(); return order!; }, ); // The claim commits only after the send, so a redelivered message does nothing // and a failed send rolls back and is retried. const _confirm = new Subscription(orderPlaced, "send-confirmation", { handler: async ({ orderId, email }) => { await using tx = await db.begin(); const claimed = await tx.queryRow` INSERT INTO confirmations (order_id) VALUES (${orderId}) ON CONFLICT DO NOTHING RETURNING order_id`; if (!claimed) return; // Call your email provider here and let its errors throw. log.info("sending confirmation", { orderId, email }); await tx.commit(); }, }); export const expirePending = api( { expose: false, method: "POST", path: "/internal/expire-pending" }, async (): Promise<{ cancelled: number }> => { const rows = await db.queryAll` UPDATE orders SET status = 'cancelled' WHERE status = 'pending' AND created_at < now() - interval '24 hours' RETURNING id`; return { cancelled: rows.length }; }, ); const _expire = new CronJob("expire-pending", { title: "Cancel orders left pending for a day", every: "1h", endpoint: expirePending, });
-- orders/migrations/1_create_orders.up.sql CREATE TABLE orders ( id BIGSERIAL PRIMARY KEY, email TEXT NOT NULL, status TEXT NOT NULL DEFAULT 'pending', created_at TIMESTAMPTZ NOT NULL DEFAULT now() ); CREATE TABLE confirmations ( order_id BIGINT PRIMARY KEY REFERENCES orders (id) );

The database, the topic, the subscription and the schedule are all in the diff, so the reviewer reads one pull request. The agent brings the change; Encore provides the local environment, the system context and the deploy controls, so the agent can add infrastructure in the same change as the logic and run it right away.

How does the agent test it?

The agent runs the app and calls it, the way a developer would. You should see something like:

~/orders
$ encore runYour API is running at: http://127.0.0.1:4000$ curl -X POST localhost:4000/orders -d '{"email":"ada@example.com"}'{"id":1,"email":"ada@example.com","status":"pending"}$ curl -X POST localhost:4000/orders -d '{"email":"nope"}'{"code":"invalid_argument","message":"a valid email is required",...}

encore test runs the test suite against separate test databases, so a broken migration or a missing ON CONFLICT fails on the laptop. Cron jobs are managed by the Encore platform and run only in deployed cloud environments, not locally and not in preview environments, so the agent calls /internal/expire-pending directly to check it. Every request shows up as a trace in the local development dashboard, which the agent can read through the MCP server when a test fails.

How does it get to your cloud?

Connect your AWS or GCP account once and push. The Encore platform provisions what the code declares in your account: RDS or Cloud SQL for the database, SNS and SQS or Google Cloud Pub/Sub for the topic, and the services on Fargate, EKS or Cloud Run. The cron job is run by the Encore platform, with no scheduler resource to create.

Each pull request gets a preview environment hosted by Encore, with its own database, so a reviewer tries the feature before merge. For production you can require deploy approval for any deploy that changes infrastructure. The agent's session never holds cloud keys. Pair this with deny rules for aws, gcloud and terraform in the agent config, such as Claude Code's permission rules, so the session cannot go around the pull request.

Which approach should you pick?

ApproachWhere it runsInfrastructureCloud credentials
Hosted app builderThe builder's own platformManaged by the builderNot your cloud
Coding agent plus your own hosting and TerraformYour cloudSeparate Terraform or console workOften given to the agent to get unstuck
Coding agent plus EncoreYour own AWS or GCP accountDeclared in the code, provisioned on deployHeld by the platform, not the agent
  • Hosted app builder for a prototype that does not need to live in your company's cloud.
  • Agent plus your own pipeline and Terraform if a platform team already owns modules and wants every resource to go through them.
  • Agent plus Encore if you want the agent to build the whole backend, infrastructure included, test it locally and ship it to your own cloud through review.

Verdict. Any of the major coding agents can write the backend. What decides whether it reaches production safely is the repo: whether the agent can run it, whether the infrastructure sits next to the code, and whether credentials stay out of the session. Encore covers all three, which makes it the shortest path from an agent's pull request to your own AWS or GCP account.

Frequently asked questions

How do I build a backend with an AI coding agent and deploy it to my own cloud?

Start from a repo the agent can run locally, let it declare the database, queues and scheduled jobs in the application code, have it run the tests against local infrastructure, and open a pull request. With Encore, you bring your own coding agent; Encore provides the local environment, the system context and the deploy controls, and provisions those declarations in your AWS or GCP account, so the agent never needs cloud credentials.

Which AI coding agent is best for building a backend?

Claude Code, Cursor, OpenAI Codex and GitHub Copilot's coding agent can all write a backend. The difference that matters more is the repo: an agent that can start the database and run the tests locally fixes its own mistakes, while one that cannot only guesses.

Can an AI agent deploy a backend to AWS by itself?

Yes. It should not do it with your credentials, though. Let the agent open a pull request and let a reviewed pipeline or platform hold the AWS role and do the deploy. That keeps a prompt-injected or confused session from changing production directly.

Do I need Terraform for a backend an AI agent builds?

No. When the database, Pub/Sub topics, cron jobs and buckets are declared in the application code, the deploy provisions them, so there is no separate Terraform module for the agent to write.

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