A quick way to test a Lambda function, is to add a little portion of code at the bottom of the script that tells it to run itself when it is the main script. The if statement means that the script will only run if it is ran directly vs being required as library.

So this will run the if statement:

ruby hi.rb # run directly

And this will not run the if statement:

myfile.rb:

require_relative "hi" # being required, so not ran directly
ruby myfile.rb # will not run script

Always forget the way to do it between languages, so writing it down for posterity.

Here are 3 ways to check if the script is being ran directly in: Ruby, Python and Node.

Code Examples

Ruby

def handle(event, context)
  p event
end

if __FILE__ == $0
  event = {hi: "world"}
  handle(event, {})
end

Output:

{:hi=>"world"}

Python

def lambda_handler(event, context):
  print(event)

if __name__ == "__main__":
  event = {'hi': 'world'}
  lambda_handler(event, {})

Output:

$ python hi.py
{'hi': 'world'}

Node

const handler = function(event, context, callback) {
  console.log(event);
}

if (require.main === module) {
  event = {'hi': 'world'}
  handler(event);
}

Output:

$ node hi.js
{ hi: 'world' }