Skip to main content

Remote call AWS Lambda functions directly that have API Gateway integration

Project description

Call Serverless

call-serverless is a Python library designed to remotely invoke AWS Lambda functions that have API Gateway integration. It simplifies the process of invoking Lambda functions with HTTP-like requests, providing both synchronous and asynchronous methods to send requests and handle responses.

Features

  • Invoke AWS Lambda Functions: Call Lambda functions via their ARN using HTTP methods (GET, POST, PUT, DELETE)
  • Simulate API Gateway Requests: Send requests with customizable paths, headers, stages, and body payloads
  • Async Support: Built-in support for asynchronous operations using aiobotocore
  • Simple Setup: Built on top of boto3/aiobotocore, the library manages AWS Lambda client connections and simplifies API invocation
  • Response Handling: Automatic parsing of Lambda responses with proper error handling

Installation

To install the library, use pip:

pip install call-serverless

Requirements

  • Python 3.6 or higher
  • boto3 for AWS SDK integration
  • aiobotocore for async operations

Usage

Synchronous Lambda Invocation

from call_serverless.apis import call_lambda

# Basic GET request
response = call_lambda(
    lambda_arn="arn:aws:lambda:us-west-2:123456789012:function:MyFunction",
    path="/users/{id}",
    method="GET",
    path_params={"id": "123"},
    query_params={"include": "profile"},
    headers={"Authorization": "Bearer token123"}
)

print(response.status_code)  # 200
print(response.body)  # {"name": "John Doe", "email": "john@example.com"}

# POST request with body
response = call_lambda(
    lambda_arn="arn:aws:lambda:us-west-2:123456789012:function:MyFunction",
    path="/users",
    method="POST",
    body={"username": "new_user", "email": "user@example.com"},
    headers={"Content-Type": "application/json"}
)

# Handle errors
try:
    response.raise_for_status()
except ErrorResponse as e:
    print(f"Error: {e}")

Asynchronous Lambda Invocation

import asyncio
from call_serverless.apis import call_lambda_async

async def get_user():
    response = await call_lambda_async(
        lambda_arn="arn:aws:lambda:us-west-2:123456789012:function:MyFunction",
        path="/users/{id}",
        method="GET",
        path_params={"id": "123"},
        query_params={"include": "profile"},
        headers={"Authorization": "Bearer token123"}
    )
    return response.body

# Run the async function
user = asyncio.run(get_user())

Response Object

The library returns a CLResponse object that provides:

  • status_code: HTTP status code from the Lambda response
  • body: Response body (automatically parsed as JSON if possible)
  • raise_for_status(): Method to raise an exception for error status codes (>= 400)

Error Handling

The library provides several exception classes:

  • LambdaAccessError: Raised when there are issues accessing or executing the Lambda function
  • InvalidResponseFormat: Raised when the Lambda response is not in the expected format
  • ErrorResponse: Raised when the Lambda returns a status code >= 400

Contributing

If you want to contribute to this project, please submit a pull request or open an issue on GitHub.

License

This project is licensed under the MIT License. See the LICENSE file for more details.

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

call_serverless-0.2.9.tar.gz (6.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

call_serverless-0.2.9-py3-none-any.whl (8.0 kB view details)

Uploaded Python 3

File details

Details for the file call_serverless-0.2.9.tar.gz.

File metadata

  • Download URL: call_serverless-0.2.9.tar.gz
  • Upload date:
  • Size: 6.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.3

File hashes

Hashes for call_serverless-0.2.9.tar.gz
Algorithm Hash digest
SHA256 e9fe1df2a6b669dbe9c7c611dd97a5a7bc8b7ad193eee511eb27adba80231987
MD5 7f9cafd038996cc7c63bb36dfcf3c76d
BLAKE2b-256 1f5e78fbd930f74f62451097e658c12787ec1e3ca3ed8b157c9ca87d880701ea

See more details on using hashes here.

File details

Details for the file call_serverless-0.2.9-py3-none-any.whl.

File metadata

File hashes

Hashes for call_serverless-0.2.9-py3-none-any.whl
Algorithm Hash digest
SHA256 d8eb3b20f3d5af96c6933a8cf6392511f53583dfa5f344998317f8aa4c9c8ca1
MD5 8a6288b0ad8d5a00f3adf6523fa9ade5
BLAKE2b-256 e9cb42e86b2ae9885ca72782b774146d09bf403acdf1f67a80c22e94b65c6cf4

See more details on using hashes here.

Supported by

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