Upgrading Lambda function from Python 3.8 to Higher
Ashish Gajjar
Posted on November 16, 2024
This guide walks you through the process of upgrading AWS Lambda Python code from version 3.8 to 3.11. While there are several approaches to achieving this, I will focus on one method: deploying a new Lambda function
The reason for this upgrade is that AWS Lambda will deprecate Python 3.8 on October 14, 2024, in line with Python 3.8’s End-Of-Life (EOL), also scheduled for October 2024 [1]. Migrating to a newer version ensures continued support and access to the latest features and security updates.
Background
Our existing Lambda function is from an Get S3 Object which was an existing deployment provided by AWS. This python code was deployed with Python 3.8.
Create a New Lambda Function
The first option we will explore is deploying a new Lambda function and updating the existing code to be compatible with Python 3.11. One of the major changes when upgrading from Python 3.8 to Python 3.11 is the shift in how the print statement works. In Python 3.8, print was a statement, but in Python 3.11, it became a function. Since print is commonly used for debugging and logging, you’ll often encounter it in Python code. This change will require updating any legacy print statements to function calls in the new version.
The screen shot below is an example of the existing Lambda function definition. You will notice that the code is deployed as a “Zip” package type.
In Python 3, the print statement from Python 2 was replaced by the print() function, which means you need to use parentheses around what you want to print. If you want to print an exception error message (such as when catching an exception), you can print the exception object itself.
Here’s how you can print an exception using Python 3:
import traceback
try:
# Code that might raise an exception
1 / 0 # This will raise a ZeroDivisionError
except Exception as e:
# Print the exception details
print(f"An error occurred: {e}")
# Alternatively, you can also print the full traceback:
print("Full traceback:")
traceback.print_exc()
Explanation:
In Python 3, e is the exception object in the except block. To print its details, use print(f"An error occurred: {e}").
The f string allows for string interpolation, inserting the value of e into the string.
traceback.print_exc()
will print the full traceback of the exception if you want more detailed debugging information.
This is the modern way to handle and print errors in Python 3.
Step 1: Backup your existing function code
As a precaution with everything we do in the IT space it is good to start with a backup. On the top right corner of the screen there is an “Action” drop down list. Press the “Action” title and select “Download function zip”
Then we will select “Download deployment package”. This will down a Zip file with the original deployment package an any code changes that have occurred. Save this file somewhere were you can find it in the future, incase there are any issues.
Step 2: Create a new version of your code
Navigate to Lambda Functions. On the top of the screen you will see your existing functions. We will create a new function with the “Create function” orange button on the top right.
On the create screen we will give a new “Function name”, and select the “Runtime” of “Python 3.11” and in this case we will use the existing role from our previous Lambda function as we are going to be duplicating that code. Then press the “Create function” on the both
Runtime Changes are as below
Deploy a new Lambda function,
Next we will need to import your code as a Zip file or if you are using code that does not require supporting libraries you can add your code directly into the editor. While you are converting your code you will need to make sure you convert all of the print statements to the Python 3.11 version of a print.
After you have changed your lambda_function.py code to incorporate all of the Python 3 changes you will need to upload a Zip of the code. If you need help creating a zip file for python libraries that are not part of the AWS Lambda default footprint you can follow the following article
https://docs.aws.amazon.com/lambda/latest/dg/python-package.html
Also don’t forget to edit the basic settings if your code is taking a while to execute and you get a timeout message. The default for Lambda is 3 seconds
Step 3: Test your Code
We will then test the Lambda function by pressing the “Test” button on your “Code” screen for the Lambda function
Name your test, and provide any JSON data objects that you normally test your code with. This will give you an execution results tab that you can debug your code from.
Hope this helped migrate your Lambda function to Python 3 by creating a new Lambda function and changing the trigger to call your new Lambda function.
Conclusion:
Upgrading the Python version in an AWS Lambda function is beneficial for staying current with new features, performance improvements, and security updates. However, it requires careful testing of your codebase, dependencies, and Lambda configuration to avoid breaking changes and ensure a seamless transition. Following best practices like using Lambda versioning, testing in a staging environment, and monitoring post-upgrade performance will help ensure that the upgrade is successful and provides the expected benefits.
Posted on November 16, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.