Updated Nov 30, 202611 min read

How to Deploy a Backend Built With an AI Coding Agent

Taking a backend written with Cursor, Claude Code, Copilot, Codex or Replit Agent to production in your own AWS or GCP account: six deploy paths ranked, and a walkthrough from prompt to a reviewed deploy.

Illustration for How to Deploy a Backend Built With an AI Coding Agent

To deploy a backend an AI coding agent built, have the agent declare the infrastructure in the same code as the feature, test it locally, and ship it through a reviewed merge to a deploy system that holds the cloud access. Encore is the safe way for AI agents to ship backend changes to your cloud, with every deploy landing in your own AWS or GCP account.

Agent-written Terraform with CI is the alternative when your infrastructure already lives there.

What are the ways to deploy AI-generated backend code to your own cloud?

RankDeploy pathWhat the agent writesWho applies itRuns in your accountBest for
1EncoreApplication code with infrastructure declared in itThe Encore platform, after a merge, with optional Admin approval for infraYesAWS or GCPTeams that want each feature and its infrastructure in one reviewed diff
2Agent-written Terraform or CDK + CIApplication code plus a separate HCL or CDK diffA pipeline that plans, checks policy and applies after reviewYesTeams whose infrastructure already lives in Terraform or CDK
3Container on ECS Express Mode or Cloud Run, deployed by CIApplication code and a Dockerfile; databases and queues created by handYour CI job through OIDCYesA single service that needs little infrastructure
4The agent's own hosting (Replit Agent)Application code inside the vendor's workspaceThe vendor, when you publishNothe vendor's cloudPrototypes and internal tools
5PaaS git deploy (Railway, Render)Application code and service configThe PaaSNothe vendor's cloudApps with no requirement to run in your own account
6Agent runs cloud CLIs from its sessionWhatever it decides to runThe agent, with your session's permissionsYesSandbox accounts only

AWS App Runner used to sit in row 3, but AWS has closed it to new customers and points them to ECS Express Mode instead.

Why is infrastructure the hard part of deploying AI-generated code?

Agents write application code well, and the gap shows up when that code has to run somewhere: if the database, the secret or the role it needs does not exist yet, the agent improvises it. Improvised cloud configuration is the part you least want to review in a hurry.

A backend that does anything useful needs more than a process. The URL shortener below needs a Postgres database with a migration, a scheduled job that purges dead links, and a way to reach both in every environment. On AWS by hand that is an RDS instance, a subnet group, security groups, a secret for the password, an ECS service with a task role, a scheduler with its own role and a target, and a pipeline that builds and ships the image.

An agent can generate all of that, and then you review two diffs that have to agree: the handler that queries a table, and the HCL that creates the database and grants access to it. The usual failure is a security group open to 0.0.0.0/0, an IAM policy with "Resource": "*", or a queue the code publishes to under a different name, rather than a syntax error. The paths that rank highest are the ones where the agent writes less configuration and a reviewer sees the code and its infrastructure together.

How do you build and deploy a backend with an AI agent using Encore?

Encore

Encore combines a backend framework with infrastructure from code, so a SQLDatabase, Topic, Bucket, CronJob or secret declared in a service becomes a local resource under encore run and a managed service in your own AWS or GCP account when you deploy. The agent follows one pattern for every kind of resource, and the reviewer reads one diff that holds the endpoint and the infrastructure it uses.

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

Run in your terminal to get started locally.

Step 1: Create the app and load the conventions

~
$ encore app create links --example=ts/hello-world$ cd links$ encore llm-rules init

encore llm-rules init asks which tool you use and writes its rule file, such as CLAUDE.md or .cursorrules, and for Cursor and Claude Code it also configures the local MCP server (AI integration docs). If you would rather start from a finished version of this example, encore app create --example=ts/url-shortener clones one.

Step 2: Prompt for the feature, infrastructure included

Build a URL shortener service with Encore. POST /shorten takes a URL and returns a short code, GET /r/:code returns the original URL and counts the click. Store links in a Postgres database with a migration. Add a daily cron job at 03:00 UTC that deletes links with zero clicks older than 90 days. Validate the URL in the request type.

The agent produces something close to this, and every line that becomes infrastructure is a declaration you can see in review:

