AWS Lambdas on FastAPI – Simplifying Python Lambda Development and Testing with LoF

Emily Techscribe Avatar

·

AWS Lambdas on FastAPI – Simplifying Python Lambda Development and Testing with LoF

badge1 badge2 badge3workflow

Are you looking for a seamless way to develop and test your Python AWS Lambdas on FastAPI? Look no further! Introducing LoF (AWS Lambdas on FastAPI), a powerful command line tool designed to simplify the development and testing of Python lambdas on FastAPI. Whether you’re a seasoned developer or just starting with AWS Lambdas, LoF provides an intuitive and efficient solution.

How does it work?

LoF is incredibly easy to install. Simply run the following command:

bash
pip install lof

Once installed, you can use LoF to set up and run your lambdas by providing the path to your template YAML or JSON file. Alternatively, you can run LoF from the source directory, which assumes you have a template.yaml or template.json file.

To get started with LoF, run the following command:

bash
lof

If you have a custom path to your template file, you can pass it as an argument:

bash
lof --template example/template.yaml

LoF also allows you to exclude specific lambdas from being run. Simply provide the names of the lambdas to exclude as a comma-separated string:

bash
lof --template example/template.yaml --exclude=PostLambda2Function

To pass environment variables to your lambdas, you can use the –env flag. LoF supports two formats for environment variables: JSON format and ‘.env’ format. Examples of both files can be found in the example/ folder:

bash
lof --env=.env

or

bash
lof --env=vars.json

Autorizer Lambda

LoF allows you to emulate the behavior of an Authorizer lambda using the –proxy-lambdas flag. By passing the name of your Authorizer lambda from the CloudFormation template, you can create a proxy where requests will pass through the specified lambdas before reaching the target lambda. Any return values from the proxy lambdas will update the “requestContext” key in the event. To use this feature, run the following command:

bash
lof --proxy-lambdas=LambdaAuthorizer

If you need to use multiple proxy lambdas, you can pass their names as a comma-separated string:

bash
lof --proxy-lambdas=LambdaAuthorizer,CORS

Other settings

LoF provides additional settings to customize your experience. Here are some of the available options:

  • –port: Specify the port number to run LoF (default: 8000)
  • –host: Specify the host to run LoF (default: 0.0.0.0)
  • –workers: Set the number of unicorn workers to run (default: 1)
  • –debug / –no-debug: Enable or disable the debug flag for Uvicorn (default: True)
  • –reload / –no-reload: Enable or disable the reload flag for Uvicorn (default: False)

For a complete list of options, use the –help command.

Example

To get a taste of how LoF works, you can try using the AWS CloudFormation template.yaml and the lambdas provided in the example/ folder. This will give you a hands-on experience of setting up and running your Python lambdas on FastAPI using LoF.

Issues & Feature Requests

We welcome your feedback! If you encounter any issues or have any suggestions or feature requests, please don’t hesitate to open an issue on the LoF repository. We value your input and will address any problems or requests as soon as possible.

Problem Context

When working on projects with a large number of AWS Lambdas, the process of setting up and running them can be time-consuming and challenging, especially with tools like SAM local. LoF was developed to address some of the common issues faced by developers, providing a fast and efficient way to develop and test Python lambdas on FastAPI.

Some of the key challenges faced by developers include:

  1. Inability to skip certain lambdas from the configuration.
  2. Lengthy build and rebuild times for Docker containers when using tools like SAM.

With LoF, these issues are resolved, making it easier than ever to work with AWS Lambdas in local environments, including virtual desktop infrastructure (VDI) setups.

Changelog

v0.5.5
– Fixed issue with autorizer response population in event context.

v0.5.4
– Fixed issue with autorizer response population in event context.

v0.5.3
– Fixed issues related to missing pathParams in event and incorrect authorizer context.

v0.5.2
– Bug fixes.

v0.5.0
– Added AWS Context object, which currently includes mock values. Values from the config will be added in future versions.

v0.4.1
Features:
– Added option –proxy-lambdas, allowing you to specify lambdas that will act as middleware request handlers. These lambdas can be used for authorization or to implement CORS functionality.

Fixes:
– Fixed paths with symbols ‘-.’ causing issues when using 1 or more workers.

v0.3.0
– Added the ability to run multiple workers using the –workers flag. This allows for faster local server performance and enables lambdas to call other lambdas directly.
– Added the –reload flag to automatically reload the server when code changes.
– Added support for CloudFormation templates in JSON format.

v0.2.3
– Added the ability to send port and host to start several instances simultaneously.

v0.2.2
– Updated README.md.
– Fixed issue with lambdas in the template that do not have events with a path (e.g., S3 triggered lambdas).
– Fixed issue with status code 204, now correctly returning a response with no failures.
– Added some tests.

v0.2.1
– LoF no longer try/excepts lambda errors.

v0.2.0
– Fixed status_code resending from lambda and JSON body response.

v0.1.0
– First version of Lambdas on FastAPI.

With LoF, developing and testing your Python AWS Lambdas on FastAPI is now simpler and more efficient than ever before. Say goodbye to time-consuming setups and embrace a streamlined development workflow. Try out LoF today and experience the power and convenience it brings to AWS Lambda development.

Source: LoF Repository

Leave a Reply

Your email address will not be published. Required fields are marked *