Skip to content

New pattern - lambda-ruby4-bedrock-cdk#3098

Closed
NithinChandranR-AWS wants to merge 3 commits into
aws-samples:mainfrom
NithinChandranR-AWS:NithinChandranR-AWS-feature-lambda-ruby4-bedrock-cdk
Closed

New pattern - lambda-ruby4-bedrock-cdk#3098
NithinChandranR-AWS wants to merge 3 commits into
aws-samples:mainfrom
NithinChandranR-AWS:NithinChandranR-AWS-feature-lambda-ruby4-bedrock-cdk

Conversation

@NithinChandranR-AWS
Copy link
Copy Markdown
Contributor

Description

First pattern for the AWS Lambda Ruby 4.0 runtime (launched April 30, 2026) with Amazon Bedrock integration.

What it does

  • Ruby 4.0 Lambda on ARM64 (Graviton) invokes Bedrock Claude Sonnet
  • JSON structured logging enabled (new in Ruby 4.0 runtime)
  • Documents cross-region inference profile IAM requirement

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.

… 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.
@NithinChandranR-AWS
Copy link
Copy Markdown
Contributor Author

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.

Comment on lines +24 to +29
fn.addToRolePolicy(new iam.PolicyStatement({
actions: ['bedrock:InvokeModel'],
resources: [
'arn:aws:bedrock:*::foundation-model/*',
`arn:aws:bedrock:*:${this.account}:inference-profile/*`,
],
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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/*.

@parikhudit
Copy link
Copy Markdown
Contributor

parikhudit commented Jun 5, 2026

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: bedrock-agent-openai-cdk/ is unrelated to this pattern; please split that PR.

Comment on lines +6 to +17
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 }]
})
)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

  1. In the handler, validate the prompt before constructing the body
  2. Make the guardrail optional via env vars and pass it through.

Comment on lines +18 to +20
environment: {
MODEL_ID: 'us.anthropic.claude-sonnet-4-20250514-v1:0',
},
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hardcoded model ID couples the IaC and the env var.

@@ -0,0 +1,52 @@
{
Copy link
Copy Markdown
Contributor

@parikhudit parikhudit Jun 5, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

services, patternType field is missing from the metadata.

Comment on lines +16 to +22
"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"
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Copy link
Copy Markdown
Contributor

@parikhudit parikhudit Jun 5, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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', {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

@parikhudit
Copy link
Copy Markdown
Contributor

parikhudit commented Jun 5, 2026

Miscellaneous nits

  • Set a CloudWatch retention policy on the function's log group (lib/lambda-ruby4-bedrock-stack.ts). Auto-created log groups default to Never expire and are not removed by cdk destroy. Provision an explicit logs.LogGroup with retention: logs.RetentionDays.ONE_MONTH and removalPolicy: cdk.RemovalPolicy.DESTROY. This also makes the existing aws-cdk-lib/aws-logs import live.
  • Cleanup section (README.md): Either adopt 1.4 (recommended) or document the manual aws logs delete-log-group ... step.
  • Testing command is inline-chained (README.md) : Split into two steps: capture FN_NAME=$(aws cloudformation describe-stacks ...), then run aws lambda invoke ... response.json && cat response.json.
  • Pattern Overview is one sentence (README.md). Expand the opening paragraph (~3-5 sentences) to call out the use case and the Ruby 4.0 / AL2023 / ARM64 angle.
  • Unused logs import (lib/lambda-ruby4-bedrock-stack.ts line 4) : Remove or wire up to the LogGroup in 1.4.
  • handler.rb has no error handling: Wrap invoke_model in begin/rescue Aws::BedrockRuntime::Errors::ServiceError, JSON::ParserError and return a structured 5xx body.
  • First reference to AWS Lambda omits the "AWS" prefix in the README opening line : "an AWS Lambda function on the Ruby 4.0 managed runtime …".
  • Heads-up (not a finding but worth knowing): lambda.Runtime.RUBY_4_0 was added to aws-cdk-lib in v2.251.0 (April 2026). Your package.json pins ^2.180.0. The submitted code uses the dynamic new lambda.Runtime('ruby4.0', lambda.RuntimeFamily.RUBY) form, which works on 2.180+, but consider either bumping to ^2.251.0 and switching to lambda.Runtime.RUBY_4_0, or keeping the dynamic form and adding a comment explaining why.

@parikhudit parikhudit closed this Jun 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants