Deploying to Shuttle
Learn how to deploy an Axum app to Shuttle, the Rust-native platform, using the shuttle_runtime main macro, Shuttle.toml, secrets, and a provisioned Postgres database
Shuttle is a platform built for Rust. Where Fly.io and Render run a Docker image you built, Shuttle builds and runs the Rust code directly. There is no Dockerfile to write. The app's entry point changes from the usual #[tokio::main] function to a #[shuttle_runtime::main] function, and Shuttle takes the router that function returns, binds it to a port, and serves it. Databases and secrets are requested from inside the Rust code itself, as function arguments, and Shuttle provisions them.
Where This Fits
The previous two platforms treated the app as a container: build an image, run it, route traffic to it. Shuttle treats the app as Rust code: hand over the router, and the platform does the rest. The app still defines the same routes and handlers. What changes is the entry point, the configuration file, and where the database comes from. The Compose file and the Dockerfile stay on the laptop for local work, and Shuttle replaces them in the cloud with its own build and its own resource provisioning.
Think of the difference between renting a kitchen and hiring a caterer. Fly.io and Render rent you a kitchen, and you bring your own recipe and equipment. Shuttle is the caterer: you hand over the menu, and it brings the kitchen, the ingredients, and the serving staff. The food is the same. The work around it is different.
The Smallest Working Version
Install the Shuttle command line tool, named shuttle. On Linux or macOS, the install script from the Shuttle docs does it:
curl -sSfL https://www.shuttle.dev/install | bash
The same tool is also available as a Cargo subcommand, installed with cargo install cargo-shuttle and then run as cargo shuttle. Either way, the commands below are the same, with shuttle in front. Then create a project with the Axum template:
shuttle init --template axum my-axum-app
The template creates a Cargo project with the Shuttle dependencies already in place. The generated src/main.rs looks like this:
// src/main.rs
use axum::{routing::get, Router};
async fn hello_world() -> &'static str {
"Hello, world!"
}
#[shuttle_runtime::main]
async fn main() -> shuttle_axum::ShuttleAxum {
let router = Router::new().route("/", get(hello_world));
Ok(router.into())
}
# Cargo.toml
[package]
name = "my-axum-app"
version = "0.1.0"
edition = "2021"
[dependencies]
axum = "=0.8.9"
shuttle-axum = "=0.57.0"
shuttle-runtime = "=0.57.0"
tokio = { version = "=1.54.1", features = ["full"] }
Run it locally first:
shuttle run
The local run builds the project and starts it on port 8000 by default. Test it with curl http://localhost:8000/ and the answer is Hello, world!. When it works locally, deploy it:
shuttle login
shuttle deploy
The login command opens a browser window to connect a GitHub account. The deploy command archives the project, builds it on Shuttle's infrastructure, and starts the service. The service gets a public address under *.shuttle.app, such as https://my-axum-app-0000.shuttle.app. Open that address and the same greeting appears, this time over HTTPS.
No Live Deployment Was Made for This Page:
No Shuttle account was created and nothing was deployed while writing this page. The commands, the Shuttle.toml file, and the resource attributes below were checked against the Shuttle documentation on docs.shuttle.dev and the crate documentation for shuttle-runtime 0.57.0 and shuttle-axum 0.57.0, and the Axum code was compile-checked locally against those exact crate versions. Treat the deploy commands as checked against the official docs, not as a deployment that happened here. Shuttle's platform changed its domain from *.shuttleapp.rs to *.shuttle.app, and older guides still show the old one, so check the current docs before following any guide you find elsewhere.
Working Setup:
If shuttle run answers on port 8000 locally, and after shuttle deploy the public address answers the same greeting, the deployment works. The router reached Shuttle, Shuttle bound it and gave it an address, and the same code serves in both places.
Understanding the Entry Point Piece by Piece
The main function looks strange to a reader used to #[tokio::main], so each part deserves a plain explanation.
The macro
#[shuttle_runtime::main]
async fn main() -> shuttle_axum::ShuttleAxum {
The #[shuttle_runtime::main] attribute is a procedural macro, which is a piece of code that rewrites the function at compile time. It generates the same kind of startup code that #[tokio::main] generates, a main function that starts the Tokio runtime, and adds the Shuttle wiring: it receives the provisioned resources, builds the service, and hands it to the runtime that serves it. The function itself never binds a port and never calls axum::serve. Shuttle does that part after the function returns.
The return type
let router = Router::new().route("/", get(hello_world));
Ok(router.into())
The function returns a shuttle_axum::ShuttleAxum, which is Shuttle's wrapper around an Axum router. The .into() call converts the Router into that wrapper type. The Ok wraps it in a Result, because the function can fail, for example when a requested database cannot be provisioned, and a failure at this point stops the deploy with an error instead of starting a broken service.
What disappeared
There is no TcpListener::bind, no PORT variable, and no axum::serve call. Shuttle chooses the port and runs the server. An app that binds its own port inside a Shuttle service would fight the platform for control of the socket. The router is the whole app as far as Shuttle is concerned, and everything about serving it is the platform's job.
The Concept Behind Infrastructure From Code
Shuttle calls its approach infrastructure from code, which means resources are declared in the Rust code instead of in a separate configuration file. A database is not a line in a YAML file. It is a function argument with an attribute on it. A secret is not an entry in a dashboard form. It is a value read from a store that Shuttle fills in.
A beginner can think of it as ordering by pointing at the menu instead of filling in a form. The form asks for the database name, size, and version in separate boxes. The menu lets you point at the dish and say that one. The attribute on the function argument is the pointing. Shuttle reads it, provisions the matching resource, and passes the connected handle into the function.
Why This Design
The design removes the whole container layer for Rust apps. There is no Dockerfile to keep in sync with the code, no image to build and push, and no port mapping to get wrong. The deploy is one command, and the thing being deployed is the code itself, not an image of the code. For a small Rust API, that is less to learn, less to maintain, and fewer places for a configuration mistake to hide.
The trade-off is control. A Dockerfile lets the app choose its exact base image, its system packages, and its build steps. Shuttle makes those choices, and an app that needs a specific system library or a custom build step may not fit. The platforms in the previous pages keep that control. Shuttle trades it for simplicity.
Adding a Postgres Database
A database is requested by adding a String argument to the main function, with the Postgres attribute from the shuttle-shared-db crate. Shuttle provisions a managed Postgres database and passes its connection string in as that argument. The app then builds its own pool from the string, exactly as it would from a DATABASE_URL variable.
// src/main.rs
use axum::{extract::State, routing::get, Json, Router};
use serde_json::json;
use sqlx::PgPool;
async fn hello() -> &'static str {
"Hello from Shuttle!"
}
async fn health(State(pool): State<PgPool>) -> Json<serde_json::Value> {
match sqlx::query("SELECT 1").fetch_one(&pool).await {
Ok(_) => Json(json!({"status": "ok", "database": "ok"})),
Err(_) => Json(json!({"status": "ok", "database": "down"})),
}
}
#[shuttle_runtime::main]
async fn main(
#[shuttle_shared_db::Postgres] database_url: String,
) -> shuttle_axum::ShuttleAxum {
let pool = PgPool::connect(&database_url)
.await
.expect("could not connect to Postgres");
let router = Router::new()
.route("/", get(hello))
.route("/health", get(health))
.with_state(pool);
Ok(router.into())
}
# Cargo.toml
[package]
name = "my-axum-app"
version = "0.1.0"
edition = "2021"
[dependencies]
axum = "=0.8.9"
serde_json = "1.0.154"
shuttle-axum = "=0.57.0"
shuttle-runtime = "=0.57.0"
shuttle-shared-db = { version = "=0.57.0", features = ["postgres"] }
sqlx = { version = "=0.9.0", features = ["postgres", "runtime-tokio"] }
tokio = { version = "=1.54.1", features = ["full"] }
The #[shuttle_shared_db::Postgres] attribute on the database_url argument is the whole database configuration. During shuttle run, Shuttle starts a local Postgres, usually in a container, and passes the local connection string in, so local development matches production. During shuttle deploy, Shuttle provisions a managed Postgres and passes that connection string instead. The handler code never sees where the database lives. It receives a string, builds a pool from it, and uses the pool exactly as it would a pool built by hand.
The features = ["postgres"] line on shuttle-shared-db is required. Without it, the Postgres resource does not exist in the crate, and the compiler reports that it cannot find it. The feature gates the Postgres resource, the same way the postgres feature on sqlx gates Postgres support there.
The versions checked for this page are axum 0.8.9, tokio 1.54.1, sqlx 0.9.0, and shuttle-runtime, shuttle-axum, and shuttle-shared-db all at 0.57.0. The shuttle-axum 0.57.0 crate accepts axum 0.8, which is why this pairing works. Older Shuttle guides show 0.49.0 or earlier, and those versions pair with older axum releases, so do not mix version numbers from different guides. The code on this page was compile-checked locally against these exact versions, including the database example above.
The Pool Arrives as a String, Not as a Pool:
On Fly.io and Render, the app receives a DATABASE_URL string and builds its own pool from it. On Shuttle, the app receives the same kind of value, a connection string, but it arrives as a function argument instead of an environment variable. There is no DATABASE_URL to read and no separate configuration step. The string is the configuration, and PgPool::connect turns it into the pool the handlers use. A sqlx feature on shuttle-shared-db can hand over a ready-made pool instead, but that feature depends on sqlx 0.8, which cannot share a binary with the sqlx 0.9 used everywhere else in this chapter, so the connection-string form is the one to use here.
Secrets
Secrets are read from a SecretStore, which Shuttle passes into main the same way it passes the database connection string. Local values live in a Secrets.toml file, and deployed values are set with the CLI.
// src/main.rs
use axum::{routing::get, Router};
async fn hello() -> &'static str {
"Hello from Shuttle!"
}
#[shuttle_runtime::main]
async fn main(
#[shuttle_runtime::Secrets] secret_store: shuttle_runtime::SecretStore,
) -> shuttle_axum::ShuttleAxum {
let jwt_secret = secret_store
.get("JWT_SECRET")
.expect("JWT_SECRET must be set");
let _ = jwt_secret;
let router = Router::new().route("/", get(hello));
Ok(router.into())
}
# Secrets.toml
JWT_SECRET = "a-long-random-secret-for-local-development-only"
The #[shuttle_runtime::Secrets] attribute marks the argument as the secrets store, and secret_store.get("JWT_SECRET") reads one value by name, returning None when it is missing. The .expect call fails the deploy with a clear message when a required secret is absent, which is the same fail-fast behaviour as the configuration pages: a server that needs a secret should not start without one.
For a deployed service, the same file is used at deploy time. By default shuttle deploy reads Secrets.toml from the project, and a different file can be passed with shuttle deploy --secrets <file>. Secrets can also be managed in the Shuttle Console. Either way, the values never go into version control:
shuttle deploy --secrets Secrets.toml
The Secrets.toml file holds real secrets, so add it to .gitignore before the first commit, exactly as with a .env file.
Never Commit Secrets.toml:
Secrets.toml holds the same kind of values as a .env file: signing secrets, API keys, and passwords. Committing it publishes those values to everyone who can read the repository, and changing them later does not erase them from the history. Add Secrets.toml to .gitignore before the first commit, and set deployed secrets with the CLI.
Shuttle.toml and Project Naming
The Shuttle.toml file sits beside Cargo.toml and holds the project's settings. The most used setting is the name:
# Shuttle.toml
name = "my-axum-app"
When the name is not set, the service takes the crate's name from Cargo.toml. Setting it explicitly is useful when the crate name and the public service name should differ. The name can also be overridden on the command line, with shuttle deploy --name my-axum-app, which is handy when the same code is deployed under different names.
Other settings live in the same file, and the current list is in the Shuttle documentation on project configuration. The file is small on purpose. Most of what other platforms put in configuration files lives in Shuttle's Rust attributes instead, so Shuttle.toml holds only what does not fit in the code.
Everyday Commands
The daily commands cover the whole lifecycle, from local run to deploy to logs.
shuttle run
Runs the service locally on port 8000, with a local database when one is requested. This is the command for development and for checking that the service starts before deploying it.
shuttle deploy
Archives the project, builds it on Shuttle, and starts the service. The service gets its public address under *.shuttle.app.
shuttle logs
Streams the service's logs from the deployed run. The startup messages and any panic text appear here, and this is the first place to look when a deploy fails.
shuttle --help
Lists every command and subcommand the installed CLI offers, with a short description of each. When a command on this page does not match the installed version, this output is the authority, because the CLI changes faster than any guide.
Checking the Deploy
Two checks confirm a deploy from the outside. The first is the public address:
curl https://my-axum-app-0000.shuttle.app/
Hello from Shuttle!
The address includes a short number after the project name, which Shuttle adds to keep names unique. The exact address is printed by the deploy command and shown in the dashboard, so copy it from there rather than guessing it. The second check is the health route, which proves the database is connected as well as the server:
curl https://my-axum-app-0000.shuttle.app/health
{"database": "ok", "status": "ok"}
When the greeting works but the health route reports the database down, the service deployed fine and the database provisioning is the problem. When neither answers, the deploy itself failed, and shuttle logs is the next step.
Common Beginner Mistakes
Keeping #[tokio::main] alongside the Shuttle macro
// Wrong - Shuttle provides the runtime, and two entry points conflict
#[tokio::main]
#[shuttle_runtime::main]
async fn main() -> shuttle_axum::ShuttleAxum {
A Shuttle service has one entry point, the #[shuttle_runtime::main] function. Keeping the old #[tokio::main] attribute on the same function, or keeping a second main elsewhere in the project, produces a confusing compile error about duplicate entry points. The fix is to remove #[tokio::main] and let the Shuttle macro own the startup.
Binding a port by hand
// Wrong - Shuttle binds the port, and this code fights it
let listener = tokio::net::TcpListener::bind("0.0.0.0:8000").await.unwrap();
axum::serve(listener, router).await.unwrap();
Shuttle serves the router it receives. Binding a listener and serving it inside the main function means the function never returns the router, so Shuttle never gets it, and the service never starts. The fix is to return the router, as in the examples, and let Shuttle serve it.
Reading DATABASE_URL by hand
// Wrong - Shuttle passes the pool, and there is no URL to read
let pool = PgPool::connect(&std::env::var("DATABASE_URL").unwrap()).await.unwrap();
On Shuttle, the database arrives as a connection string argument, not as an environment variable. Reading DATABASE_URL finds nothing, and the program fails at startup. The fix is the #[shuttle_shared_db::Postgres] database_url: String argument from the example above.
Mixing Shuttle versions from different guides
Shuttle's crates move together: shuttle-runtime, shuttle-axum, and shuttle-shared-db should all be the same version, and that version must accept the axum version in the project. Version 0.57.0 accepts axum 0.8. Mixing 0.57.0 of one crate with 0.49.0 of another, or copying a Cargo.toml from an old guide, produces version errors that look like code errors. The fix is to take all three version numbers from the same release, and to check the current release on crates.io rather than trusting a guide's numbers.
Committing Secrets.toml
A Secrets.toml file with real values, committed to version control, publishes those values to everyone who can read the repository. The fix is .gitignore, and managing deployed secrets through the Shuttle Console or a deploy-time secrets file.
Final Complete Example
Here is the whole service in one place: the entry point with a database and a secret, the router, and the commands to run and deploy it.
// src/main.rs
use axum::{extract::State, routing::get, Json, Router};
use serde_json::json;
use sqlx::PgPool;
async fn hello() -> &'static str {
"Hello from Shuttle!"
}
async fn health(State(pool): State<PgPool>) -> Json<serde_json::Value> {
match sqlx::query("SELECT 1").fetch_one(&pool).await {
Ok(_) => Json(json!({"status": "ok", "database": "ok"})),
Err(_) => Json(json!({"status": "ok", "database": "down"})),
}
}
#[shuttle_runtime::main]
async fn main(
#[shuttle_shared_db::Postgres] database_url: String,
#[shuttle_runtime::Secrets] secret_store: shuttle_runtime::SecretStore,
) -> shuttle_axum::ShuttleAxum {
let jwt_secret = secret_store
.get("JWT_SECRET")
.expect("JWT_SECRET must be set");
let _ = jwt_secret;
let pool = PgPool::connect(&database_url)
.await
.expect("could not connect to Postgres");
let router = Router::new()
.route("/", get(hello))
.route("/health", get(health))
.with_state(pool);
Ok(router.into())
}
# Cargo.toml
[package]
name = "my-axum-app"
version = "0.1.0"
edition = "2021"
[dependencies]
axum = "=0.8.9"
serde_json = "1.0.154"
shuttle-axum = "=0.57.0"
shuttle-runtime = "=0.57.0"
shuttle-shared-db = { version = "=0.57.0", features = ["postgres"] }
sqlx = { version = "=0.9.0", features = ["postgres", "runtime-tokio"] }
tokio = { version = "=1.54.1", features = ["full"] }
# Shuttle.toml
name = "my-axum-app"
shuttle run
curl http://localhost:8000/
shuttle deploy
curl https://my-axum-app-0000.shuttle.app/
The let _ = jwt_secret; line in the example keeps the code compiling while the secret is not yet used by a handler. In a real service, the secret would be passed into the router state or into the code that signs tokens, instead of being discarded. The line is a placeholder for that wiring, not a pattern to keep.
Summary
Shuttle removes the container from the deployment story and replaces it with Rust code: the router is the app, the database is a function argument, and the secret is a value from a store. That is the whole trade. What the app gives up is control over the image and the port. What it gains is a deploy that is one command, a local run that matches production without a Dockerfile, and a configuration surface small enough to hold in one file.
The decision framework is short. Choose Shuttle when the app is plain Rust, the database is Postgres, and the team would rather write Rust than maintain Dockerfiles. Choose a container platform when the app needs a specific system package, a custom build step, or a second service that Shuttle does not provide. The app code that matters, the routes, the handlers, and the health checks, is the same either way, and the health route that proves the database answers is the same route on every platform in this chapter.