Python Conference APAC 2024

Pythonic Ways to Build Serverless Apps in AWS and how to migrate from Frameworks
2024-10-27 , CLASS #3 - 4C
Language: English

Serverless applications have transformed software development and deployment. Building serverless applications with Python on AWS presents unique challenges, especially for developers accustomed to frameworks like Django, Flask, and FastAPI. This talk explores strategies to maintain the developer experience of these frameworks while gaining the benefits of serverless. We will cover key patterns and tools, such as Lambdalith, AWS Chalice, AWS Lambda Powertools, single-purpose function patterns, event-driven architecture, and Infrastructure as Code (IaC) tools.


Description

Pythonistas love their tools and frameworks like Django, Flask, and FastAPI, which make it incredibly convenient to build and deploy applications. The clean syntax and ease of defining routes and handling HTTP requests are significant draws. For example, Flask uses Python's decorator syntax to create routes in a clean and readable manner.

from flask import Flask

app = Flask(__name__)

@app.route('/submit', methods=['POST']) 
def submit(): 
    return 'Form Submitted!'

if __name__ == '__main__':
    app.run(debug=True)

Transitioning to a serverless architecture, while seeking its benefits, introduces several challenges, especially for teams accustomed to these beloved frameworks:

  1. Learning Curve: Shifting to serverless requires understanding new concepts such as event-driven architectures, Infrastructure as Code (IaC) tools like AWS CloudFormation, and new security best practices.

  2. Tooling and Debugging: Unlike traditional frameworks, serverless lacks dedicated tools for running a serverless REST API locally. Engineers must adapt to workarounds such as local testing and CLI commands for local invocations. Additionally, they need to learn cloud-native monitoring tools like AWS CloudWatch and X-Ray.

  3. Python's Limitations: Unlike Node.js, Python lacks advanced bundling and tree-shaking capabilities, leading to larger deployment packages and slower cold starts.

Despite these challenges, our goal is to achieve the same developer experience provided by frameworks while gaining the benefits of serverless. My talk will delve into these challenges and offer strategies to mitigate them, enabling a smooth transition to serverless for Python developers. Key strategies and patterns include:

  1. Lambdalith: Bundles all dependencies and application code into a single package, simplifying deployment but requiring careful management to avoid bloated package sizes.

  2. AWS Chalice: A Python microframework that simplifies deploying serverless applications with AWS Lambda through automated packaging and deployment.

  3. AWS Lambda Powertools: A suite of utilities enhancing the observability, logging, and manageability of AWS Lambda functions written in Python.

  4. Single-Purpose Function Pattern: Encourages creating Lambda functions that perform a single task, leading to simpler, more maintainable code and reduced deployment package size.

  5. Event-Driven Architecture: Utilizes AWS services like SQS, SNS, and EventBridge to build scalable and loosely coupled serverless applications.

  6. Infrastructure as Code (IaC): Uses tools like AWS CloudFormation and AWS CDK for defining and provisioning infrastructure, ensuring consistent and repeatable deployments.

Additionally, I am writing an article series on Medium that dives into these steps outlined in my talk. The first article is titled "The Pythonic Way to Build Serverless Apps with AWS Chalice and CDK: A Guide to Migrating from Micro-frameworks." This series provides detailed guidance on leveraging AWS Chalice and the AWS Cloud Development Kit (CDK) to build and migrate serverless applications efficiently.

By the end of this session, attendees will have a comprehensive understanding of best practices and tools for developing efficient and scalable serverless applications with Python on AWS, and how to transition smoothly from traditional frameworks they know and love while maintaining a great developer experience.

Arnel is an Senior Python Backend Engineer specializing in cloud-native and serverless applications in the AWS Cloud. He works at Elemnta, a Sydney-based Fintech Company that provides digital products specialized for the needs of financial advisors. He is deeply involved in the Python ecosystem and actively contributes to its growth and integration in software solutions. As the Community Lead of DurianPy, Davao's official Python User Group, he is dedicated to fostering innovation and collaboration to drive advancements in Davao's Tech Landscape.