Skip to content

Latest commit

 

History

History
82 lines (52 loc) · 5.11 KB

lambda-ruby.md

File metadata and controls

82 lines (52 loc) · 5.11 KB

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.

Lambda supports the following Ruby runtimes.

Ruby runtimes

Name Identifier SDK for Ruby Operating system Architectures
Ruby 2.7 ruby2.7 3.0.1 Amazon Linux 2 x86_64, arm64
Ruby 2.5 ruby2.5 3.0.1 Amazon Linux x86_64

Note
For end of support information about Ruby 2.5, see Runtime support policy.

Lambda functions use an execution role to get permission to write logs to Amazon CloudWatch Logs, and to access other services and resources. If you don't already have an execution role for function development, create one.

To create an execution role

  1. Open the roles page in the IAM console.

  2. Choose Create role.

  3. Create a role with the following properties.

    • Trusted entityLambda.
    • PermissionsAWSLambdaBasicExecutionRole.
    • Role namelambda-role.

    The AWSLambdaBasicExecutionRole policy has the permissions that the function needs to write logs to CloudWatch Logs.

You can add permissions to the role later, or swap it out for a different role that's specific to a single function.

To create a Ruby function

  1. Open the Lambda console.

  2. Choose Create function.

  3. Configure the following settings:

    • Namemy-function.
    • RuntimeRuby 2.7.
    • RoleChoose an existing role.
    • Existing rolelambda-role.
  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 Lambda Functions 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 SDE) 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.
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.

Topics