Building Lambda functions with Ruby
You can run Ruby code in Amazon Lambda. Lambda provides runtimes for Ruby that run your code to process events. Your code runs in an environment that includes the Amazon SDK for Ruby, with credentials from an Amazon Identity and Access Management (IAM) role that you manage.
Lambda supports the following Ruby runtimes.
Ruby | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Name | Identifier | SDK | Operating system | Architectures | Deprecation (Phase 1) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Ruby 3.2 |
|
3.1.0 |
Amazon Linux 2 |
x86_64, arm64 |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Ruby 2.7 |
|
3.1.0 |
Amazon Linux 2 |
x86_64, arm64 |
Dec 7, 2023 |
To create a Ruby function
-
Open the Lambda console
. -
Choose Create function.
-
Configure the following settings:
-
Function name: Enter a name for the function.
-
Runtime: Choose Ruby 3.2.
-
-
Choose Create function.
-
To configure a test event, choose Test.
-
For Event name, enter
test
. -
Choose Save changes.
-
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 Amazon Cloud9 to provide an integrated development environment in the browser. You can also use Amazon Cloud9 to develop Lambda functions in your own environment. For more information, see Working with Amazon Lambda functions using the Amazon Toolkit in the Amazon 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, Amazon X-Ray tracing, layers, unit tests and the Amazon SDK. Ruby Code Samples for Amazon Lambda – Code samples written in Ruby that demonstrate how to interact with Amazon 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.
Enabling Yet Another Ruby JIT (YJIT)
The Ruby 3.2 runtime supports YJIT
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