Deploying TanStack Start to AWS Amplify Hosting
Learn how to deploy a TanStack Start app to AWS Amplify Hosting and how to use a custom adapter to make authenticated server-side calls to AWS services like AppSync and S3.
TanStack Start is a full-stack React framework built on top of TanStack Router.
This post walks through how to deploy a TanStack Start app to AWS Amplify Hosting, and introduces an adapter that enables authenticated server-side calls to AWS services like AppSync and S3.
Basic Hosting with Nitro's aws_amplify Preset
TanStack Start uses Nitro as its server engine.
Nitro ships with a built-in
aws_amplify preset, so apps that don't make authenticated AWS API calls can be deployed to Amplify Hosting without any additional adapter.Configure
vite.config.ts as follows:import { defineConfig } from "vite";import { tanstackStart } from "@tanstack/react-start/plugin/vite";import viteReact from "@vitejs/plugin-react";import { nitro } from "nitro/vite";
export default defineConfig({ plugins: [ nitro({ config: { preset: "aws_amplify", awsAmplify: { runtime: "nodejs24.x" } } }), tanstackStart(), viteReact(), ],});After the build, artifacts are output to the
.amplify-hosting directory. Point amplify.yml to that directory:version: 1frontend: preBuild: commands: - npm install build: commands: - npm run build artifacts: baseDirectory: .amplify-hostingAn Adapter Is Required for Authenticated AWS API Calls
When you need to call AppSync (Amplify Data) or S3 (Amplify Storage) from the server side with authentication, you need to enable Amplify's SSR support.
Amplify JS provides official SSR adapters for Next.js and Nuxt, but there was no adapter for TanStack Start.
So I built one and published it as an npm package.
How to Use amplify-adapter-tanstack-start
Installation
npm add aws-amplify amplify-adapter-tanstack-startServer-side Configuration
Create
src/lib/amplifyServerUtils.ts and initialize runWithAmplifyServerContext using createServerRunner .import { createServerRunner } from "amplify-adapter-tanstack-start";import outputs from "../../amplify_outputs.json";
export const { runWithAmplifyServerContext } = createServerRunner({ config: outputs,});Generate the Data Client for SSR
Create
src/lib/amplify-ssr-client.ts to set up a server-side Data client.import type { Schema } from "../../amplify/data/resource";import { parseAmplifyConfig } from "aws-amplify/utils";import { generateClient } from "aws-amplify/api/server";import config from "../../amplify_outputs.json";
const amplifyConfig = parseAmplifyConfig(config);export const client = generateClient<Schema>({ config: amplifyConfig,});Client-side Configuration
Call
Amplify.configure in your root component (e.g. src/routes/__root.tsx ).
ssr: true is required. This tells Amplify to store auth tokens in the browser's cookie store so that they are automatically sent with requests to your TanStack Start server.import { Amplify } from "aws-amplify";import config from "../../amplify_outputs.json";
Amplify.configure(config, { ssr: true });Calling Amplify APIs from Server Functions
In TanStack Start, server-side Amplify API calls must be made inside a
createServerFn handler.
Pass your operation to runWithAmplifyServerContext and the request-scoped auth context is managed automatically.import { createFileRoute } from "@tanstack/react-router";import { createServerFn } from "@tanstack/react-start";import { runWithAmplifyServerContext } from "~/lib/amplifyServerUtils";import { client } from "~/lib/amplify-ssr-client";
const fetchTodos = createServerFn({ method: "GET" }).handler(async () => { const { data: todos, errors } = await runWithAmplifyServerContext({ operation: (contextSpec) => client.models.Todo.list(contextSpec), }); return { todos: todos ?? [], error: errors?.map((e) => e.message).join(", "), };});
export const Route = createFileRoute("/")({ loader: () => fetchTodos(), component: Home,});
function Home() { const { todos } = Route.useLoaderData(); return ( <ul> {todos.map((todo) => ( <li key={todo.id}>{todo.content}</li> ))} </ul> );}Example App (Todo App)
The repository includes a fully working Todo app under
examples/todo-app :- Sign-in / sign-out with Amplify Auth (Cognito)
- Todo CRUD with Amplify Data (AppSync)
- Protected routes that redirect unauthenticated users to the login page
Check it out if you want a runnable reference implementation.
Summary
Use case | What you need |
|---|---|
Static site or simple SSR | Nitro aws_amplify preset only |
Authenticated SSR calls to AppSync / S3 | amplify-adapter-tanstack-start in addition |
If you are interested in combining TanStack Start with Amplify, give amplify-adapter-tanstack-start a try.
Feedback Welcome
If you try it out, I'd love to hear what you think. Bug reports, feature requests, and general feedback are all welcome via GitHub Issues .
And if you find it useful, a ⭐ on the repo would mean a lot!
Enjoyed reading this content? Let the author know!
Your likes, comments, shares, and saves help creators reach more builders.
Loading recommendations
Loading article