Discover AWS Lambda Basics to Run Powerful Serverless Functions

Author:Murphy  |  View: 27030  |  Time: 2025-03-22 20:11:07

This article walks you through how I got started using AWS Lambdas. The article aims to show you how to set up an AWS Lambda function and also to show you my problem-solving approach, figuring out how to set up a Lambda function for the first time. Hopefully, this will show you how to approach new problems within computer science and figure out a way to solve them. Being able to figure out problems yourself is a crucial skill within programming, and developing this skill is one of the main motivations of this article.

This tutorial will show you how to set up an AWS Lambda function, and also how I approached the problem of figuring out how to do that for the first time. Image by ChatGPT.

Motivation

My motivation for this tutorial, and similar ones in the future, is to learn new concepts that are important for me as a data scientist. One of the most important parts of being a data scientist is continuously updating your knowledge. I realized I had a limited understanding of deploying functions to the cloud, which is important if you, for example, want to host your machine learning model. I, therefore, set out to learn how I can deploy functions using AWS Lamba functions. Another important motivation for this tutorial is to show you my approach to problem-solving, as I will be working on a task I have little prior knowledge of. This article should, therefore, be helpful in showing you how you can figure out solutions to your own problems within programming.

Table of Contents

· Motivation · Table of Contents · Prerequisites · Setting up a Lambda in the AWS console · Setting up and invoking Lambdas from Python How I approached setting up AWS Lambdas for the first timeLambda function fileZipping fileLambda function utility file · Conclusion

Prerequisites

  • Simple Python knowledge
  • Basic AWS Console knowledge

A prerequisite for this tutorial is simple Python knowledge and some knowledge of the AWS platform. I have worked a bit with the AWS platform before, for example, with adding users, giving rights to users, and so on. These topics will, therefore, not be covered in this tutorial, though you can find documentation online showing you how to perform these tasks.

Setting up a Lambda in the AWS console

I used the following steps to set up a Lambda function in the console:

  1. Google about setting up a Lambda function
  2. After finding a good tutorial, I decided to follow that tutorial as long as possible. This is only possible because a high-quality tutorial was available for the exact problem I wanted to solve
  3. Verify the solution works as expected
  4. Play around with the code, and Lambda functions to ensure I understand how Lambda functions work

To figure out how to set up a Lambda function, I start by Googling: "how to set up a Lambda function AWS", and the first search result is the AWS tutorial on setting up your first Lambda function. I checked out the tutorial and discovered that it seems to discuss the problem I am addressing. I do notice, however, that the tutorial shows you how to set up a Lambda in the AWS console and not from the AWS CLI or AWS Python SDK. I decided, however, to continue following the tutorial to learn the basics of setting up a Lambda function, and after doing this, I will continue to do the same from the AWS Python SDK instead (see the next section).

You can either follow the tutorial and skip to the next section of this article or continue reading this article. In this section, I follow the tutorial on setting up a lambda in the cloud. In the next section, I will learn to set up a Lambda from Python code itself using AWS credentials.

To create a Lambda function, log into the AWS console, search for Lambda, and press create a function. Make sure to Author the function from scratch, give it a name, and choose your preferred runtime between Node and Python. I will be using Python for this tutorial. You should keep the architecture variable to x86_64.

The code source tab you will see after creating a function. Image by the author.

After creating the function, you can go to the code source tab and look at the Lambda function. The _lambdahandler is the function that is invoked when using the Lambda, and you should, therefore, include all the code for your lambda within that function. I will use a simple addition script to show you how to use Lambda and how the Lambda function can take in parameters.

The code below will show you how to make an addition lambda (add to numbers together), which should be used in the _lambdafunction Python file:

First, use a logger to print out information to the console:

import json
import logging

logger = logging.getLogger()
logger.setLevel(logging.INFO)

Then, make the simple addition function:


def addition(a, b):
    return a+b

And use the addition function in the _lambdahandler function:

