Tuesday 3 March 2020

AWS LAMBDA Function Runtime upgrade from Python 2.7 runtime to 3.7.x



AWS LAMBDA Function Runtime upgrade from Python 2.7 runtime to 3.7.x

Why should this upgrade happen ?

Python has announced the 2 series run time has attained its EOL , The message from the python foundation is
We have decided that January 1, 2020, will be the day that we sunset Python 2. That means that we will not improve it anymore after that day, even if someone finds a security problem in it. You should upgrade to Python 3 as soon as you can.

For whom this is for ?

AWS lambda function users who is running on 2.7 framework

Getting ready for some action

1. Run command line utility 2to3

verify python 3 is the defaulted version on the operating system by typing in
$ python — version
Python 3.7.6
2to3 utility
2to3 is command line utlity that ships with python3's default insallation (This was tested on MAC OS) Alternatively if this is missing there are numerous ways to get this installed for windows or linux from google
Target the lambda function’s python file and run this utility so this utlity can upgrade the code as needed to Python 3 ‘s syntax.

If there is no change the utility outputs as below

$ 2to3 lambda_function.py
RefactoringTool: Skipping optional fixer: buffer
RefactoringTool: Skipping optional fixer: idioms
RefactoringTool: Skipping optional fixer: set_literal
RefactoringTool: Skipping optional fixer: ws_comma
RefactoringTool: No changes to lambda_function.py
RefactoringTool: Files that need to be modified:
RefactoringTool: lambda_function.py

If there is a change the Utility outputs as below

$ 2to3 lambda_function.py
RefactoringTool: Skipping optional fixer: buffer
RefactoringTool: Skipping optional fixer: idioms
RefactoringTool: Skipping optional fixer: set_literal
RefactoringTool: Skipping optional fixer: ws_comma
RefactoringTool: Refactored lambda_function.py
— — lambda_function.py (original)
+++ lambda_function.py (refactored)
@@ -1,5 +1,5 @@
def greet(name):
- print “Hello, {0}!”.format(name)
-print “What’s your name?”
-name = raw_input()
+ print(“Hello, {0}!”.format(name))
+print(“What’s your name?”)
+name = input()
greet(name)
RefactoringTool: Files that need to be modified:
RefactoringTool: lambda_function.py
Use -w keyword as an argument to write it to a file.
$ 2to3 -w lambda_function.py
RefactoringTool: Skipping optional fixer: buffer
RefactoringTool: Skipping optional fixer: idioms
RefactoringTool: Skipping optional fixer: set_literal
RefactoringTool: Skipping optional fixer: ws_comma
RefactoringTool: Refactored lambda_function.py
— — lambda_function.py (original)
+++ lambda_function.py (refactored)
@@ -1,5 +1,5 @@
def greet(name):
- print “Hello, {0}!”.format(name)
-print “What’s your name?”
-name = raw_input()
+ print(“Hello, {0}!”.format(name))
+print(“What’s your name?”)
+name = input()
greet(name)
RefactoringTool: Files that were modified:
RefactoringTool: lambda_function.py

2. Package the python file

The lambda python file has been replaced with all the python3 required syntax. Use the regular process to checkin the file to repository and build them with python3 boto3 libraries .

3 upload and deploy the build to s3 or deploy them using pipelines or terraform to test them on lambda with 3.7 runtime. This sucessfully worked in our tests.

4. Rollout the same process for every other lambda_function.py files which are due for an upgrade.

More references & usages for 2to3 utility can be found at this link

No comments:

Post a Comment