Skip to main content
The AWS Lambda adapter handles requests from API Gateway HTTP API (v2) and supports streaming responses via Lambda Response Streaming.

Installation

npm install @orpc/server
Import from the subpath:
import { RPCHandler } from '@orpc/server/aws-lambda'

Basic usage

import { RPCHandler } from '@orpc/server/aws-lambda'
import { CORSPlugin } from '@orpc/server/plugins'
import { router } from './router'

const handler = new RPCHandler(router, {
  plugins: [new CORSPlugin()],
})

export const lambdaHandler = awslambda.streamifyResponse(
  async (event, responseStream, context) => {
    const result = await handler.handle(event, responseStream, {
      context: { lambdaContext: context },
    })

    if (!result.matched) {
      const response = awslambda.HttpResponseStream.from(responseStream, {
        statusCode: 404,
      })
      response.write('No procedure matched')
      response.end()
    }
  },
)

handle(event, responseStream, options)

ParameterTypeDescription
eventAPIGatewayProxyEventV2The Lambda event from API Gateway v2
responseStreamResponseStreamThe Lambda response stream
options.contextInitial context typeContext passed to middleware
options.prefixstring (optional)Strip this prefix before routing
Returns { matched: true } or { matched: false }.
The AWS Lambda adapter requires Lambda Response Streaming, which is configured by wrapping your handler with awslambda.streamifyResponse(). This is needed to support SSE and large responses efficiently.

Passing Lambda context

You can pass the Lambda Context object (execution role, request ID, etc.) into the oRPC context:
import type { Context as LambdaContext } from 'aws-lambda'

const base = os.$context<{ lambdaContext: LambdaContext }>()

export const lambdaHandler = awslambda.streamifyResponse(
  async (event, responseStream, lambdaContext) => {
    await handler.handle(event, responseStream, {
      context: { lambdaContext },
    })
  },
)

Options

plugins
StandardHandlerPlugin[]
Plugins to apply to the handler.
strictGetMethodPluginEnabled
boolean
default:"true"
Restricts non-mutation procedures to GET requests.

Build docs developers (and LLMs) love