Building Lambda functions with Python - AWS Lambda

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.

Lambda supports the following Python runtimes.

Note

End of support for the Python 2.7 runtime starts on July 15, 2021. For more information, see Runtime support policy.

Python runtimes
Name Identifier AWS SDK for Python Operating system

Python 3.8

python3.8

boto3-1.17.100 botocore-1.20.100

Amazon Linux 2

Python 3.7

python3.7

boto3-1.17.100 botocore-1.20.100

Amazon Linux

Python 3.6

python3.6

boto3-1.17.100 botocore-1.20.100

Amazon Linux

Python 2.7

python2.7

boto3-1.17.100 botocore-1.20.100

Amazon Linux

To create a Python function

  1. Open the Lambda console.

  2. Choose Create function.

  3. Configure the following settings:

    • Namemy-function.

    • RuntimePython 3.8.

    • RoleChoose an existing role.

    • Existing rolelambda-role.

  4. Choose Create function.

  5. To configure a test event, choose Test.

  6. For Event name, enter test.

  7. Choose Create.

  8. To invoke the function, choose Test.

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. To save your changes, choose Save. Then, to run your code, choose Test.

Note

The Lambda console uses AWS Cloud9 to provide an integrated development environment in the browser. You can also use AWS Cloud9 to develop Lambda functions in your own environment. For more information, see Working with Lambda Functions in the AWS Cloud9 user guide.

The lambda_function file exports a function named lambda_handler that takes an event object and a context object. This is the handler function that Lambda calls when the function is invoked. The Python function runtime gets invocation events from Lambda and passes them to the handler. In the function configuration, the handler value is lambda_function.lambda_handler.

Each time you save your function code, the Lambda console creates a deployment package, which is a .zip file archive that contains your function code. As your function development progresses, you will want to store your function code in source control, add libraries, and automate deployments. Start by creating a deployment package and updating your code at the command line.

Note

To get started with application development in your local environment, deploy one of the sample applications available in this guide's GitHub repository.

Sample Lambda applications in Python

  • blank-python – A Python function that shows the use of logging, environment variables, AWS X-Ray tracing, layers, unit tests and the AWS SDK.

The function runtime passes a context object to the handler, in addition to the invocation event. The context object contains additional information about the invocation, the function, and the execution environment. More information is available from environment variables.

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.