Building Lambda functions with Ruby - AWS Lambda

Building Lambda functions with Ruby

You can run Ruby code in AWS Lambda. Lambda provides runtimes 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, see Runtime-included SDK versions.

Lambda supports the following Ruby runtimes.

Ruby
Name Identifier Operating system Deprecation date Block function create Block function update

Ruby 3.3

ruby3.3

Amazon Linux 2023

Ruby 3.2

ruby3.2

Amazon Linux 2

To create a Ruby function
  1. Open the Lambda console.

  2. Choose Create function.

  3. Configure the following settings:

    • Function name: Enter a name for the function.

    • Runtime: Choose Ruby 3.2.

  4. Choose Create function.

  5. To configure a test event, choose Test.

  6. For Event name, enter test.

  7. Choose Save changes.

  8. To invoke the function, choose Test.

The console creates a Lambda function with a single source file named lambda_function.rb. You can edit this file and add more files in the built-in code editor. To save your changes, choose Save. Then, to run your code, choose Test.

Note

The Lambda console uses AWS Cloud9 to provide an integrated development environment in the browser. You can also use AWS Cloud9 to develop Lambda functions in your own environment. For more information, see Working with AWS Lambda functions using the AWS Toolkit in the AWS Cloud9 user guide.

The lambda_function.rb file exports a function named lambda_handler that takes an event object and a context object. This is the handler 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 is lambda_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 to create a deployment package to upload your code to the Lambda function.

Note

To get started with application development in your local environment, deploy one of the sample applications available in this guide's GitHub repository.

Sample Lambda applications in Ruby
  • blank-ruby – A Ruby function that shows the use of logging, environment variables, AWS X-Ray tracing, layers, unit tests and the AWS SDK.

  • Ruby Code Samples for AWS Lambda – Code samples written in Ruby that demonstrate how to interact with AWS Lambda.

The function runtime passes a context object to the handler, in addition to the invocation event. The context 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 any logs that your function outputs during invocation. If your function returns an error, Lambda formats the error and returns it to the invoker.

Runtime-included SDK versions

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. Replace aws-sdk-s3 and Aws::S3with 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

Enabling Yet Another Ruby JIT (YJIT)

The Ruby 3.2 runtime supports YJIT, 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 the RUBY_YJIT_ENABLE environment variable to 1. To confirm that YJIT is enabled, print the result of the RubyVM::YJIT.enabled? method.

Example — Confirm that YJIT is enabled
puts(RubyVM::YJIT.enabled?()) # => true