Creating Our First AWS Lambda Function
First, login to your AWS console and open the Lambda console. Click on “Create Function”, give it a name and choose “Create a custom role”.
Keep the role with the default policy document since our “Hello World” function will not need any special permission.
Click on “Allow” and make sure to choose “lambda_basic_execution” as a role.
Now let’s add this Node.js Hello World function:
exports.handler = (event, context, callback) => {
// Succeed with the string "Hello world!"
callback(null, 'Hello world!');
};
Click on “Test”:
Our test event, once executed will call the Lambda Hello World function. Click on “Create” followed by “Test” and you will notice the execution results:
You can use another language like Python. In order to do this, let’s first remove the first function, so go back to your dashboard and click on “Action” then “Delete”:
Now, create a new function and choose Python 3.6 as a runtime.
The Python Hello World function is the following:
def lambda_handler(event, context):
return 'Hello from Lambda'
This function will return the string “Hello from Lambda” and exits.
Note: Note that the function handler should be configured correctly in this form file_name.function_name
The file name should not also contain the extension of the file.
In my example, my file is called lambda_function.py
and my function is defined as lambda_handler
. The handler will be:
lambda_function.lambda_handler