@effect-aws/lambda

中文文档
2.1.0发布于 2026/11/01MITTypeScriptES Module零依赖近 30 天下载 13.3万

Effectful AWS Lambda handler

原始使用文档
文档目录4 节
  1. Installation
  2. Usage
  3. Lambda Web Functions
  4. Lambda Event Functions

@effect-aws/lambda

Clean way to write AWS Lambda Event Function handlers and Web Function servers using Effect.

npm version npm downloads

It provides a LambdaHandler.make function that takes an EffectHandler and returns a native promise Lambda handler function.

The implementation supports defining a global runtime layer with graceful shutdown. Finalizers defined by acquireRelease run when a shutdown signal is received, subject to Lambda's cleanup deadline. Graceful shutdown is not guaranteed in all failure modes.

Installation

npm install --save @effect-aws/lambda

Usage

Lambda Web Functions

Lambda Web Functions run a normal HTTP server. Use LambdaWebFunction.layer with Effect's HttpRouter or HttpApi to listen on the address supplied by Lambda:

npm install @effect-aws/lambda effect @effect/platform-node @effect/platform-node-shared
import * as LambdaWebFunction from "@effect-aws/lambda/LambdaWebFunction"
import { NodeRuntime } from "@effect/platform-node"
import { Effect, Layer } from "effect"
import { HttpRouter, HttpServerResponse } from "effect/http"

const Routes = HttpRouter.use((router) =>
  router.add("GET", "/", Effect.succeed(HttpServerResponse.text("Hello from Lambda Web Functions!")))
)

HttpRouter.serve(Routes).pipe(
  Layer.provide(LambdaWebFunction.layer),
  Layer.launch,
  NodeRuntime.runMain
)

For a schema-first API, pass your HttpApiBuilder.layer(...) with its handler layers to HttpRouter.serve in place of Routes. The server supports normal HTTP response streaming and concurrent requests. Keep request-specific state in Effect's request context rather than shared mutable globals.

Web Function support is available through the @effect-aws/lambda/LambdaWebFunction subpath. It requires the optional @effect/platform-node peer dependency and a compatible Effect version (@effect/[email protected] requires effect@^4.0.0). Existing Event Function users can continue importing from @effect-aws/lambda without installing @effect/platform-node.

LambdaWebFunction.endpoint reads and validates AWS_LAMBDA_HTTP_ENDPOINT using Effect's ConfigProvider. Missing or malformed values fail with ConfigError; there is no implicit listen-address fallback. Set it explicitly to run locally:

AWS_LAMBDA_HTTP_ENDPOINT=127.0.0.1:3000 node index.js

Compile or bundle your entry point to index.js at the root of your deployment package, including its runtime dependencies. For ESM output, include a package.json with "type": "module". Web Functions use Node.js 24 on Arm64 at launch. Deploy the prepared directory with the Web Functions-enabled AWS CLI:

aws lambda-web deploy --name my-effect-app --code ./dist

Lambda supplies the listen address in production. The server must start listening within 10 seconds. NodeRuntime.runMain handles SIGTERM and closes the layer's scope; the helper limits HTTP request draining to 500 milliseconds to leave time for other finalizers within Lambda's one-second cleanup window. Cleanup remains best-effort.

Web Functions do not use exported Lambda event handlers, Function URL RESPONSE_STREAM configuration, or LambdaHandler.context(). Their management APIs belong to the separate @aws-sdk/client-lambda-web SDK package, which is not required to serve HTTP requests.

Lambda Event Functions

LambdaHandler.make, LambdaHandler.fromHttpApi, and the streaming adapters below target Event Functions, including API Gateway integrations and Lambda Function URLs.

Without dependencies:

import { Effect } from "effect"
import { EffectHandler, LambdaHandler, SNSEvent } from "@effect-aws/lambda"

// Define your effect handler
const myEffectHandler: EffectHandler<SNSEvent, never> = (event, context) => {
  // Your effect logic here
  return Effect.succeed("Hello, World!")
}

// Create the Lambda handler
export const handler = LambdaHandler.make(myEffectHandler)

With dependencies:

import { EffectHandler, LambdaHandler, SNSEvent } from "@effect-aws/lambda"
import * as Logger from "@effect-aws/powertools-logger"
import { Context, Effect, Layer } from "effect"

interface FooService {
  bar: () => Effect.Effect<never, never, void>
}
const FooService = Context.Tag<FooService>()
const FooServiceLive = Layer.succeed(
  FooService,
  FooService.of({ bar: () => Logger.logInfo("Not implemented") })
)

// Define your effect handler with dependencies
const myEffectHandler: EffectHandler<SNSEvent, FooService> = (event, context) =>
  Effect.gen(function* () {
    yield* Logger.logInfo("Received event", { event, context })
    const service = yield* FooService
    return yield* service.bar()
  })

