Building Lambda functions with Python
You can run Python code in AWS Lambda. Lambda provides runtimes for Python that run your code to process events. Your code runs in an environment that includes the SDK for Python (Boto3), with credentials from an AWS Identity and Access Management (IAM) role that you manage. To learn more about the SDK versions included with the Python runtimes, see Runtime-included SDK versions.
Lambda supports the following Python runtimes.
Name | Identifier | Operating system | Deprecation date | Block function create | Block function update |
---|---|---|---|---|---|
Python 3.12 |
|
Amazon Linux 2023 |
Not scheduled |
Not scheduled |
Not scheduled |
Python 3.11 |
|
Amazon Linux 2 |
Not scheduled |
Not scheduled |
Not scheduled |
Python 3.10 |
|
Amazon Linux 2 |
Not scheduled |
Not scheduled |
Not scheduled |
Python 3.9 |
|
Amazon Linux 2 |
Not scheduled |
Not scheduled |
Not scheduled |
To create a Python function
-
Open the Lambda console
. -
Choose Create function.
-
Configure the following settings:
-
Function name: Enter a name for the function.
-
Runtime: Choose Python 3.12.
-
-
Choose Create function.
The console creates a Lambda function with a single source file named lambda_function
. You can edit this file and add more files in the built-in code editor. In the DEPLOY section, choose Deploy to update your function's code. Then, to run your code, choose Create test event in the TEST EVENTS section.
Your Lambda function comes with a CloudWatch Logs log group. The function runtime sends details about each invocation to CloudWatch Logs. It relays any logs that your function outputs during invocation. If your function returns an error, Lambda formats the error and returns it to the invoker.
Topics
- Runtime-included SDK versions
- Response format
- Graceful shutdown for extensions
- Define Lambda function handler in Python
- Working with .zip file archives for Python Lambda functions
- Deploy Python Lambda functions with container images
- Working with layers for Python Lambda functions
- Using the Lambda context object to retrieve Python function information
- Log and monitor Python Lambda functions
- AWS Lambda function testing in Python
- Instrumenting Python code in AWS Lambda
Runtime-included SDK versions
The version of the AWS SDK included in the Python runtime depends on the runtime version and your AWS Region. To find the version of the SDK included in the runtime you're using, create a Lambda function with the following code.
import boto3 import botocore def lambda_handler(event, context): print(f'boto3 version: {boto3.__version__}') print(f'botocore version: {botocore.__version__}')
Response format
In Python 3.12 and later Python runtimes, functions return Unicode characters as part of their JSON response. Earlier Python runtimes return escaped sequences for Unicode characters in responses. For example, in Python 3.11, if you return a Unicode string such as "こんにちは", it escapes the Unicode characters and returns "\u3053\u3093\u306b\u3061\u306f". The Python 3.12 runtime returns the original "こんにちは".
Using Unicode responses reduces the size of Lambda responses, making it easier to fit larger responses into the 6 MB maximum payload size for synchronous functions. In the previous example, the escaped version is 32 bytes—compared to 17 bytes with the Unicode string.
When you upgrade to Python 3.12, you might need to adjust your code to account for the new response format. If the caller expects escaped Unicode, you must either add code to the returning function to escape the Unicode manually, or adjust the caller to handle the Unicode return.
Graceful shutdown for extensions
Python 3.12 and later Python runtimes offer improved graceful shutdown capabilities for functions with external extensions. When Lambda shuts down an execution environment, it sends a SIGTERM
signal to the runtime and then a SHUTDOWN
event to each registered external extension. You can catch the SIGTERM
signal in your Lambda function and clean up resources such as database connections that were created by the function.
To learn more about the execution environment lifecycle, see Understand the Lambda execution environment lifecycle. For examples of how to use graceful shutdown with extensions, see the AWS Samples GitHub repository