Skip to content

tRPC on AWS with SST

Create and deploy a tRPC API in AWS with SST.

We are going to build a serverless tRPC API, a simple client, and deploy it to AWS using SST.

Before you get started:

  1. Configure your AWS credentials
  2. Install the SST CLI

1. Create a project

Let’s start by creating our app.

Terminal window
mkdir my-trpc-app && cd my-trpc-app
npm init -y

Init SST

Now let’s initialize SST in our app. Make sure you have the CLI installed.

Terminal window
sst init

This’ll create a sst.config.ts file in your project root.


2. Add the API

Let’s add two Lambda functions; one for our tRPC server and one that’ll be our client. Update your sst.config.ts.

sst.config.ts
async run() {
const trpc = new sst.aws.Function("Trpc", {
url: true,
handler: "index.handler",
});
const client = new sst.aws.Function("Client", {
url: true,
link: [trpc],
handler: "client.handler",
});
return {
api: api.url,
client: client.url,
};
}

We are linking the server to our client. This will allow us to access the URL of the server in our client.

Start dev mode

Start your app in dev mode. This runs your functions Live.

Terminal window
sst dev

This will give you two URLs.

Complete
api: https://gyrork2ll35rsuml2yr4lifuqu0tsjft.lambda-url.us-east-1.on.aws
client: https://3x4y4kg5zv77jeroxsrnjzde3q0tgxib.lambda-url.us-east-1.on.aws

3. Create the server

Let’s create our tRPC server. Add the following to index.ts.

index.ts
const t = initTRPC
.context<CreateAWSLambdaContextOptions<APIGatewayEvent>>()
.create();
const router = t.router({
greet: t.procedure
.input(z.object({ name: z.string() }))
.query(({ input }) => {
return `Hello ${input.name}!`;
}),
});
export type Router = typeof router;
export const handler = awsLambdaRequestHandler({
router: router,
createContext: (opts) => opts,
});

We are creating a simple method called greet that takes a string as an input.

Add the imports.

index.ts
import { z } from "zod";
import {
APIGatewayEvent,
awsLambdaRequestHandler,
CreateAWSLambdaContextOptions
} from "@trpc/server/adapters/aws-lambda";
import { initTRPC } from "@trpc/server";

And install the npm packages.

Terminal window
npm install zod @trpc/server@next

4. Add the client

Now we’ll connect to our server in our client. Add the following to client.ts.

client.ts
const client = createTRPCClient<Router>({
links: [
httpBatchLink({
url: Resource.Trpc.url,
}),
],
});
export async function handler() {
return {
statusCode: 200,
body: await client.greet.query({ name: "Patrick Star" }),
};
}

Add the relevant imports. Notice we are importing the types for our API.

index.ts
import { Resource } from "sst";
import type { Router } from "./index";
import { createTRPCClient, httpBatchLink } from "@trpc/client";

Install the client npm package.

Terminal window
npm install @trpc/client@next

Test your app

To test our app, hit the client URL.

Terminal window
curl https://3x4y4kg5zv77jeroxsrnjzde3q0tgxib.lambda-url.us-east-1.on.aws

This will print out Hello Patrick Star!.


5. Deploy your app

Now let’s deploy your app.

Terminal window
sst deploy

Connect the console

As a next step, you can manage your app and view issues in the SST Console.

Issues in tRPC app in SST Console

You can create a free account and connect it to your AWS account.