Movatterモバイル変換


[0]ホーム

URL:


Building Lambda functions with Python - AWS Lambda
DocumentationAWS LambdaDeveloper Guide
Runtime-included SDK versionsExperimental features in Python 3.13Response formatGraceful shutdown for extensions

Building Lambda functions with Python

You can run Python code in AWS Lambda. Lambda providesruntimes 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, seeRuntime-included SDK versions.

Lambda supports the following Python runtimes.

NameIdentifierOperating systemDeprecation dateBlock function createBlock function update

Python 3.13

python3.13

Amazon Linux 2023

Jun 30, 2029

Jul 31, 2029

Aug 31, 2029

Python 3.12

python3.12

Amazon Linux 2023

Oct 31, 2028

Nov 30, 2028

Jan 10, 2029

Python 3.11

python3.11

Amazon Linux 2

Jun 30, 2026

Jul 31, 2026

Aug 31, 2026

Python 3.10

python3.10

Amazon Linux 2

Jun 30, 2026

Jul 31, 2026

Aug 31, 2026

Python 3.9

python3.9

Amazon Linux 2

Dec 15, 2025

Jan 15, 2026

Feb 15, 2026

To create a Python function
  1. Open theLambda console.

  2. ChooseCreate function.

  3. Configure the following settings:

    • Function name: Enter a name for the function.

    • Runtime: ChoosePython 3.13.

  4. ChooseCreate function.

The console creates a Lambda function with a single source file namedlambda_function. You can edit this file and add more files in the built-in code editor. In theDEPLOY section, chooseDeploy to update your function's code. Then, to run your code, chooseCreate test event in theTEST 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 anylogs that your function outputs during invocation. If your function returns an error, Lambda formats the error and returns it to the invoker.

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 boto3import botocoredef lambda_handler(event, context): print(f'boto3 version:{boto3.__version__}') print(f'botocore version:{botocore.__version__}')

Experimental features in Python 3.13

The Python 3.13 managed runtime and base images do not support the following experimental features. You cannot enable these features using runtime flags. To use these features in a Lambda function, you must deploy acustom runtime orcontainer image containing your own build of Python 3.13.

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 or later Python runtimes, 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 withexternal extensions. When Lambda shuts down an execution environment, it sends aSIGTERM signal to the runtime and then aSHUTDOWN event to each registered external extension. You can catch theSIGTERM 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, seeUnderstanding the Lambda execution environment lifecycle. For examples of how to use graceful shutdown with extensions, see theAWS Samples GitHub repository.

Tracing
Handler

[8]
ページ先頭

©2009-2025 Movatter.jp