The automatically generated code for an AWS Lambda function using the Ruby 2.7 runtime looks like this:

File: lambda_function.rb (original version)
require 'json'

def lambda_handler(event:, context:) (1)
    # TODO implement
    { statusCode: 200, body: JSON.generate('Hello from Lambda!') } (2)
end
1 The lambda_handler function has two parameters:
  • event: This parameter is a hash that is used to pass in event data to the handler. It contains, among others, the information detailed in the following table:

    Table 1. Contents of Event
    Keys Associated Value
    event['version']

    A string with the current version of this event object. Should be '2.0' at the time of this writing.

    event['queryStringParameters']

    A hash with the query string parameters (only if it actually has any).

    event['headers']

    A hash with all the headers from the request.

    event['body']

    A string with the request body (only if it actually has one).

    event['requestContext']['domainName']

    A string with the domain name of the server where the AWS Lambda function is hosted.

    event['requestContext']['http']['method']

    A string with the request HTTP method (GET or POST, for example).

    event['requestContext']['http']['path']

    A string with the request path (for example /some_resource).

    event['requestContext']['http']['protocol']

    A string with the HTTP version used by the request (HTTP/1.1, for example).

  • context: This parameter provides runtime information to your handler. Its type is LambdaContext.

2 The handler should return a hash from which the HTTP response will be constructed.
Table 2. Hash Keys for HTTP Response
Key Description
:statusCode

An integer number with the HTTP response status code.

:body

A string with the response body. Typically, the result of calling JSON.generate method to produce JSON output.

:headers

This is an optional key, but if provided it should refer to a hash with additional response headers.