CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-aws-lambda-powertools--event-handler

Lightweight routing to reduce boilerplate for API Gateway REST/HTTP API, ALB, Lambda Function URLs, and AppSync.

Pending
Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

AWS Lambda Powertools Event Handler

Lightweight routing for Lambda functions handling events from API Gateway (REST/HTTP), ALB, Lambda Function URLs, AWS AppSync (GraphQL/Events), and Amazon Bedrock Agents.

Package

  • Name: @aws-lambda-powertools/event-handler
  • Type: npm
  • Language: TypeScript
  • Install: npm install @aws-lambda-powertools/event-handler

Imports

// 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.

Quick Start

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);

Architecture

  • Router Classes: Specialized resolver per AWS service (HTTP, AppSync GraphQL, AppSync Events, Bedrock Agent)
  • Middleware: Composable with onion model execution (HTTP only)
  • Web Standards: HTTP router uses Web API Request/Response internally
  • Type Safety: Full TypeScript support with generics
  • Error Handling: Custom error classes and handlers
  • Decorators: Optional decorator-based registration

Capabilities

HTTP Routing

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;
}

HTTP Middleware

CORS, compression, and custom middleware composition.

cors(options?: CorsOptions): Middleware;
compress(options?: CompressionOptions): Middleware;
composeMiddleware(middleware: Middleware[]): Middleware;

AppSync GraphQL

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;
}

AppSync Events

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;
}

Bedrock Agent Functions

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>;
}

Common Types

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 };
Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/@aws-lambda-powertools/event-handler@2.29.x
Publish Source
CLI
Badge
tessl/npm-aws-lambda-powertools--event-handler badge