New pattern - lambda-ruby4-bedrock-cdk#3098
Conversation
… pattern Deploy an Amazon Bedrock Agent powered by OpenAI GPT OSS model with a Lambda action group for tool use (weather + time). First pattern combining Bedrock Agents with OpenAI models on Bedrock.
The InvokeAgent API returns a streaming response that the AWS CLI does not support. Replaced with Python SDK snippet that correctly handles the event stream.
First pattern for the April 2026 Ruby 4.0 Lambda runtime with Bedrock integration. Demonstrates JSON structured logging, ARM64 architecture, and documents cross-region inference profile IAM requirement. Deployed and tested on live AWS account.
|
Hi @biswanathmukherjee 👋 This is the first Ruby 4.0 Lambda pattern — brand new runtime (2026). Shows Ruby + Bedrock integration. Zero existing Ruby 4 patterns in the repo. Deployed and tested. |
| fn.addToRolePolicy(new iam.PolicyStatement({ | ||
| actions: ['bedrock:InvokeModel'], | ||
| resources: [ | ||
| 'arn:aws:bedrock:*::foundation-model/*', | ||
| `arn:aws:bedrock:*:${this.account}:inference-profile/*`, | ||
| ], |
There was a problem hiding this comment.
Bedrock IAM policy is wildcard across all foundation models and all inference profiles.**
The function only ever calls one model via one cross-Region inference profile, but the role grants bedrock:InvokeModel on arn:aws:bedrock:*::foundation-model/* and arn:aws:bedrock:*:${this.account}:inference-profile/*.
|
Hi @NithinChandranR-AWS, thanks for submitting this pattern! Unfortunately, we already have multiple patterns covering the AWS Lambda → Amazon Bedrock integration. Our approach to pattern uniqueness focuses on the integration of services rather than the specific runtime so we'll be closing this PR to avoid redundancy. That said, I've left some inline comments with observations from my review that should be helpful for future contributions. We'd love to see you submit patterns exploring novel service combinations! Thanks again for your interest in contributing! 🎉 Edit: |
| prompt = event['prompt'] || 'What are the benefits of serverless computing?' | ||
|
|
||
| response = client.invoke_model( | ||
| model_id: ENV['MODEL_ID'], | ||
| content_type: 'application/json', | ||
| accept: 'application/json', | ||
| body: JSON.generate({ | ||
| anthropic_version: 'bedrock-2023-05-31', | ||
| max_tokens: 512, | ||
| messages: [{ role: 'user', content: prompt }] | ||
| }) | ||
| ) |
There was a problem hiding this comment.
No prompt validation, no Amazon Bedrock guardrail.
event['prompt'] is forwarded into Bedrock without length cap or schema check, and there is no managed safety control. Two minimal additions:
- In the handler, validate the prompt before constructing the body
- Make the guardrail optional via env vars and pass it through.
| environment: { | ||
| MODEL_ID: 'us.anthropic.claude-sonnet-4-20250514-v1:0', | ||
| }, |
There was a problem hiding this comment.
Hardcoded model ID couples the IaC and the env var.
| @@ -0,0 +1,52 @@ | |||
| { | |||
There was a problem hiding this comment.
services, patternType field is missing from the metadata.
| "gitHub": { | ||
| "template": { | ||
| "repoURL": "https://github.com/aws-samples/serverless-patterns/tree/main/lambda-ruby4-bedrock-cdk", | ||
| "templateURL": "serverless-patterns/lambda-ruby4-bedrock-cdk", | ||
| "projectFolder": "lambda-ruby4-bedrock-cdk", | ||
| "templateFile": "lib/lambda-ruby4-bedrock-stack.ts" | ||
| } |
There was a problem hiding this comment.
gitHub block uses a non-standard nested shape.
The documented schema is gitHub.template (string), gitHub.templateFile (string), gitHub.projectFolder (string). Current submission nests these under gitHub.template as an object.
| ``` | ||
| 4. Deploy the stack: | ||
| ``` | ||
| cdk deploy |
There was a problem hiding this comment.
cdk deploy produces FunctionName and FunctionArn outputs that the README never tells the user how to read. Suggested section between Deployment Instructions and Testing:
| * [AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2.html) installed and configured | ||
| * [AWS CDK](https://docs.aws.amazon.com/cdk/latest/guide/cli.html) installed | ||
| * [Node.js](https://nodejs.org/en/download/) installed | ||
| * [Amazon Bedrock model access](https://docs.aws.amazon.com/bedrock/latest/userguide/model-access.html) enabled for Claude Sonnet in your region |
There was a problem hiding this comment.
Bedrock prerequisites are under-specified.
Two issues:
- the model is "Claude Sonnet" without a version, but the IaC pins
us.anthropic.claude-sonnet-4-20250514-v1:0; - the supported-Region constraint of the
us.cross-Region profile (us-east-1,us-east-2,us-west-2) is undocumented.
| constructor(scope: Construct, id: string, props?: cdk.StackProps) { | ||
| super(scope, id, props); | ||
|
|
||
| const fn = new lambda.Function(this, 'Ruby4BedrockFunction', { |
There was a problem hiding this comment.
No DLQ / OnFailure destination.
Synchronous-only today, but the pattern's stated goal of being a starting point for AI text generation makes async fan-out (SNS, EventBridge, S3) the likely next step.
| require 'json' | ||
|
|
||
| def lambda_handler(event:, context:) | ||
| client = Aws::BedrockRuntime::Client.new |
There was a problem hiding this comment.
Bedrock client is recreated per invocation and uses default timeouts.
The function's 30s Lambda timeout leaves no headroom for SDK retries against a slow Bedrock response. Move the client to module scope and configure adaptive retry plus open/read timeouts:
|
Miscellaneous nits
|
Description
First pattern for the AWS Lambda Ruby 4.0 runtime (launched April 30, 2026) with Amazon Bedrock integration.
What it does
Testing
Deployed and tested on live AWS account. Ruby 4.0.2 runtime confirmed, Bedrock response received successfully.
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.