AWS Lambda Python

  • Topics

Working with AWS Lambda in Python

The 2020 Stack Overflow Developer Survey named Python one of the most beloved languages by survey respondents. Python has a strong foothold in a number of different industries, from web development to artificial intelligence and data science. It’s only natural, then, that many developers would like to rely on Python when working with their serverless functions. In this article we’ll discuss using Python with AWS Lambda, exploring the process of testing and deploying serverless Python functions.

Review of AWS Lambda

As a brief refresher, AWS Lambda is a Function-as-a-Service offering from Amazon Web Services. AWS Lambda essentially created the service in 2014 with the launch of Lambda. AWS Lambda provides on-demand execution of code without the need for an always-available server to respond to the appropriate request. AWS Lambda functions are run in containers that are dynamically created and deleted as needed based upon your application’s execution characteristics, allowing you to pay for only the compute you use rather than the constant availability of services like EC2.

Restrictions in AWS Lambda for Python

Given that the power of serverless technology is in the uniformity of its containers, there are some understandable restrictions on the runtime and environment itself. The first is minor and related to how Lambda deploys code itself – namely, you’ll encounter extensive duplication of the deployed code itself. This is not in itself a problem, rather than an annoyance that can arise as your application’s complexity grows. Additionally, Lambdas only support specific Python runtimes – see the list here.

In terms of the execution environment, there are a couple of things to be aware of. The first is that you are only given limited access to the operating system layer, meaning that you cannot rely upon operating-system-level packages that might otherwise be available in a more traditional operating environment. The second thing to beware of is that Lambda containers are extremely short-lived, and are regularly recycled for use elsewhere in the system. This means you cannot debug your application based on operating system aspects, such as local file storage, without ensuring that the function context doesn’t exist. This makes post-fact identification of issues much more challenging without dedicated effort up-front.

Writing AWS Lambda Python Functions

Writing Python code for AWS Lambda is not significantly different from writing Python for other types of applications. Lambda applications, when invoked, call a handler function that has the same prototype for every Lambda trigger:

def lambda_handler(event, context):

This function is called each time your Lambda function is invoked. The two parameters are provided by the AWS Lambda infrastructure itself and contain valuable information related to the invocation of the function, and the current execution context. The event parameter contains details on the invocation event and is where any custom arguments you wish to send to your function will appear. The context parameter contains information on your Lambda functions’ execution context, including information about resource usage quotas that you can use to adjust your functions’ operation.

Tips and Tricks

AWS Lambda functions provide a powerful tool in developing data flow pipelines and application request handlers without the need for dedicated server resources, but these functions are not without their downside. The following tips and tricks may help you navigate the minefield and find serverless success:

  • Make extensive use of unit testing. Given the distributed nature of serverless functions, having verification in place is important for peace of mind when deploying your functions that run infrequently. While unit and functional testing cannot fully protect against all issues, it gives you the confidence you need to deploy your changes.
  • Beware of time limits. AWS Lambda functions contain inherent time limits in execution. These can go as high as 900 seconds but defaults to 3. If your function is likely to require long run times, ensure you configure this value properly.
  • Leverage the pattern. It’s easy enough to take a simple Flask app and translate it into a monoservice Lambda function, but there are a lot of potential efficiency gains to be made from moving a step or two beyond the basics. Take the time to rebuild your application to take advantage of the event-driven nature of Lambda processing, and you can improve user experience with minimal effort.
  • Make use of third parties! It’s powerful to have everything under your control, but it can also be distracting and time-consuming. Tools like Lumigo can provide you automated tracing and online debugging, removing the need to develop these tools from your backlog.


AWS Lambda is a powerful tool for event-driven and intermittent workloads. The dynamic nature of Lambda functions means that you can create and iterate on your functionality right away, instead of having to spend cycles getting basic infrastructure running correctly and scaling properly. Python brings a powerful language with a robust environment into the serverless realm and can be a powerful tool with some careful application of best practices. In addition to general development practices focused on maintainability, tools like Lumigo can expand your serverless metrics reporting and give you the power you need to drive user value as quickly as possible.