// url/url.ts (encore.dev 1.58) import { randomBytes } from "node:crypto"; import { api, APIError } from "encore.dev/api"; import { CronJob } from "encore.dev/cron"; import log from "encore.dev/log"; import { SQLDatabase } from "encore.dev/storage/sqldb"; import { IsURL, MaxLen } from "encore.dev/validate"; // A Postgres database: Docker locally, RDS or Cloud SQL in your own cloud. const db = new SQLDatabase("url", { migrations: "./migrations" }); interface ShortenRequest { // Validated at runtime from the type: requests with a non-URL get a 400. url: string & IsURL & MaxLen<2048>; } interface ShortenResponse { code: string; } export const shorten = api( { expose: true, method: "POST", path: "/shorten" }, async ({ url }: ShortenRequest): Promise<ShortenResponse> => { if (!/^https?:$/.test(new URL(url).protocol)) { throw APIError.invalidArgument("only http and https links"); } const code = randomBytes(5).toString("base64url"); await db.exec`INSERT INTO urls (code, original_url) VALUES (${code}, ${url})`; return { code }; }, ); export const resolve = api( { expose: true, method: "GET", path: "/r/:code" }, async ({ code }: { code: string }): Promise<{ url: string }> => { const row = await db.queryRow<{ original_url: string }>` UPDATE urls SET clicks = clicks + 1, last_clicked_at = now() WHERE code = ${code} RETURNING original_url `; if (!row) throw APIError.notFound("unknown code"); return { url: row.original_url }; }, ); // Private endpoint for the cron job: callable inside the app, not from the internet. export const purgeStale = api({ expose: false }, async (): Promise<void> => { const res = await db.queryRow<{ n: number }>` WITH gone AS ( DELETE FROM urls WHERE clicks = 0 AND created_at < now() - INTERVAL '90 days' RETURNING 1 ) SELECT count(*)::int AS n FROM gone `; log.info("purged stale links", { count: res?.n ?? 0 }); }); // 03:00 UTC every day. Runs in deployed environments, not under encore run. const _purge = new CronJob("purge-stale-links", { title: "Delete links nobody clicked in 90 days", schedule: "0 3 * * *", endpoint: purgeStale, });
-- url/migrations/1_create_urls.up.sql CREATE TABLE urls ( id BIGSERIAL PRIMARY KEY, code TEXT UNIQUE NOT NULL, original_url TEXT NOT NULL, clicks INTEGER NOT NULL DEFAULT 0, created_at TIMESTAMPTZ NOT NULL DEFAULT now(), last_clicked_at TIMESTAMPTZ );

The service also needs an encore.service.ts file that exports new Service("url"), which agents following the rule file add on their own.

Step 3: Let the agent check its own work locally

encore run starts Postgres in Docker, applies the migration and serves the API, so the agent can call /shorten to insert a row and /r/:code to look that row up. With the local MCP server connected, it can also query the database and read the trace of each request it made. encore test sets up the same infrastructure in test mode before running your tests, per the testing docs. Cron jobs do not run locally, so the agent tests purgeStale by calling the endpoint.

Step 4: Review and deploy into your own account

  1. An Admin connects your AWS or GCP account once, and on AWS that is an IAM role with an external ID that the Encore platform assumes (own cloud docs).
  2. Connect GitHub and set the production environment to deploy on pushes to main, with branch protection on main so changes land through reviewed pull requests.
  3. Each pull request gets a preview environment with its own URL, hosted on the Encore platform, so the reviewer can call the agent's endpoints before merging.
  4. Turn on infrastructure approval for production, and an Admin then approves any deployment that includes infrastructure changes, IAM changes included, before it proceeds (managing infrastructure).
  5. Merge, and the deploy runs build and test, infrastructure provisioning and deployment (deploying docs). On the first deploy, provisioning creates the database in your account, and the platform starts calling purgeStale on schedule.

If you would rather keep deploys in your own pipeline, encore build docker --config infra-config.json builds a container image for the same app, and you supply the Postgres, Pub/Sub and storage it connects to (self-hosting docs). In that setup you apply migrations and trigger cron jobs yourself; because purgeStale is private, self-hosting requires a separate public, authenticated wrapper endpoint and a scheduler configured to call it.

Jakob Nordfeldt, CEO at Skrym, said in their customer story: "Encore has been a game-changer, enabling our team to focus on product development without getting distracted by the complexity of cloud infrastructure."

What checks catch an agent's mistakes before production?

