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.
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.
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 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.
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:
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.