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.8.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.8-py3-none-any.whl (7.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: call_serverless-0.2.8.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.8.tar.gz
Algorithm Hash digest
SHA256 2129fc3680a2782b780e39732fd9e6f6d9a68836efff1216d7f878afac7bcae2
MD5 559322c60032303643f976efa6d63b44
BLAKE2b-256 35523b50775b9f058c02689611bb74fd47592ed1d91e1b041bd431c135c5e243

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for call_serverless-0.2.8-py3-none-any.whl
Algorithm Hash digest
SHA256 4ea5053a367bfef560556e03d5299108ea71087b749e89a677613066485a0ec7
MD5 09a34faaa14ca124822db6320e0d2c00
BLAKE2b-256 bb5670dcc5efeb1c6bcf209042fc2025d6f277b37a492511b6c31bd44e736be2

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