Skip to main content

Validate input and output for AWS Lambda handlers using Pydantic

Project description

pylambdic

pylambdic is a Python package that simplifies the process of validating input and output for AWS Lambda handlers using Pydantic. It automatically validates the input and output types of your Lambda function using Pydantic models, making it easier to ensure your function is working with the correct data.

Features

  • Automatic input and output validation using Pydantic models.
  • Simplified error handling for invalid input and output data.
  • Support for AWS Lambda context object.
  • Easy integration with existing AWS Lambda functions.

Installation

Install pylambdic using pip:

pip install pylambdic

Usage

To use pylambdic, simply import the handler decorator and apply it to your AWS Lambda function. You should also define your input and output types using Pydantic models.

Here's an example of how to use pylambdic:

from pydantic import BaseModel
import pylambdic

class InputModel(BaseModel):
    name: str
    age: int

class OutputModel(BaseModel):
    message: str

@pylambdic.handler
def my_lambda_handler(input_data: InputModel) -> OutputModel:
    return OutputModel(message=f"Hello {input_data.name}, you are {input_data.age} years old.")

In this example, the my_lambda_handler function expects an input event with name and age fields, and returns a response with a message field. pylambdic will automatically validate the input and output data against the InputModel and OutputModel Pydantic models.

If the input data is invalid, the Lambda function will return a 400 status code with a descriptive error message. If the output data is invalid, it will return a 500 status code with a descriptive error message.

You can also use the context object provided by AWS as follows:

from pydantic import BaseModel
import pylambdic

class InputModel(BaseModel):
    name: str
    age: int

class OutputModel(BaseModel):
    message: str
    request_id: str

@pylambdic.handler
def my_lambda_handler(input_data: InputModel, context) -> OutputModel:
    message = f"Hello {input_data.name}, you are {input_data.age} years old."
    request_id = context.aws_request_id
    return OutputModel(message=message, request_id=request_id)

Contributing

Contributions are welcome! Please feel free to submit issues and pull requests for consideration.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pylambdic-1.0.0.tar.gz (3.9 kB view details)

Uploaded Source

Built Distribution

pylambdic-1.0.0-py3-none-any.whl (3.6 kB view details)

Uploaded Python 3

File details

Details for the file pylambdic-1.0.0.tar.gz.

File metadata

  • Download URL: pylambdic-1.0.0.tar.gz
  • Upload date:
  • Size: 3.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.16

File hashes

Hashes for pylambdic-1.0.0.tar.gz
Algorithm Hash digest
SHA256 166ea785efc4b91b09d9af9aa80e92b1054b729a22a1d0abab4b31258954f390
MD5 be3f1500daca627c8d4f858d3820d063
BLAKE2b-256 587ebbb3d8ec2daf4a260ee3d7c143f629661f71cf1f8a1d914cb6a524656527

See more details on using hashes here.

File details

Details for the file pylambdic-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: pylambdic-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 3.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.16

File hashes

Hashes for pylambdic-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6776ec48982bacbc9ecd3df37f47e736b6a4b066409dd5267e5c88c7ecd435c2
MD5 6cbdd18a7b122a65b5af08ca00c454b5
BLAKE2b-256 314b956d1922e016589fbd659efac75b144fc185fbb838ff8d4193865f38cc00

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page