// Create the global layer
const LambdaLive = Layer.provideMerge(
  FooServiceLive,
  Logger.DefaultPowerToolsLoggerLayer
)

// Create the Lambda handler
export const handler = LambdaHandler.make({
  handler: myEffectHandler,
  layer: LambdaLive
})

Streaming:

import { S3 } from "@effect-aws/client-s3"
import { Handler, LambdaHandler } from "@effect-aws/lambda"
import { NodeStream } from "@effect/platform-node"
import { Cause, Effect, Stream } from "effect"
import { Readable } from "node:stream"
import { createGzip } from "node:zlib"

/**
 * Streaming handler that reads a file from S3, compresses it using gzip, and
 * returns the compressed data as a stream.
 */
const streamHandler = () => {
  const stream = S3.getObject({
    Bucket: "example-bucket",
    Key: "file.txt"
  }).pipe(
    Effect.map((result) =>
      NodeStream.fromReadable(
        () => result.Body as Readable,
        (e) => new Cause.UnknownException(e)
      )
    ),
    Stream.unwrap
  )

  return stream.pipe(
    Stream.pipeThroughChannelOrFail(
      NodeStream.fromDuplex(
        () => createGzip(),
        (e) => new Cause.UnknownException(e)
      )
    )
  )
}

export const handler: Handler = LambdaHandler.stream({
  handler: streamHandler,
  layer: S3.defaultLayer
})

HttpApi streaming with Lambda Function URLs:

import { LambdaHandler } from "@effect-aws/lambda"
import {
  HttpApi,
  HttpApiBuilder,
  HttpApiEndpoint,
  HttpApiGroup,
  HttpApiSchema,
  HttpServer,
  HttpServerResponse
} from "@effect/platform"
import { Effect, Layer, Stream } from "effect"

const encoder = new TextEncoder()

const streamEndpoint = HttpApiEndpoint.get("stream")`/stream`.addSuccess(
  HttpApiSchema.Text()
)
const pingEndpoint = HttpApiEndpoint.get("ping")`/ping`.addSuccess(
  HttpApiSchema.Text()
)

const group = HttpApiGroup.make("api").add(streamEndpoint).add(pingEndpoint)
const api = HttpApi.make("Api").add(group)

const routes = HttpApiBuilder.group(api, "api", (handlers) =>
  handlers
    .handle("ping", () => Effect.succeed("pong\n"))
    .handleRaw("stream", () =>
      Effect.succeed(
        HttpServerResponse.stream(
          Stream.fromIterable(["chunk 1\n", "chunk 2\n", "done\n"]).pipe(
            Stream.map((chunk) => encoder.encode(chunk))
          ),
          { contentType: "text/plain; charset=utf-8" }
        )
      )
    )
)

const apiLive = HttpApiBuilder.api(api).pipe(Layer.provide(routes))

export const handler = LambdaHandler.streamFromHttpApi(
  Layer.mergeAll(apiLive, HttpServer.layerContext)
)

Use handle for normal schema responses and handleRaw for streaming HttpServerResponse values. The Lambda must be invoked through a Lambda Function URL with response streaming enabled, for example InvokeMode: RESPONSE_STREAM. API Gateway HTTP API integrations do not support Lambda response streaming.

版本与发布记录

@effect-aws/lambda 共发布 25 个版本,首次发布于 2024/01/29;过去 12 个月发布了 11 个版本。

版本发布日期距今
2.1.0latest2026/11/0112 天前
2.0.02026/10/021 个月前
2.0.0-beta.5beta2026/06/045 个月前
1.7.02026/06/045 个月前
2.0.0-beta.42026/04/156 个月前
2.0.0-beta.32026/03/128 个月前

关于 @effect-aws/lambda 的常见问题

@effect-aws/lambda 是什么?

Effectful AWS Lambda handler

如何安装 @effect-aws/lambda?

在项目目录运行 npm install @effect-aws/lambda,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:effect(>=4.0.0-beta.66 <5.0.0)、@effect/platform-node-shared(>=4.0.0 <5.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@effect-aws/lambda 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@effect-aws/lambda 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@effect-aws/lambda 可以商用吗?

@effect-aws/lambda 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@effect-aws/lambda 的下载量有多少?

根据 npm 官方下载统计,@effect-aws/lambda 在 2026/10/13 – 2026/11/11 这 30 天内被下载 133,022 次,日均约 4,434 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@effect-aws/lambda 还在维护吗?

最新版本 2.1.0 发布于 2026/11/01(12 天前);至今共发布 25 个版本,其中过去 12 个月发布了 11 个;首次发布于 2024/01/29。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。