← Back to Dashboard
1. Lambda Invocation Pipeline2. API Gateway, Auth, and Rate Limits

Lambda Invocation Pipeline

📚 Bedrock with Lambda and APIs11 min105 XP⌨ Hands-on lab

Serverless Bedrock APIs

Lambda + API Gateway is a strong default for many Bedrock workloads: scalable, managed, and easy to secure. The shape is simple - but several LLM-specific details separate a working demo from a solid service.

The Reference Pipeline

API Gateway (auth, throttle, validate)
  → Lambda (contract validation → prompt assembly → Bedrock call → shaping)
      → bedrock-runtime.converse(...)
  → telemetry (traceId, tokens, latency, guardrail action)

A Handler Shaped for Production

const { BedrockRuntimeClient, ConverseCommand } = require("@aws-sdk/client-bedrock-runtime");
const client = new BedrockRuntimeClient({});   // init OUTSIDE handler - reused across warm invocations

exports.handler = async (event) => {
  const body = validate(JSON.parse(event.body));       // 1. contract first
  if (!body.ok) return { statusCode: 400, body: body.error };

  const out = await client.send(new ConverseCommand({
    modelId: process.env.MODEL_ID,                      // 2. config, not hardcode
    messages: [{ role: "user", content: [{ text: body.prompt }] }],
    inferenceConfig: { maxTokens: 512 },
  }));

  log({ traceId: event.requestContext.requestId,        // 3. telemetry every call
        usage: out.usage, stop: out.stopReason });
  return { statusCode: 200, body: JSON.stringify(out.output.message) };
};

LLM-Specific Lambda Settings

SettingGuidanceWhy
Timeout60-120s (generation is slow)Default 3s kills every real inference
API Gateway limit29s integration ceilingLong generations need streaming (Function URLs) or async patterns
Memory512MB-1GB typicalCPU scales with memory → faster JSON/TLS handling
StreamingLambda response streaming + ConverseStreamToken-by-token UX for chat frontends
  • Validate request contracts at the API boundary - schemas on API Gateway plus in-handler checks.
  • Attach an execution role with narrow Bedrock permissions (pinned model ARNs - see the IAM module).
  • Emit trace IDs for every invocation and propagate them into tool calls.
Cold-start note: initialise the Bedrock client outside the handler and keep bundles lean. For spiky low-latency APIs, a small provisioned-concurrency floor smooths TTFT far cheaper than over-provisioning containers.
⌨ HANDS-ON LABScaffold a Bedrock Lambda Handler
⭐ +150 XP

Create a minimal Lambda shape for validating input and forwarding Bedrock requests.

1Create a handler file.
2Add environment-driven model ID.
lab-sandbox — simulated environment
INFINITY LAB SANDBOX v2.6 — simulated shell
Type the command for the current objective. Helpers: "hint", "solution", "clear".
$
OBJECTIVE 1 / 2 — type "hint" if stuck
🧪 Knowledge Check
Press 1-4 to select1 of 2
Why validate at API boundary before model calls?
To increase latency
To block malformed/unsafe requests early
To remove IAM
To avoid logs