Skip to content

AWS Bedrock plugin

An AWS Bedrock plugin for Genkit Go that provides text generation, image generation, and embedding capabilities using AWS Bedrock foundation models via the Converse API.

Terminal window
go get github.com/xavidop/genkit-aws-bedrock-go
  • Text Generation: Support for multiple foundation models via AWS Bedrock Converse API
  • Image Generation: Support for image generation models like Amazon Titan Image Generator
  • Embeddings: Support for text embedding models from Amazon Titan and Cohere
  • Streaming: Full streaming support for real-time responses
  • Tool Calling: Complete function calling capabilities with schema validation and type conversion
  • Multimodal Support: Support for text + image inputs (vision models)
  • Schema Management: Automatic conversion between Genkit and AWS Bedrock schemas
  • Type Safety: Robust type conversion for tool parameters (handles AWS document.Number types)
package main
import (
"context"
"log"
"github.com/firebase/genkit/go/ai"
"github.com/firebase/genkit/go/genkit"
bedrock "github.com/xavidop/genkit-aws-bedrock-go"
)
func main() {
ctx := context.Background()
bedrockPlugin := &bedrock.Bedrock{
Region: "us-east-1",
}
// Initialize Genkit
g := genkit.Init(ctx,
genkit.WithPlugins(bedrockPlugin),
genkit.WithDefaultModel("bedrock/anthropic.claude-sonnet-4-5-20250929-v1:0"), // Set default model
)
bedrock.DefineCommonModels(bedrockPlugin, g) // Optional: Define common models for easy access
log.Println("Starting basic Bedrock example...")
// Example: Generate text (basic usage)
response, err := genkit.Generate(ctx, g,
ai.WithPrompt("What are the key benefits of using AWS Bedrock for AI applications?"),
)
if err != nil {
log.Printf("Error generating text: %v", err)
} else {
log.Printf("Generated response: %s", response.Text())
}
log.Println("Basic Bedrock example completed")
}
package main
import (
"context"
"log"
"github.com/firebase/genkit/go/ai"
"github.com/firebase/genkit/go/genkit"
bedrock "github.com/xavidop/genkit-aws-bedrock-go"
)
func main() {
ctx := context.Background()
// Initialize Bedrock plugin
bedrockPlugin := &bedrock.Bedrock{
Region: "us-east-1", // Optional, defaults to AWS_REGION or us-east-1
}
// Initialize Genkit
g := genkit.Init(ctx,
genkit.WithPlugins(bedrockPlugin),
)
// Define a Claude 3 model
claudeModel := bedrockPlugin.DefineModel(g, bedrock.ModelDefinition{
Name: "anthropic.claude-sonnet-4-5-20250929-v1:0",
Type: "text",
}, nil)
// Generate text
response, err := genkit.Generate(ctx, g,
ai.WithModel(claudeModel),
ai.WithMessages(ai.NewUserMessage(
ai.NewTextPart("Hello! How are you?"),
)),
)
if err != nil {
log.Fatal(err)
}
log.Println(response.Text())
}

The plugin supports various configuration options:

bedrockPlugin := &bedrock.Bedrock{
Region: "us-west-2", // AWS region
MaxRetries: 3, // Max retry attempts
RequestTimeout: 30 * time.Second, // Request timeout
AWSConfig: customAWSConfig, // Custom AWS config (optional)
}
OptionTypeDefaultDescription
Regionstring"us-east-1"AWS region for Bedrock
MaxRetriesint3Maximum retry attempts
RequestTimeouttime.Duration30sRequest timeout
AWSConfig*aws.ConfignilCustom AWS configuration

The plugin uses the standard AWS SDK v2 configuration methods:

  1. Environment Variables:
Terminal window
export AWS_ACCESS_KEY_ID="your-access-key"
export AWS_SECRET_ACCESS_KEY="your-secret-key"
export AWS_REGION="us-east-1"
  1. AWS Credentials File (~/.aws/credentials):
[default]
aws_access_key_id = your-access-key
aws_secret_access_key = your-secret-key
region = us-east-1
  1. IAM Roles (when running on AWS services like EC2, ECS, Lambda)

  2. AWS SSO/CLI (aws configure sso)

Create an IAM policy with these permissions:

{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"bedrock:InvokeModel",
"bedrock:InvokeModelWithResponseStream"
],
"Resource": ["arn:aws:bedrock:*::foundation-model/*"]
}
]
}
// Prompt caching helps to save input token costs and reduce latency for repeated contexts.
// The first cache point must be defined after 1,024 tokens for most models.
// More about prompt caching: https://docs.aws.amazon.com/bedrock/latest/userguide/prompt-caching.html
response, err := genkit.Generate(ctx, g,
ai.WithMessages(
ai.NewSystemMessage(
ai.NewTextPart(sysprompt), // A big system prompt that is reused
bedrock.NewCachePointPart(), // A cache point after the system prompt
),
ai.NewUserTextMessage(input),
),
)