You can run Ruby code in AWS Lambda. Lambda providesruntimes for Ruby that run your code to process events. Your code runs in an environment that includes the AWS SDK for Ruby, with credentials from an AWS Identity and Access Management (IAM) role that you manage. To learn more about the SDK versions included with the Ruby runtimes, seeRuntime-included SDK versions.
Lambda supports the following Ruby runtimes.
Name | Identifier | Operating system | Deprecation date | Block function create | Block function update |
---|---|---|---|---|---|
Ruby 3.4 |
| Amazon Linux 2023 | Not scheduled | Not scheduled | Not scheduled |
Ruby 3.3 |
| Amazon Linux 2023 | Mar 31, 2027 | Apr 30, 2027 | May 31, 2027 |
Ruby 3.2 |
| Amazon Linux 2 | Mar 31, 2026 | Apr 30, 2026 | May 31, 2026 |
Open theLambda console.
ChooseCreate function.
Configure the following settings:
Function name: Enter a name for the function.
Runtime: ChooseRuby 3.4.
ChooseCreate function.
The console creates a Lambda function with a single source file namedlambda_function.rb
. You can edit this file and add more files in the built-in code editor. In theDEPLOY section, chooseDeploy to update your function's code. Then, to run your code, chooseCreate test event in theTEST EVENTS section.
Thelambda_function.rb
file exports a function namedlambda_handler
that takes an event object and a context object. This is thehandler function that Lambda calls when the function is invoked. The Ruby function runtime gets invocation events from Lambda and passes them to the handler. In the function configuration, the handler value islambda_function.lambda_handler
.
When you save your function code, the Lambda console creates a .zip file archive deployment package. When you develop your function code outside of the console (using an IDE) you need tocreate a deployment package to upload your code to the Lambda function.
The function runtime passes a context object to the handler, in addition to the invocation event. Thecontext object contains additional information about the invocation, the function, and the execution environment. More information is available from environment variables.
Your Lambda function comes with a CloudWatch Logs log group. The function runtime sends details about each invocation to CloudWatch Logs. It relays anylogs that your function outputs during invocation. If your function returns an error, Lambda formats the error and returns it to the invoker.
The version of the AWS SDK included in the Ruby runtime depends on the runtime version and your AWS Region. The AWS SDK for Ruby is designed to be modular and is separated by AWS service. To find the version number of a particular service gem included in the runtime you're using, create a Lambda function with code in the following format. Replaceaws-sdk-s3
andAws::S3
with the name of the service gems your code uses.
require 'aws-sdk-s3'def lambda_handler(event:, context:) puts "Service gem version: #{Aws::S3::GEM_VERSION}" puts "Core version: #{Aws::CORE_GEM_VERSION}"end
The Ruby 3.2 runtime supportsYJIT, a lightweight, minimalistic Ruby JIT compiler. YJIT provides significantly higher performance, but also uses more memory than the Ruby interpreter. YJIT is recommended for Ruby on Rails workloads.
YJIT is not enabled by default. To enable YJIT for a Ruby 3.2 function, set theRUBY_YJIT_ENABLE
environment variable to1
. To confirm that YJIT is enabled, print the result of theRubyVM::YJIT.enabled?
method.