AWS Cloud Operations Blog

Leverage Amazon Q to upgrade Lambda runtime functions

Cloud operations are at the heart of every organization. Operating in the cloud allows IT teams to focus on business outcomes, optimizing IT processes while accelerating software development and innovation. These days, it is no longer a question if your organization is moving to the cloud, but how quickly you can move with security and safety. Companies of every size have realized benefits from moving to AWS, including lower cost of operations, stronger operational resilience, and a smaller carbon footprint. Organizations moving large portfolios of legacy applications, ITIL-based processes, and data centers to the cloud want a secure and efficient way to manage and govern their infrastructure and applications in the cloud, on-premises, and at the edge.

AWS Lambda is a serverless compute service that runs your code without server management. It supports multiple languages through the use of runtimes. A runtime provides a language-specific environment that relays invocation events, context information, and responses between Lambda and the function. Each major language version has a unique runtime identifier (e.g., python3.7, Python 3.8). To configure a function to use a new major language version, you need to change the runtime identifier. Since AWS Lambda cannot guarantee backward compatibility between major versions, this is a customer-driven operation.

In this blog post, we cover how to leverage Amazon Q developer to upgrade the Lambda Function runtime version from Python 3.7 to latest version 3.12.The latest version can provide significant value to by ensuring you are running their applications on the most recent and secure version of the programming language. The launch of Amazon Q Developer enables developers to leverage AWS services more efficiently in their day to day tasks. This generative AI-powered assistant enables developers to efficiently build applications, research best practices, troubleshoot issues, and resolve errors on AWS.

Lambda’s standard deprecation policy is to deprecate a runtime when any major component of the runtime reaches the end of community long-term support (LTS), and security updates are no longer available. After a runtime is deprecated, you’re still able to create and update functions for a limited period. For more details on supported Lambda runtimes and projected deprecation dates, refer this link

Identify Deprecated AWS Lambda function runtime:

Below are some ways to find the list of deprecated or soon-to-be deprecated lambda function runtime:

  1. Trusted Advisor : Enterprise Support customers can view Trusted Advisor Recommendations to view Security – AWS Lambda Functions Using Deprecated Runtimes
  2. AWS Health Dashboard: Check your AWS Health Dashboard often to identify events that might affect your account or applications. For example, you might receive an event notification about your resources, such as an Amazon Elastic Compute Cloud (Amazon EC2) instance that needs to be updated, or soon-to-be deprecated lambda runtimes.
  3. Programmatically list the deprecated functions. Use this link to Create lambda function and use below sample code to list the functions. The output will display a list of functions using outdated runtimes:
import boto3

# Create an AWS Lambda client
lambda_client = boto3.client('lambda')

def is_runtime_deprecated(runtime):
	# Define a list of deprecated runtimes
	deprecated_runtimes = ['python2.7', 'python3.7','nodejs4.3', 'nodejs6.10', 'nodejs8.10', 'java8']

	# Check if the given runtime is in the list of deprecated runtimes
    return runtime in deprecated_runtimes

def lambda_handler(event, context):
	# Get a list of all Lambda functions
	response = lambda_client.list_functions(FunctionVersion='ALL')
	functions = response['Functions']
	
	# Iterate through the functions and check for deprecated runtimes
	deprecated_functions = []
	for function in functions:
		runtime = function['Runtime']
		if is_runtime_deprecated(runtime):
			deprecated_functions.append({
			'FunctionName': function['FunctionName'],
          	'Runtime': runtime,
            'FunctionArn': function['FunctionArn']
          })
	
	# Return the list of functions with deprecated runtimes
	return {
        'statusCode': 200,
        'body': {
        'deprecated_functions': deprecated_functions
        }
    }

Prerequisites

  1. Install Amazon Q Developer in your IDE
  2. Setting up Amazon Q Developer

Note: For additional information on the different pricing tiers, please refer to Amazon Q Developer pricing page

Walk-through

1. Log into AWS Management Console and open

2. Open the Lambda function with version is python 3.7. Copy the python code from the Lambda function and save it in a file as code_v1_37.py

Solution architecture diagram

Figure 1: SolutionArchitectureDiagram

3. Open VSCode and then open the folder where you saved previous file

Figure 2: VSCode window

Figure 2: VSCodeWindow

4. In the left navigation pane of VSCode click on the Amazon Q icon

AmazonQChatPanel

Figure 3: AmazonQChatPanel

5. Open a new or existing project and enter /dev in the Amazon Q chat panel, and the comment as Review the code from the file ‘code_v1_37.py’ and suggest the modifications to make it to run on Python 3.12 version and hit Enter. Once comment is passed to Amazon Q Developer, it will take few seconds to generate the complete plan on how to upgrade the code from Python 3.7 to 3.12. It will suggest the steps to update the code to run on Python 3.7 to 3.12.

Note : The code generated by Amazon Q is an initial draft to boost developer productivity. However, it requires thorough review and testing by the developer before implementation

6. Review the steps and make the changes manually by identifying where the suggestions have been made or click the button Generate code in the Amazon Q chat panel to generate modified code, then wait for few seconds to a minute for Amazon Q to generate the code.

 GenerateCode

Figure 4: GenerateCode

7. Click on the file in Amazon Q chat panel, it will open the updated code with file diff. Compare the files side-by-side to see the modifications suggested by Amazon Q. The code changes suggested by Amazon Q are marked in green.

NewCodeFileDiff

Figure 5: NewCodeFileDiff

8. In the Amazon Q chat panel, click the “`Insert Code“` button to merge the updated code to the original file.late, you can copy the code from VSCode and paste into the AWS Lambda Console, then click Deploy.

InsertCode

Figure 6: AmazonQInsertCodeConsole

Note: For upgrading AWS Lambda runtime with other programming languages, you can follow the same steps as above:

nodejs,nodejs6.10,nodejs4.3 edge, nodejs4.3,nodejs8.10, nodejs10.x, nodejs12.x,nodejs14.x,python2.7,python3.6,python3.7,ruby2.5,ruby2.7,go1.x

Conclusion

AWS Lambda is deprecating runtime versions for several programming languages. This post demonstrated how to use Amazon Q Developer to review and upgrade code from Python 3.7 to Python 3.12. You can also follow the similar steps to upgrade from Python 3.8 to Python 3.12. The post demonstrated how Amazon Q allows for seamless code updates to transition from older runtime versions to the most recently supported runtime.

About the Authors:

Snehal Nahar

Snehal Nahar is a Principal Technical Account Manager (Security Specialist) at AWS. She is passionate about building innovative solutions using AWS services to help customers achieve their business objectives. She enjoys spending time with family and friends, playing board games and watching TV

Ravindra Kori

Ravindra Kori is a Solutions Architect and GenAI ambassador at AWS based in Arlington, specializing in Cloud Operations and Serverless technologies. He works extensively with Enterprise and Startup segments, architecting solutions and facilitating AWS modernization and migrations. Outside of work, he finds joy in playing drums and spending quality time with family.