Skip to content

Deploy with AWS Lambda

This plugin includes an onCallGenkit helper function (similar to Firebase Functions’ onCallGenkit) that makes it easy to deploy Genkit Flows as AWS Lambda functions.

import { genkit, z } from 'genkit';
import { awsBedrock, amazonNovaProV1, onCallGenkit } from 'genkitx-aws-bedrock';
const ai = genkit({
plugins: [awsBedrock()],
model: amazonNovaProV1(),
});
const myFlow = ai.defineFlow(
{
name: 'myFlow',
inputSchema: z.string(),
outputSchema: z.string(),
},
async (input) => {
const { text } = await ai.generate({ prompt: input });
return text;
},
);
// Export as Lambda handler
export const handler = onCallGenkit(myFlow);

When streaming: true is set, onCallGenkit returns a streaming Lambda handler directly for real incremental streaming via Lambda Function URLs. This is compatible with streamFlow from genkit/beta/client.

const myStreamingFlow = ai.defineFlow(
{
name: 'myStreamingFlow',
inputSchema: z.object({ subject: z.string() }),
outputSchema: z.object({ joke: z.string() }),
streamSchema: z.string(),
},
async (input, sendChunk) => {
const { stream, response } = await ai.generateStream({
prompt: `Tell me a joke about ${input.subject}`,
output: { schema: z.object({ joke: z.string() }) },
});
for await (const chunk of stream) {
sendChunk(chunk.text);
}
const result = await response;
return result.output || { joke: result.text };
},
);
// streaming: true returns a StreamifyHandler directly
export const streamingHandler = onCallGenkit(
{ streaming: true, cors: { origin: '*' } },
myStreamingFlow,
);

Deploy with a Lambda Function URL in serverless.yml:

functions:
myStreamingFunction:
handler: src/index.streamingHandler
url:
invokeMode: RESPONSE_STREAM
cors: true
import { onCallGenkit, requireApiKey } from 'genkitx-aws-bedrock';
export const handler = onCallGenkit(
{
// CORS configuration
cors: {
origin: 'https://myapp.com',
credentials: true,
},
// Context provider for authentication
contextProvider: requireApiKey('X-API-Key', process.env.API_KEY!),
// Debug logging
debug: true,
// Custom error handling
onError: async (error) => ({
statusCode: 500,
message: error.message,
}),
},
myFlow,
);

The plugin provides built-in context provider helpers that follow Genkit’s ContextProvider pattern (same as @genkit-ai/express):

import {
allowAll, // Allow all requests
requireHeader, // Require a specific header
requireApiKey, // Require API key in header
requireBearerToken, // Require Bearer token with custom validation
allOf, // Combine providers with AND logic
anyOf, // Combine providers with OR logic
} from 'genkitx-aws-bedrock';
// Public endpoint
export const publicHandler = onCallGenkit(
{ contextProvider: allowAll() },
myFlow,
);
// API key authentication
export const apiKeyHandler = onCallGenkit(
{ contextProvider: requireApiKey('X-API-Key', 'my-secret-key') },
myFlow,
);
// Bearer token with custom validation
export const tokenHandler = onCallGenkit(
{
contextProvider: requireBearerToken(async (token) => {
const user = await validateJWT(token);
return { auth: { user } };
}),
},
myFlow,
);
// Combine multiple providers (all must pass)
export const strictHandler = onCallGenkit(
{
contextProvider: allOf(
requireHeader('X-Client-ID'),
requireBearerToken(async (token) => {
return await validateToken(token);
}),
),
},
myFlow,
);

The handler follows the Genkit callable protocol (same as @genkit-ai/express).

Request body (callable protocol):

{
"data": {}
}

Direct input is also supported for convenience:

{}

Successful response:

{
"result": {}
}

Error response:

{
"error": {
"status": "UNAUTHENTICATED",
"message": "Missing auth token"
}
}

Streaming response (SSE, via streaming: true):

data: {"message": "chunk text"}
data: {"message": "more text"}
data: {"result": {"joke": "full result"}}