def lambda_handler(event, context):
    a = event['a']
    b = event['b']
    result = addition(a, b)

    logger.info(f"Lambda function name: {context.function_name}")
    logger.info(f"Adding {a}+{b}={result}")

    # return the calculated area as a JSON string
    data = {"result": result}
    return json.dumps(data)

Make sure to press the deploy button after modifying the code.

The function takes in the variables a and b, from an event (I will show you how to use this soon), runs the addition function, logs the important values (so we can view them in the console later), and returns the result as a JSON object.

To test if the function works, we can now go to the Test page (besides the Code page above the interpreter), create a new test, and set the event JSON to:

{
  "a": 3,
  "b": 4
}

You can now go back to the Code page and press the Test button. This should result in the output you see below:

Output after testing the Lambda function you created. You can see the result from adding variables 3 and 4, which is 7, and you can also see logs from the function invocation, like the name of the Lambda function, the duration of the function call, and so on. Image by the author.

If you want to look further into the logs of the function call, you can search for Cloudwatch in the AWS console. Press the Logs dropdown menu to the left, and then press the Log groups that appear in the dropdown menu. Here, you can press the log stream of the function, which will show you the logs shown in the image above, as well as the logs for older function invocations if you invoked the function several times.

Finally, I recommend you delete your Lambda function if you will not use it anymore.

  • You can delete the Lambda function by going to the Lambda page on the AWS console, marking your function, pressing actions, and then clicking delete.
  • You can delete the Log group by going to Cloud Watch, selecting the Logs dropdown menu, selecting the Log groups, marking your log group, and deleting it.
  • You can delete the execution role by searching Roles in the AWS console, marking the role which is named "-role-", and pressing delete.

Setting up and invoking Lambdas from Python

This section will be divided into the different files you need to set up and invoke a Lambda function in Python. I will only be showing functions contained within .py files, following my new workflow, which you can read about in my TowardsDataScience article on making your data science / ML engineer workflow more effective, linked below:

How to Make Your Data Science/ML Engineer Workflow More Effective

You can find all the files in my GitHub repository.

I start off with a subsection about how I approached figuring out how to use AWS Lambdas.

How I approached setting up AWS Lambdas for the first time

Setting up all of this code was not straightforward, as it wasn't contained within a single tutorial. To understand Lambdas in AWS and to figure out how to do it, I mostly used the following three tools:

  1. AWS documentation for Python SDK
  2. ChatGPT
  3. Online tutorials, for example, from Medium
  • Each has its upsides and downsides. The AWS documentation is usually very good and, for example, contains information about all the functions you want to use. Unfortunately, however, the documentation does not contain tutorials for this exact use case of setting up a Lambda function with the Python SDK, which is why I also use two other tools.
  • ChatGPT can work very well in some situations, as it can provide context-specific answers and quick responses. However, it is also a bit outdated (the last model was trained sometime in 2023), and the web search does not always yield the desired results. Furthermore, ChatGPT sometimes makes mistakes.
  • Online tutorials are super useful tools for the use case I am currently working with. If you are working on something many others have done before you (for example, setting up a Lambda function), there is likely to be a good tutorial for it online. The downsides of online tutorials, however, are that they can be outdated (for example, if the AWS console has changed, commands are outdated, and so on), and that some online sources are not reliable.

Lambda function file

First, you can create the file you want to make a Lambda function of. This will be a multiplication function to keep this article simple. Since I already discussed writing this function earlier, I will show the code I used.

import json
import logging

logger = logging.getLogger()
logger.setLevel(logging.INFO)

def lambda_handler(event, context):

 a, b = event['a'], event['b']
 result = multiplication(a,b)

 logger.info(f"Multiplication of {a} and {b} is {result}")
 logger.info("Dette er en ny linje")
 result_object = {
  "result": result
 }
 return json.dumps(result_object)

def multiplication(a,b):
 return float(a) * float(b)

Remember to note the name of the file for later. I named my file "eivinds_lambda_function.py".

Zipping file

You need to zip the file to deploy the code above to a Lambda function. You can do this with the following code:

import zipfile

file_to_zip = 'eivinds_lambda_function.py'
zip_file_name = 'function.zip'