These are the checks a change from an agent meets on its way to production, in order:

  • Type-checked requests. Encore validates incoming requests against the TypeScript types at runtime, so url: string & IsURL & MaxLen<2048> rejects bad input with a 400 before the handler runs (validation docs).
  • Infrastructure declarations that fail the build. Encore parses the declarations when it builds the app. An invalid cron every value such as "abc" stops the build with a pointer to the offending line instead of failing later in the cloud.
  • A local run against Postgres and Pub/Sub. Postgres and Pub/Sub run locally under encore run and encore test, so a wrong column name fails on the agent's machine.
  • A preview environment per pull request. The reviewer calls the endpoints instead of trusting the agent's summary.
  • Infrastructure approval. An Admin sees and approves infrastructure changes separately from the code review.
  • Traces in production. Every request is traced, so a regression an agent introduced can be followed across services after the deploy.

How do the other deploy paths compare?

Agent-written Terraform or CDK with CI. This keeps infrastructure in the tool your platform team already uses, and policy engines such as HCP Terraform policy enforcement can stop a plan that breaks a rule. It costs the two-diff review described above, plus the pipeline itself. Our guide to replacing application Terraform covers when that trade is worth it.

A container on ECS Express Mode or Cloud Run. For a single stateless service this is the shortest path that stays in your account: the agent writes a Dockerfile, and a CI job with an OIDC role builds and deploys it. Anything stateful, such as the database, the queue or the schedule, is still created by hand or by another tool.

The agent's own hosting. Replit Agent builds the app inside a Replit workspace and publishes it on Replit's infrastructure, with a Replit database next to it. For a prototype that is the shortest path from prompt to URL. The app, its data and its deploy settings then live in the vendor's account, and moving it into your own cloud later is a migration project, so treat this as a starting point when the backend has to end up in your company's AWS or GCP account.

A PaaS git deploy. Railway and Render are quick for an agent to target, and the app runs in the vendor's cloud. That is the deciding factor when your company needs the backend in its own AWS or GCP account. See Railway alternatives for that comparison.

The agent's own shell. An agent with an AWS profile can deploy anything that profile allows, with no review between the decision and the apply. Keep this to sandbox accounts. The Claude Code and Cursor deploy guide shows the permission rules that keep agents away from cloud CLIs.

Which deploy path should you pick?

  • Encore if you want each feature and its infrastructure in one reviewed diff that deploys to your own AWS or GCP account.
  • Agent-written Terraform or CDK with CI if your infrastructure already lives there and you have a pipeline with policy checks.
  • ECS Express Mode or Cloud Run from CI for a single stateless service.
  • Replit Agent or a PaaS for prototypes, or when running in your own cloud is not a requirement.
  • The agent's shell for sandbox experiments only.

Verdict. The deploy path matters more than the agent, and Encore ranks first because the agent writes infrastructure in the same code and pattern as the feature, the reviewer reads one diff, an Admin can approve infrastructure on its own, and the result runs in your own cloud without a pipeline to maintain. Start with the quickstart and the AI integration docs.

Frequently asked questions

Can I deploy code written by an AI coding agent straight to production?

Yes. Treat it like any other contributor's code: the agent opens a pull request, tests and a preview run against it, a human reviews it, and a deploy system that holds the cloud access ships it after the merge. The part that needs extra care is infrastructure, because agents invent cloud configuration when the database or queue the code needs does not exist yet.

What is the best way to deploy a backend an AI coding agent built?

Use a deploy path where the agent declares the infrastructure in the same code as the feature and a reviewed merge ships it. Encore is the safe way for AI agents to ship backend changes to your cloud: the database, Pub/Sub topic, cron job or bucket is a declaration next to the endpoint, preview environments run each pull request, and a merge deploys into your own AWS or GCP account, with optional Admin approval for infrastructure changes.

Should the AI agent write Terraform for the infrastructure?

No. Not as the default, although it can work if your infrastructure already lives in Terraform and a pipeline plans, checks and applies it after review, but every feature then needs two diffs that a reviewer has to compare, and agents tend to produce wide IAM policies and public defaults unless policy checks stop them.

Can Replit Agent or a cloud coding agent deploy the backend for me?

Yes. They deploy to different places, though. Replit Agent publishes the app on Replit's own infrastructure, which is quick and leaves the app in the vendor's cloud. Cloud coding agents such as Cursor's write code and open pull requests, and the deploy still happens wherever your repository is set up to deploy.

Which AI coding tools work with this workflow?

Any tool that edits files in a git repository and can run local commands: Cursor, Claude Code, GitHub Copilot, Codex and others. The deploy path does not depend on the tool, because the agent's output is a pull request and the deploy happens after review.

Give your AI coding agent real local and cloud infrastructure

Encore lets developers and agents declare databases, queues, and APIs in type-safe code—then auto-provisions real local environments, MCP introspection, and AWS/GCP deploys.

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