Lightweight routing to reduce boilerplate for API Gateway REST/HTTP API, ALB, Lambda Function URLs, and AppSync.
—
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Pending
The risk profile of this skill
Lightweight routing for Lambda functions handling events from API Gateway (REST/HTTP), ALB, Lambda Function URLs, AWS AppSync (GraphQL/Events), and Amazon Bedrock Agents.
npm install @aws-lambda-powertools/event-handler// HTTP (API Gateway, ALB, Function URLs)
import { Router, HttpStatusCodes, NotFoundError, RouteMatchingError, ParameterValidationError, composeMiddleware } from '@aws-lambda-powertools/event-handler/http';
import { cors, compress } from '@aws-lambda-powertools/event-handler/http/middleware';
// AppSync GraphQL
import { AppSyncGraphQLResolver, makeId, awsDateTime, awsDate, awsTime, awsTimestamp } from '@aws-lambda-powertools/event-handler/appsync-graphql';
// AppSync Events
import { AppSyncEventsResolver, UnauthorizedException } from '@aws-lambda-powertools/event-handler/appsync-events';
// Bedrock Agent
import { BedrockAgentFunctionResolver, BedrockFunctionResponse } from '@aws-lambda-powertools/event-handler/bedrock-agent';
// CommonJS
const { Router } = require('@aws-lambda-powertools/event-handler/http');Type Inference: TypeScript types are inferred automatically. Most types are not exported as runtime values. Use generic parameters for custom typing.
HTTP Router
import { Router } from '@aws-lambda-powertools/event-handler/http';
const router = new Router();
router.get('/hello', async ({ req }) => ({ message: 'Hello World!' }));
router.post('/users', async ({ req }) => {
const body = await req.json();
return new Response(JSON.stringify({ id: '123', ...body }), { status: 201 });
});
export const handler = async (event, context) => router.resolve(event, context);AppSync GraphQL
import { AppSyncGraphQLResolver } from '@aws-lambda-powertools/event-handler/appsync-graphql';
const app = new AppSyncGraphQLResolver();
app.onQuery<{ id: string }>('getTodo', async ({ id }) => ({ id, title: 'Sample', completed: false }));
app.onMutation<{ title: string }>('createTodo', async ({ title }) => ({ id: makeId(), title, completed: false }));
export const handler = async (event, context) => app.resolve(event, context);API Gateway (V1/V2), ALB, Lambda Function URLs with middleware support.
class Router {
constructor(options?: { logger?: GenericLogger; prefix?: Path });
resolve(event, context, options?: ResolveOptions): Promise<APIGatewayProxyResult | APIGatewayProxyStructuredResultV2 | ALBResult>;
get|post|put|patch|delete|head|options(path: Path, handler: RouteHandler): void;
get|post|put|patch|delete|head|options(path: Path, middleware: Middleware[], handler: RouteHandler): void;
use(middleware: Middleware): void;
includeRouter(router: Router, options?: { prefix: Path }): void;
errorHandler<T extends Error>(errorType: ErrorConstructor<T> | ErrorConstructor<T>[], handler: ErrorHandler<T>): void;
}CORS, compression, and custom middleware composition.
cors(options?: CorsOptions): Middleware;
compress(options?: CompressionOptions): Middleware;
composeMiddleware(middleware: Middleware[]): Middleware;Route GraphQL queries, mutations, and custom resolvers without VTL.
class AppSyncGraphQLResolver {
constructor(options?: { logger?: GenericLogger });
resolve(event, context, options?: ResolveOptions): Promise<unknown>;
onQuery<TParams>(fieldName: string, handler: ResolverHandler<TParams>): void;
onMutation<TParams>(fieldName: string, handler: ResolverHandler<TParams>): void;
resolver<TParams>(handler: ResolverHandler<TParams>, options: { fieldName: string; typeName?: string }): void;
batchResolver<TParams, TSource>(handler: BatchResolverHandler<TParams, TSource>, options: GraphQlBatchRouteOptions): void;
exceptionHandler<T extends Error>(error: ErrorClass<T> | ErrorClass<T>[], handler: ExceptionHandler<T>): void;
}Real-time pub/sub with automatic channel routing.
class AppSyncEventsResolver {
constructor(options?: { logger?: GenericLogger });
resolve(event, context, options?: ResolveOptions): Promise<void | OnPublishOutput | OnPublishAggregateOutput>;
onPublish<T extends boolean>(path: string, handler: OnPublishHandler<T>, options?: { aggregate?: T }): void;
onSubscribe(path: string, handler: OnSubscribeHandler): void;
}Expose tools for LLM agents with automatic routing.
class BedrockAgentFunctionResolver {
constructor(options?: { logger?: Pick<GenericLogger, 'debug' | 'warn' | 'error'> });
tool<TParams extends Record<string, ParameterValue>>(fn: ToolFunction<TParams>, config: { name: string; description?: string }): void;
resolve(event, context): Promise<BedrockAgentFunctionResponse>;
}interface GenericLogger {
debug(message: string, ...args: unknown[]): void;
info(message: string, ...args: unknown[]): void;
warn(message: string, ...args: unknown[]): void;
error(message: string, ...args: unknown[]): void;
}
interface ResolveOptions {
scope?: unknown; // For decorator binding
}
type JSONValue = string | number | boolean | null | JSONValue[] | { [key: string]: JSONValue };