with zipfile.ZipFile(zip_file_name, 'w') as zipf:
    zipf.write(file_to_zip)

Which saves your Python code to a file called function.zip.

Lambda function utility file

Create a file called lambda_function_utility, which will contain the code you need to create, update, and delete a Lambda function. First, I define the imports and load environment variables:

import boto3
from dotenv import load_dotenv
load_dotenv()
import os
import json

I then load 3 variables:

ROLE = os.getenv("ROLE")
LAMBDA_FUNCTION_NAME="eivindsPythonLambdaFunction"
lambda_client = boto3.client('lambda', region_name='eu-north-1')
  • You can set up the ROLE variable by going to Roles in the AWS console and setting up a new role called, for example, UseLambdaFunctions. Make sure you select Lambda as a use case for the role. After creating the role, go to the page containing the role and copy the ARN, which is the value of the ROLE variable (the format of the ARN is "arn:aws:i am::role/").
  • The _LAMBDA_FUNCTIONNAME variable can be whatever you prefer.
  • The _lambdaclient variable is your AWS client, which I will assume you have already set up. If not, you can read about setting it up in the AWS documentation.

You can then make a function to create a lambda function. I first found out how to use this function with ChatGPT, but I discovered an error with regard to the Handler variable. I therefore Googled for the documentation of the function to read more about how to use it. The other functions below are also made using ChatGPT, and I then check out each function's documentation to ensure they are invoked correctly.

def create_lambda_function():
 with open('function.zip', 'rb') as f:
  zipped_code = f.read()

 response = lambda_client.create_function(
  FunctionName=LAMBDA_FUNCTION_NAME,
  Runtime='python3.12',
  Role=ROLE,
  Handler='eivinds_lambda_function.lambda_handler', # NOTE this must be the path to the handler function (so: .lambda_handler)
  Description='This is a lambda function created using boto3 which performs multiplication<<<<<<<<<<<<<<<',
  Code=dict(ZipFile=zipped_code),
 )
 return response

Remember that the Handler variable needs to refer to your lambda_handler function. This is done by setting the Handler variable to .lambda_handler.

If you want to update the code of your function, you first modify the actual function (in my case, the code inside _eivinds_lambdafunction.py), zip the file again, and run the code below:

def update_lambda_function():
 with open('function.zip', 'rb') as f:
  zipped_code = f.read()

 response = lambda_client.update_function_code(
  FunctionName=LAMBDA_FUNCTION_NAME,
  ZipFile=zipped_code,
  Publish=True
 )
 return response

You can also delete a Lambda function with the code below:

def delete_lambda_function():
 response = lambda_client.delete_function(
  FunctionName=LAMBDA_FUNCTION_NAME
 )
 return response

To get the status of the Lambda function, you can use this function:

def check_status(function_name = LAMBDA_FUNCTION_NAME):
 response = lambda_client.get_function(FunctionName=function_name)
 return response

And finally, you can invoke the lambda function with the code below. Make sure to update the payload if your lambda function takes in different parameters:

def invoke_lambda_function():
 payload = {"a": 5, "b": 10}
 response = lambda_client.invoke(
  FunctionName=LAMBDA_FUNCTION_NAME,
  InvocationType='RequestResponse',
  Payload=json.dumps(payload)
 )
 return response['Payload'].read().decode('utf-8')

If you run this for the multiplication code I wrote above, your output should be:

'"{"result": 50.0}"'

If you get a 403 forbidden, you should ensure the IAM user you are working with has the lambda:InvokeFunctionUrl permission. I discovered this after getting a 403 forbidden for my request, and Googling why invocation of Lambda function gives 403 error.

Conclusion

I have discussed how I set up an AWS Lambda function in this tutorial. This is the first time I have set up such a function, and throughout this article, I discussed how I approached setting up a Lambda function for the first time. The article's goal was to teach you how to set up an AWS Lambda function and, even more importantly, how to approach a problem you haven't solved before and figure out the solution.

Tags: Artificial Intelligence AWS Cloud Computing Hands On Tutorials Lambda

Comment