Lambda-httpx; use familiar async httpx library to access HTTP enabled (simple proxy) AWS Lambda functions
Quick start
Installation
pip intall lambda-httpx
Usage Examples
Lambda-httpx provides a transport which can be mounted with the httpx client. This then relays over all requests with http+lambda:// to AWS lambda whose name matches the hostname.
Using from existing event loop:
import httpx
from lambda_httpx import AsyncLambdaTransport
# ...
async with AsyncLambdaTransport() as transport:
mounts = {"http+lambda://": transport}
async with httpx.AsyncClient(mounts=mounts) as client:
response = await client.get("http+lambda://flaskexp-test/health")
Stand alone that calls endpoint 10 times asyncronously.
import asyncio
import httpx
from lambda_httpx import AsyncLambdaTransport
async def main(count):
async with AsyncLambdaTransport() as transport:
mounts = {"http+lambda://": transport}
async with httpx.AsyncClient(mounts=mounts) as client:
coros = [client.get("http+lambda://flaskexp-test/health") for _ in range(count)]
return await asyncio.gather(*coros)
if __name__ == "__main__":
loop = asyncio.get_event_loop()
responses = loop.run_until_complete(main(10))
print(responses)
A synchronous Transport also exists and can be utilized as follows:
import httpx
from lambda_httpx import LambdaTransport
with LambdaTransport() as transport:
mounts = {"http+lambda://": transport}
with httpx.Client(mounts=mounts) as client:
response = client.get("http+lambda://flaskexp-test/health")
print(response)
Lambda authorization is configured via boto3, and can be set up using environment variables or a configuration file. Configuration file is recommended. Example credential file ~/.aws/credentials:
[default]
aws_access_key_id = XXXXXXXXXXXXXXXXXXXX
aws_secret_access_key = XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Similar to authorization, region can be configured either via the environment variable AWS_DEFAULT_REGION, configuration file. Region can also be set on initialization of AsyncLambdaTransport(region=”us-west-2”). Example configuration file ~/.aws/config:
[profile default]
region = us-west-2
The lambdas must support proxy integration, which is used commonly by frameworks such as Zappa, Mangum.
Why
In using REST microservice architecture it is important to be able to conveniently make calls from one service to another. To use this pattern in AWS serverless ecosphere along with Lambda one is practically forced to stand up an API Gateway in front of the lambda. This has several distinct disadvantages, all mostly along the lines of security.
API Gateway publicly exposes endpoints
API Gateway uses own authentication / authorization schema. While Lambda already supplies us with IAM.
Extra dependencies in call chain. While availability is high, latency may still be of concern.
Over all, to reduce exposure of private sub-services, re-use IAM authentication / authorization and reduce latency.
How does its work
Simple, we register a scheme name with httpx and use a lambda specific transport adapter which translates a httpx request to lambda invoke compatible with AWS API Gateway simple proxy format.
See also
Lambda-requests: Similar library that allows same functionality via python requests library.
Metadata
Release files for lambda-httpx 0.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| lambda-httpx-0.2.tar.gz | 5.8 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| lambda_httpx-0.2-py2.py3-none-any.whl | Python 2, Python 3 | none | any | Details |
Total release size: 11.5 kB
Release files / lambda-httpx-0.2.tar.gz
| Download URL | lambda-httpx-0.2.tar.gz |
|---|---|
| Size | 5.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
570930a8d348ca7ea7b58727db63940cb843c21e35d126b0b941cfe07f79f341
|
|
BLAKE2b-256 checksum How to use checksums |
59b5a5eca59f44728a19d84c4525d5475398de9131084f1e84c1500e7c920eac
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.9.16
|
Release files / lambda_httpx-0.2-py2.py3-none-any.whl
| Download URL | lambda_httpx-0.2-py2.py3-none-any.whl |
|---|---|
| Size | 5.7 kB |
| Tags | Python 2 Python 3 |
|
SHA-256 checksum How to use checksums |
cfdae729affdb0818645e5a9ad7f6adaa9f34d6c2f7505a1ebe3fcf8091d27a3
|
|
BLAKE2b-256 checksum How to use checksums |
cc0606288e6efb4ede8a517db7015b35791f347cf49e39ba6d33f41c7a640aa9
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.9.16
|